Search in sources :

Example 36 with Parameter

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();
}
Also used : Constructor(java.lang.reflect.Constructor) Parameter(org.simpleflatmapper.reflect.Parameter) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 37 with Parameter

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);
    }
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) MappingContext(org.simpleflatmapper.map.MappingContext) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter)

Aggregations

Parameter (org.simpleflatmapper.reflect.Parameter)37 Test (org.junit.Test)20 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)20 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)17 HashMap (java.util.HashMap)16 BiFunction (org.simpleflatmapper.util.BiFunction)16 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)10 InstantiatorFactory (org.simpleflatmapper.reflect.InstantiatorFactory)10 ConstantBiFunction (org.simpleflatmapper.util.ConstantBiFunction)9 Getter (org.simpleflatmapper.reflect.Getter)6 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)6 BuilderBiInstantiator (org.simpleflatmapper.reflect.impl.BuilderBiInstantiator)6 MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)5 Method (java.lang.reflect.Method)4 Type (java.lang.reflect.Type)4 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)4 DbFinalPrimitiveObject (org.simpleflatmapper.test.beans.DbFinalPrimitiveObject)4 InputStream (java.io.InputStream)3 Member (java.lang.reflect.Member)3 Constructor (java.lang.reflect.Constructor)2