use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class InstantiatorBuilder method appendNewInstanceBuilderOnMethod.
private static <S> void appendNewInstanceBuilderOnMethod(Class<?> sourceClass, ExecutableInstantiatorDefinition instantiatorDefinition, Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, String targetType, String sourceType, String classType, Parameter[] parameters) throws NoSuchMethodException {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(sourceType) + ")" + AsmUtils.toTargetTypeDeclaration(targetType), null, new String[] { "java/lang/Exception" });
mv.visitCode();
mv.visitTypeInsn(NEW, targetType);
mv.visitInsn(DUP);
StringBuilder sb = new StringBuilder();
for (Parameter p : parameters) {
Getter<? super S, ?> getter = injections.get(p);
sb.append(AsmUtils.toTargetTypeDeclaration(p.getType()));
if (getter == null) {
if (TypeHelper.isPrimitive(p.getType())) {
mv.visitInsn(AsmUtils.defaultValue.get(p.getType()));
} else {
mv.visitInsn(ACONST_NULL);
}
} else {
invokeGetter(p, getter, classType, sourceClass, mv, null, false);
}
}
Member exec = instantiatorDefinition.getExecutable();
if (exec instanceof Constructor) {
mv.visitMethodInsn(INVOKESPECIAL, targetType, "<init>", "(" + sb.toString() + ")V", false);
} else {
mv.visitMethodInsn(INVOKESTATIC, AsmUtils.toAsmType(((Method) exec).getDeclaringClass()), exec.getName(), AsmUtils.toSignature((Method) exec), false);
}
mv.visitInsn(ARETURN);
mv.visitMaxs(3, 2);
mv.visitEnd();
}
use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceMapperBuilder method getConstructorFieldMappersAndInstantiator.
@SuppressWarnings("unchecked")
private InstantiatorAndFieldMappers getConstructorFieldMappersAndInstantiator() throws MapperBuildingException {
InstantiatorFactory instantiatorFactory = reflectionService.getInstantiatorFactory();
try {
ConstructorInjections constructorInjections = constructorInjections();
Map<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>> injections = constructorInjections.parameterGetterMap;
MapperBiInstantiatorFactory mapperBiInstantiatorFactory = new MapperBiInstantiatorFactory(instantiatorFactory);
GetterFactory<? super S, K> getterFactory = fieldMapperAsGetterFactory();
BiInstantiator<S, MappingContext<? super S>, T> instantiator = mapperBiInstantiatorFactory.<S, T, K, FieldMapperColumnDefinition<K>>getBiInstantiator(mapperSource.source(), target, propertyMappingsBuilder, injections, getterFactory, reflectionService.builderIgnoresNullValues());
return new InstantiatorAndFieldMappers(constructorInjections.fieldMappers, instantiator);
} catch (Exception e) {
return ErrorHelper.rethrow(e);
}
}
Aggregations