Search in sources :

Example 6 with MethodVisitor

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

the class BiInstantiatorBuilder method appendToString.

private static <S1, S2> void appendToString(List<InjectionPoint> 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);
        InjectionPoint injectionPoint = findFunctionCalls(parameters[i], injections);
        String getterName = ", parameter" + i + "=";
        String getterString = injectionPoint != null ? injectionPoint.functionType.toString() : "<null>";
        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 7 with MethodVisitor

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

the class BiInstantiatorBuilder method appendInitBuilder.

private static <S1, S2> void appendInitBuilder(List<InjectionPoint> injections, ClassWriter cw, String s1, String s2, String classType, BuilderInstantiatorDefinition instantiatorDefinition) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/util/Map;L" + AsmUtils.toAsmType(INSTANTIATOR_CLASS) + ";)V", "(Ljava/util/Map<Ljava.lang.String;L" + AsmUtils.toAsmType(BiFunction.class) + "<" + AsmUtils.toTargetTypeDeclaration(s1) + "*" + AsmUtils.toTargetTypeDeclaration(s2) + "*>;>;" + "L" + AsmUtils.toAsmType(INSTANTIATOR_CLASS) + "<Ljava/lang/Void;L" + AsmUtils.toAsmType(getTargetType(instantiatorDefinition.getBuilderInstantiator())) + ";>;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitFieldInsn(PUTFIELD, classType, "builderInstantiator", "L" + AsmUtils.toAsmType(INSTANTIATOR_CLASS) + ";");
    appendInitField(injections, classType, mv);
    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}
Also used : BiFunction(org.simpleflatmapper.util.BiFunction) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 8 with MethodVisitor

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

the class GetterBuilder method appendBridge.

private static void appendBridge(ClassWriter cw, String targetType, String propertyType, String classType) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "get", "(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, targetType);
    mv.visitMethodInsn(INVOKEVIRTUAL, classType, "get", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + ")" + AsmUtils.toTargetTypeDeclaration(propertyType), false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}
Also used : MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 9 with MethodVisitor

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

the class GetterBuilder method appendToString.

private static void appendToString(ClassWriter cw) {
    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);
    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 10 with MethodVisitor

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

the class GetterBuilder method createPrimitiveGetter.

public static byte[] createPrimitiveGetter(String className, Method method) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    Class<?> target = method.getDeclaringClass();
    Class<?> primitive = method.getReturnType();
    Class<?> property = AsmUtils.wrappers.get(primitive);
    String targetType = toType(target);
    String primitiveType = AsmUtils.primitivesType.get(primitive);
    String propertyType = toType(property);
    String classType = toType(className);
    String methodSuffix = property.getSimpleName();
    if ("Integer".equals(methodSuffix)) {
        methodSuffix = "Int";
    }
    String getMethod = "get" + methodSuffix;
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;" + "L" + GETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;" + "L" + ORG_SFM_REFLECT_PRIMITIVE + methodSuffix + "Getter<L" + targetType + ";>;", "java/lang/Object", new String[] { GETTER_TYPE, ORG_SFM_REFLECT_PRIMITIVE + "/" + methodSuffix + "Getter" });
    {
        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, getMethod, "(" + AsmUtils.toTargetTypeDeclaration(targetType) + ")" + primitiveType, null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        AsmUtils.invoke(mv, target, method.getName(), "()" + primitiveType);
        mv.visitInsn(AsmUtils.returnOps.get(primitive));
        mv.visitMaxs(1, 2);
        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(), "()" + primitiveType);
        mv.visitMethodInsn(INVOKESTATIC, propertyType, "valueOf", "(" + primitiveType + ")L" + propertyType + ";", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    appendPrimitiveBridges(cw, primitive, targetType, primitiveType, propertyType, classType, getMethod);
    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