Search in sources :

Example 11 with ClassWriter

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

Example 12 with ClassWriter

use of org.simpleflatmapper.ow2asm.ClassWriter 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 13 with ClassWriter

use of org.simpleflatmapper.ow2asm.ClassWriter 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 14 with ClassWriter

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

the class SetterBuilder method createObjectSetter.

public static byte[] createObjectSetter(final String className, final Method method) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    Class<?> target = method.getDeclaringClass();
    Class<?> property = method.getParameterTypes()[0];
    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" + SETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { SETTER_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();
    }
    appendSet(method, cw, target, 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 15 with ClassWriter

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

the class SetterBuilder method createObjectSetter.

public static byte[] createObjectSetter(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" + SETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { SETTER_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();
    }
    appendSet(field, cw, target, 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

ClassWriter (org.simpleflatmapper.ow2asm.ClassWriter)16 MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)12 FieldVisitor (org.simpleflatmapper.ow2asm.FieldVisitor)3 Parameter (org.simpleflatmapper.reflect.Parameter)2 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)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 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)1 Instantiator (org.simpleflatmapper.reflect.Instantiator)1