Search in sources :

Example 11 with MethodVisitor

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

the class GetterBuilder method createPrimitiveGetter.

public static byte[] createPrimitiveGetter(String className, Field field) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    Class<?> target = field.getDeclaringClass();
    Class<?> primitive = field.getType();
    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);
        mv.visitFieldInsn(GETFIELD, targetType, field.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);
        mv.visitFieldInsn(GETFIELD, targetType, field.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)

Example 12 with MethodVisitor

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

the class InstantiatorBuilder method appendInit.

private static <S> void appendInit(Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, String sourceType, String classType) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/util/Map;)V", "(Ljava/util/Map<Ljava.lang.String;L" + AsmUtils.toAsmType(Getter.class) + "<" + AsmUtils.toTargetTypeDeclaration(sourceType) + "*>;>;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    appendInitGetters(injections, classType, mv);
    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}
Also used : MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 13 with MethodVisitor

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

the class SetterBuilder method appendSet.

private static void appendSet(Field field, ClassWriter cw, Class<?> target, String targetType, String propertyType, String classType) {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(ACC_PUBLIC, "set", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + AsmUtils.toTargetTypeDeclaration(propertyType) + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(PUTFIELD, targetType, field.getName(), AsmUtils.toTargetTypeDeclaration(propertyType));
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    appendSynthetic(cw, targetType, propertyType, classType);
}
Also used : MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 14 with MethodVisitor

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

the class SetterBuilder method createPrimitiveSetter.

public static byte[] createPrimitiveSetter(String className, Field field) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    Class<?> target = field.getDeclaringClass();
    Class<?> primitive = field.getType();
    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 valueMethodPrefix = methodSuffix.toLowerCase();
    if ("character".equals(valueMethodPrefix)) {
        valueMethodPrefix = "char";
    }
    String setMethod = "set" + methodSuffix;
    String valueMethod = valueMethodPrefix + "Value";
    int primitiveLoadOp = AsmUtils.loadOps.get(primitive);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;" + "L" + SETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;" + "L" + ORG_SFM_REFLECT_PRIMITIVE + methodSuffix + "Setter<L" + targetType + ";>;", "java/lang/Object", new String[] { SETTER_TYPE, ORG_SFM_REFLECT_PRIMITIVE + "/" + methodSuffix + "Setter" });
    {
        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, setMethod, "(" + AsmUtils.toTargetTypeDeclaration(targetType) + primitiveType + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(primitiveLoadOp, 2);
        mv.visitFieldInsn(PUTFIELD, targetType, field.getName(), primitiveType);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "set", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + AsmUtils.toTargetTypeDeclaration(propertyType) + ")V", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        AsmUtils.invoke(mv, property, valueMethod, "()" + primitiveType);
        mv.visitFieldInsn(PUTFIELD, targetType, field.getName(), primitiveType);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    appendPrimitiveSynthetic(cw, targetType, primitiveType, propertyType, classType, setMethod, primitiveLoadOp);
    appendToString(cw);
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 15 with MethodVisitor

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

the class CsvMapperCellHandlerBuilder method createTargetSetterClass.

public static <T> byte[] createTargetSetterClass(String className, DelayedCellSetterFactory<T, ?>[] delayedCellSetters, CellSetter<T>[] setters, Type type, boolean ignoreException, int maxMethodSize) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    FieldVisitor fv;
    MethodVisitor mv;
    final String targetType = AsmUtils.toAsmType(type);
    final String classType = AsmUtils.toAsmType(className);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classType, "L" + CSV_CELL_MAPPER_TYPE + "<L" + targetType + ";>;", CSV_CELL_MAPPER_TYPE, null);
    // declare fields
    for (int i = 0; i < delayedCellSetters.length; i++) {
        if (delayedCellSetters[i] != null) {
            fv = cw.visitField(Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL, "delayedCellSetter" + i, AsmUtils.toTargetTypeDeclaration(DELAYED_CELL_SETTER_TYPE), "L" + DELAYED_CELL_SETTER_TYPE + "<L" + targetType + ";*>;", null);
            fv.visitEnd();
        }
    }
    for (int i = 0; i < setters.length; i++) {
        if (setters[i] != null) {
            fv = cw.visitField(Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL, "setter" + i, AsmUtils.toTargetTypeDeclaration(CELL_SETTER_TYPE), "L" + CELL_SETTER_TYPE + "<L" + targetType + ";>;", null);
            fv.visitEnd();
        }
    }
    appendInit(delayedCellSetters, setters, cw, targetType, classType, maxMethodSize);
    appendDelayedCellValue(delayedCellSetters, ignoreException, cw, classType);
    append_delayedCellValue(delayedCellSetters, cw, classType, maxMethodSize);
    appendCellValue(setters, ignoreException, cw, classType);
    append_cellValue(delayedCellSetters, setters, cw, classType, maxMethodSize);
    appendApplyDelayedSetter(delayedCellSetters, ignoreException, cw, classType, maxMethodSize);
    appendApplyDelayedCellSetterN(delayedCellSetters, cw, classType);
    appendGetDelayedCellSetter(delayedCellSetters, cw, targetType, classType, maxMethodSize);
    appendPeekDelayedCellSetterValue(delayedCellSetters, cw, classType, maxMethodSize);
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : FieldVisitor(org.simpleflatmapper.ow2asm.FieldVisitor) 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