Search in sources :

Example 31 with Parameter

use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorDefinitionTest method setUp.

@Before
public void setUp() throws Exception {
    c = InstantiatorDefinitionTest.class.getConstructor();
    setUpMethod = InstantiatorDefinitionTest.class.getMethod("setUp");
    valueOfMethod = InstantiatorDefinitionTest.class.getMethod("valueOf");
    parameter = new Parameter(0, "i0", String.class);
}
Also used : Parameter(org.simpleflatmapper.reflect.Parameter) Before(org.junit.Before)

Example 32 with Parameter

use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorDefinitionTest method testTypeAffinityCompatibilityScorers.

@Test
public void testTypeAffinityCompatibilityScorers() {
    InstantiatorDefinitions.CompatibilityScorer compatibilityScorer = InstantiatorDefinitions.getCompatibilityScorer(new TypeAffinity() {

        @Override
        public Class<?>[] getAffinities() {
            return new Class<?>[] { Date.class, URL.class };
        }
    });
    assertEquals(0, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", InputStream.class))));
    assertEquals(12, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Date.class))));
    assertEquals(11, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", URL.class))));
    assertEquals(1, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Number.class))));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinitions(org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions) InputStream(java.io.InputStream) TypeAffinity(org.simpleflatmapper.reflect.TypeAffinity) Parameter(org.simpleflatmapper.reflect.Parameter) Date(java.util.Date) URL(java.net.URL) Test(org.junit.Test)

Example 33 with Parameter

use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorBuilder method createInstantiator.

public static <S> byte[] createInstantiator(final String className, final Class<?> sourceClass, final ExecutableInstantiatorDefinition instantiatorDefinition, final Map<Parameter, Getter<? super S, ?>> injections) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    Class<?> targetClass = TypeHelper.toClass(BiInstantiatorBuilder.getTargetType(instantiatorDefinition));
    String targetType = AsmUtils.toAsmType(targetClass);
    String sourceType = AsmUtils.toWrapperType(sourceClass);
    String classType = AsmUtils.toAsmType(className);
    String instantiatorType = AsmUtils.toAsmType(Instantiator.class);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + instantiatorType + "<L" + targetType + ";>;", "java/lang/Object", new String[] { instantiatorType });
    Parameter[] parameters = instantiatorDefinition.getParameters();
    appendGetters(injections, cw);
    appendInit(injections, cw, sourceType, classType);
    appendNewInstanceBuilderOnMethod(sourceClass, instantiatorDefinition, injections, cw, targetType, sourceType, classType, parameters);
    appendBridgeMethod(cw, targetType, sourceType, classType);
    appendToString(injections, cw, parameters);
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : Parameter(org.simpleflatmapper.reflect.Parameter) ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter)

Example 34 with Parameter

use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorBuilder method appendNewInstanceBuilderOnBuilder.

private static <S> void appendNewInstanceBuilderOnBuilder(Class<?> sourceClass, BuilderInstantiatorDefinition instantiatorDefinition, Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, String targetType, String sourceType, String classType, Map<Parameter, Method> setters, boolean ignoreNullValues) 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.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classType, "builderInstantiator", AsmUtils.toTargetTypeDeclaration(Instantiator.class));
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEINTERFACE, AsmUtils.toAsmType(Instantiator.class), "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    final Type builderClass = BiInstantiatorBuilder.getTargetType(instantiatorDefinition.getBuilderInstantiator());
    final String builderType = AsmUtils.toAsmType(builderClass);
    mv.visitTypeInsn(CHECKCAST, builderType);
    mv.visitVarInsn(ASTORE, 2);
    boolean first = true;
    for (final Entry<Parameter, Method> e : setters.entrySet()) {
        Parameter p = e.getKey();
        Getter<? super S, ?> getter = injections.get(p);
        if (getter != null) {
            final Class<?> parameterType = p.getType();
            final boolean checkIfNull = ignoreNullValues && !getGetterCall(parameterType, getter.getClass()).isPrimitive;
            mv.visitVarInsn(ALOAD, 2);
            invokeGetter(p, getter, classType, sourceClass, mv, new Consumer<MethodVisitor>() {

                @Override
                public void accept(MethodVisitor mv) {
                    if (checkIfNull) {
                        mv.visitVarInsn(ALOAD, 2);
                        mv.visitVarInsn(AsmUtils.getLoadOps(parameterType), 3);
                    }
                    AsmUtils.invoke(mv, TypeHelper.toClass(builderClass), e.getValue().getName(), AsmUtils.toSignature(e.getValue()));
                    if (!Void.TYPE.equals(e.getValue().getReturnType())) {
                        mv.visitVarInsn(ASTORE, 2);
                    }
                }
            }, ignoreNullValues);
            if (checkIfNull) {
                if (first && !Void.TYPE.equals(e.getValue().getReturnType())) {
                    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { builderType }, 0, null);
                } else {
                    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                }
                first = false;
            }
        }
    }
    mv.visitVarInsn(ALOAD, 2);
    AsmUtils.invoke(mv, TypeHelper.toClass(builderClass), instantiatorDefinition.getBuildMethod().getName(), AsmUtils.toSignature(instantiatorDefinition.getBuildMethod()));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}
Also used : Type(java.lang.reflect.Type) Parameter(org.simpleflatmapper.reflect.Parameter) Instantiator(org.simpleflatmapper.reflect.Instantiator) Method(java.lang.reflect.Method) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 35 with Parameter

use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.

the class InstantiatorBuilder method appendGetters.

private static <S> void appendGetters(Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw) {
    FieldVisitor fv;
    for (Entry<Parameter, Getter<? super S, ?>> entry : injections.entrySet()) {
        GetterCall getterCall = getGetterCall(entry.getKey().getType(), entry.getValue().getClass());
        fv = cw.visitField(ACC_FINAL, "getter_" + entry.getKey().getName(), AsmUtils.toTargetTypeDeclaration(getterCall.getterType), null, null);
        fv.visitEnd();
    }
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Parameter(org.simpleflatmapper.reflect.Parameter) FieldVisitor(org.simpleflatmapper.ow2asm.FieldVisitor)

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