Search in sources :

Example 6 with ClassWriter

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

Example 7 with ClassWriter

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

the class BiInstantiatorBuilder method createInstantiator.

public static <S1, S2> byte[] createInstantiator(final String className, final Class<?> s1, final Class<?> s2, final ExecutableInstantiatorDefinition instantiatorDefinition, final Map<Parameter, BiFunction<? super S1, ? super S2, ?>> injectionsMap) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    Class<?> targetClass = TypeHelper.toClass(getTargetType(instantiatorDefinition));
    String targetType = AsmUtils.toAsmType(targetClass);
    String s1Type = AsmUtils.toWrapperType(s1);
    String s2Type = AsmUtils.toWrapperType(s2);
    String classType = AsmUtils.toAsmType(className);
    String instantiatorType = AsmUtils.toAsmType(BI_INSTANTIATOR_CLASS);
    List<InjectionPoint> injections = toInjections(injectionsMap);
    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();
    appendFunctionsField(injections, cw);
    appendInit(injections, cw, s1Type, s2Type, classType);
    appendNewInstance(s1, s2, instantiatorDefinition, injections, cw, targetType, s1Type, s2Type, classType, parameters);
    appendBridgeMethod(cw, targetType, s1Type, s2Type, 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 8 with ClassWriter

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

the class BiInstantiatorBuilder method createInstantiator.

public static <S1, S2> byte[] createInstantiator(final String className, final Class<?> s1, final Class<?> s2, final Instantiator<Void, ?> builderInstantiator, final BuilderInstantiatorDefinition instantiatorDefinition, final Map<Parameter, BiFunction<? super S1, ? super S2, ?>> injectionsMap, boolean ignoreNullValues) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    Class<?> targetClass = TypeHelper.toClass(getTargetType(instantiatorDefinition));
    String targetType = AsmUtils.toAsmType(targetClass);
    String s1Type = AsmUtils.toWrapperType(s1);
    String s2Type = AsmUtils.toWrapperType(s2);
    String classType = AsmUtils.toAsmType(className);
    String instantiatorType = AsmUtils.toAsmType(BI_INSTANTIATOR_CLASS);
    List<InjectionPoint> injections = toInjections(injectionsMap);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + instantiatorType + "<L" + targetType + ";>;", "java/lang/Object", new String[] { instantiatorType });
    {
        FieldVisitor fv = cw.visitField(ACC_FINAL, "builderInstantiator", "L" + AsmUtils.toAsmType(INSTANTIATOR_CLASS) + ";", "L" + AsmUtils.toAsmType(INSTANTIATOR_CLASS) + "<Ljava/lang/Void;L" + AsmUtils.toAsmType(getTargetType(instantiatorDefinition.getBuilderInstantiator())) + ";>;", null);
        fv.visitEnd();
    }
    appendFunctionsField(injections, cw);
    appendInitBuilder(injections, cw, s1Type, s2Type, classType, instantiatorDefinition);
    appendNewInstanceBuilder(s1, s2, instantiatorDefinition, injections, cw, targetType, s1Type, s2Type, classType, instantiatorDefinition.getSetters(), ignoreNullValues);
    appendBridgeMethod(cw, targetType, s1Type, s2Type, classType);
    appendToString(injections, cw, instantiatorDefinition.getParameters());
    cw.visitEnd();
    return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Also used : FieldVisitor(org.simpleflatmapper.ow2asm.FieldVisitor) ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter)

Example 9 with ClassWriter

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

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

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