Search in sources :

Example 21 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorBuilder method appendNewInstanceBuilder.

private static <S1, S2> void appendNewInstanceBuilder(Class<?> s1, Class<?> s2, BuilderInstantiatorDefinition instantiatorDefinition, List<InjectionPoint> injectionPoints, ClassWriter cw, String targetType, String s1Type, String s2Type, String classType, Map<Parameter, Method> setters, boolean ignoreNullValues) throws NoSuchMethodException {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(s1Type) + AsmUtils.toTargetTypeDeclaration(s2Type) + ")" + 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 = getTargetType(instantiatorDefinition.getBuilderInstantiator());
    final String builderType = AsmUtils.toAsmType(builderClass);
    mv.visitTypeInsn(CHECKCAST, builderType);
    mv.visitVarInsn(ASTORE, 2);
    for (final Entry<Parameter, Method> e : setters.entrySet()) {
        Parameter p = e.getKey();
        final InjectionPoint injectionPoint = findFunctionCalls(p, injectionPoints);
        if (injectionPoint != null) {
            final boolean checkIfNull = ignoreNullValues && !injectionPoint.isPrimitive;
            mv.visitVarInsn(ALOAD, 2);
            invokeBiFunction(injectionPoint, classType, s1, s2, mv, new Consumer<MethodVisitor>() {

                @Override
                public void accept(MethodVisitor mv) {
                    if (checkIfNull) {
                        mv.visitVarInsn(ALOAD, 2);
                        mv.visitVarInsn(AsmUtils.getLoadOps(injectionPoint.parameter.getType()), 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);
        }
    }
    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) Method(java.lang.reflect.Method) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 22 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.

the class ConstructorBuilder method createEmptyConstructor.

public static byte[] createEmptyConstructor(final String className, final Class<?> sourceClass, final Class<?> targetClass) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    String targetType = AsmUtils.toAsmType(targetClass);
    String sourceType = AsmUtils.toAsmType(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 });
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(L" + sourceType + ";)L" + targetType + ";", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitTypeInsn(NEW, targetType);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, targetType, "<init>", "()V", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, sourceType);
        mv.visitMethodInsn(INVOKEVIRTUAL, classType, "newInstance", "(L" + sourceType + ";)L" + targetType + ";", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 23 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.

the class AsmUtilsTest method testAddIndex.

@Test
public void testAddIndex() {
    MethodVisitor mv = mock(MethodVisitor.class);
    AsmUtils.addIndex(mv, 0);
    verify(mv).visitInsn(ICONST_0);
    AsmUtils.addIndex(mv, 1);
    verify(mv).visitInsn(ICONST_1);
    AsmUtils.addIndex(mv, 2);
    verify(mv).visitInsn(ICONST_2);
    AsmUtils.addIndex(mv, 3);
    verify(mv).visitInsn(ICONST_3);
    AsmUtils.addIndex(mv, 4);
    verify(mv).visitInsn(ICONST_4);
    AsmUtils.addIndex(mv, 5);
    verify(mv).visitInsn(ICONST_5);
    AsmUtils.addIndex(mv, 123);
    verify(mv).visitIntInsn(BIPUSH, 123);
    AsmUtils.addIndex(mv, 512);
    verify(mv).visitIntInsn(SIPUSH, 512);
    AsmUtils.addIndex(mv, Short.MAX_VALUE + 1);
    verify(mv).visitLdcInsn(Short.MAX_VALUE + 1);
}
Also used : MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor) Test(org.junit.Test)

Example 24 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.

the class MapperAsmBuilder method dump.

public static <S, T> byte[] dump(final String className, final FieldMapper<S, T>[] mappers, final FieldMapper<S, T>[] constructorMappers, final Class<? super S> sourceClass, final Class<T> target) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    final String targetType = AsmUtils.toAsmType(target);
    final String classType = AsmUtils.toAsmType(className);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "L" + ABSTRACT_MAPPER_TYPE + "<" + toTargetTypeDeclaration(targetType) + ">;", ABSTRACT_MAPPER_TYPE, null);
    for (int i = 0; i < mappers.length; i++) {
        declareMapperFields(cw, mappers[i], i);
    }
    for (int i = 0; i < constructorMappers.length; i++) {
        declareConstructorMapperFields(cw, constructorMappers[i], i);
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "([L" + FIELD_MAPPER_TYPE + ";" + "[L" + FIELD_MAPPER_TYPE + ";L" + INSTANTIATOR_TYPE + ";)V", "(" + "[L" + FIELD_MAPPER_TYPE + "<" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + ">;" + "[L" + FIELD_MAPPER_TYPE + "<" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + ">;" + "L" + INSTANTIATOR_TYPE + "<" + toTargetTypeDeclaration(targetType) + ">;)V", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESPECIAL, ABSTRACT_MAPPER_TYPE, "<init>", "(L" + INSTANTIATOR_TYPE + ";)V", false);
        for (int i = 0; i < mappers.length; i++) {
            addFieldMapperInit(mv, mappers[i], i, classType);
        }
        for (int i = 0; i < constructorMappers.length; i++) {
            addGConstructorFieldMapperInit(mv, constructorMappers[i], i, classType);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "mapFields", "(" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + toTargetTypeDeclaration(mappingContextType) + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        for (int i = 0; i < mappers.length; i++) {
            generateMappingCall(mv, mappers[i], i, classType, AsmUtils.toAsmType(sourceClass), targetType);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "mapFields", "(Ljava/lang/Object;Ljava/lang/Object;" + toTargetTypeDeclaration(mappingContextType) + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, AsmUtils.toAsmType(sourceClass));
        mv.visitVarInsn(ALOAD, 2);
        mv.visitTypeInsn(CHECKCAST, targetType);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, classType, "mapFields", "(" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + toTargetTypeDeclaration(mappingContextType) + ")V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_FINAL, "mapToFields", "(" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + toTargetTypeDeclaration(mappingContextType) + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        for (int i = 0; i < constructorMappers.length; i++) {
            generateConstructorMappingCall(mv, constructorMappers[i], i, classType, AsmUtils.toAsmType(sourceClass), targetType);
        }
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, classType, "mapFields", "(" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + toTargetTypeDeclaration(mappingContextType) + ")V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "mapToFields", "(Ljava/lang/Object;Ljava/lang/Object;" + toTargetTypeDeclaration(mappingContextType) + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, AsmUtils.toAsmType(sourceClass));
        mv.visitVarInsn(ALOAD, 2);
        mv.visitTypeInsn(CHECKCAST, targetType);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, classType, "mapToFields", "(" + toTargetTypeDeclaration(sourceClass) + toTargetTypeDeclaration(targetType) + toTargetTypeDeclaration(mappingContextType) + ")V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getSimpleName", "()Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitLdcInsn("{");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, ABSTRACT_MAPPER_TYPE, "appendToStringBuilder", "(Ljava/lang/StringBuilder;)V", false);
        mv.visitVarInsn(ALOAD, 1);
        for (int i = 0; i < mappers.length; i++) {
            String mapperName = ", fieldMapper" + i + "=";
            String mapper = String.valueOf(mappers[i]);
            mv.visitLdcInsn(mapperName);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
            mv.visitLdcInsn(mapper);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        }
        for (int i = 0; i < constructorMappers.length; i++) {
            String mapperName = ", constructorMapper" + i + "=";
            String mapper = String.valueOf(constructorMappers[i]);
            mv.visitLdcInsn(mapperName);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
            mv.visitLdcInsn(mapper);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        }
        mv.visitLdcInsn("}");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 25 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.

the class GetterBuilder method createObjectGetter.

public static byte[] createObjectGetter(final String className, final Field field) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    Class<?> target = field.getDeclaringClass();
    Class<?> property = field.getType();
    String targetType = toType(target);
    String propertyType = toType(property);
    String classType = toType(className);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + GETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { GETTER_TYPE });
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "get", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + ")" + AsmUtils.toTargetTypeDeclaration(propertyType), null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(GETFIELD, targetType, field.getName(), AsmUtils.toTargetTypeDeclaration(propertyType));
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    appendBridge(cw, targetType, propertyType, classType);
    appendToString(cw);
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Aggregations

MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)37 ClassWriter (org.simpleflatmapper.ow2asm.ClassWriter)12 Parameter (org.simpleflatmapper.reflect.Parameter)5 Method (java.lang.reflect.Method)4 Label (org.simpleflatmapper.ow2asm.Label)4 Member (java.lang.reflect.Member)3 Type (java.lang.reflect.Type)3 Instantiator (org.simpleflatmapper.reflect.Instantiator)3 Constructor (java.lang.reflect.Constructor)2 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 ParsingContext (org.simpleflatmapper.csv.ParsingContext)1 ParsingContextFactory (org.simpleflatmapper.csv.ParsingContextFactory)1 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)1 CsvMapperCellHandler (org.simpleflatmapper.csv.mapper.CsvMapperCellHandler)1 DelayedCellSetter (org.simpleflatmapper.csv.mapper.DelayedCellSetter)1