Search in sources :

Example 26 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 Method method) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    Class<?> target = method.getDeclaringClass();
    Class<?> property = method.getReturnType();
    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);
        AsmUtils.invoke(mv, target, method.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)

Example 27 with MethodVisitor

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

the class InstantiatorBuilder method appendToString.

private static <S> void appendToString(Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, Parameter[] parameters) {
    MethodVisitor mv;
    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);
    for (int i = 0; i < parameters.length; i++) {
        String paramName = (i > 0 ? ", " : "") + "parameter" + i + "=";
        String parameter = String.valueOf(parameters[i]);
        mv.visitLdcInsn(paramName);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitLdcInsn(parameter);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        Getter<? super S, ?> getter = injections.get(parameters[i]);
        String getterName = ", parameter" + i + "=";
        String getterString = String.valueOf(getter);
        mv.visitLdcInsn(getterName);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitLdcInsn(getterString);
        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();
}
Also used : MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 28 with MethodVisitor

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

the class InstantiatorBuilder method appendBridgeMethod.

private static void appendBridgeMethod(ClassWriter cw, String targetType, String sourceType, String classType) {
    MethodVisitor mv;
    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", "(" + AsmUtils.toTargetTypeDeclaration(sourceType) + ")" + AsmUtils.toTargetTypeDeclaration(targetType), false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}
Also used : MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 29 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor 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 30 with MethodVisitor

use of org.simpleflatmapper.ow2asm.MethodVisitor 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)

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