Search in sources :

Example 1 with Instantiator

use of org.simpleflatmapper.reflect.Instantiator in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCellHandlerBuilder method createTargetSetterFactory.

public static byte[] createTargetSetterFactory(String factoryName, String className, Type target) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    String factoryType = AsmUtils.toAsmType(factoryName);
    String classType = AsmUtils.toAsmType(className);
    String targetType = AsmUtils.toAsmType(target);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, factoryType, "L" + CELL_HANDLER_FACTORY_TYPE + "<L" + targetType + ";>;", CELL_HANDLER_FACTORY_TYPE, null);
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", "(" + "L" + AsmUtils.toAsmType(Instantiator.class) + "<L" + AsmUtils.toAsmType(CsvMapperCellHandler.class) + "<L" + targetType + ";>;L" + targetType + ";>;" + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + "L" + AsmUtils.toAsmType(FieldMapperErrorHandler.class) + "<L" + AsmUtils.toAsmType(CsvColumnKey.class) + ";>;" + ")V", null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        mv.visitVarInsn(Opcodes.ALOAD, 4);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, CELL_HANDLER_FACTORY_TYPE, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(5, 5);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(DelayedCellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CellSetter[].class) + ")" + AsmUtils.toTargetTypeDeclaration(CsvMapperCellHandler.class), "(" + "[L" + DELAYED_CELL_SETTER_TYPE + "<L" + targetType + ";*>;" + "[L" + CELL_SETTER_TYPE + "<L" + targetType + ";>;" + ")" + "L" + AsmUtils.toAsmType(CsvMapperCellHandler.class) + "<L" + targetType + ";>;", null);
        mv.visitCode();
        mv.visitTypeInsn(Opcodes.NEW, classType);
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "instantiator", AsmUtils.toTargetTypeDeclaration(Instantiator.class));
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "keys", AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class));
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "parsingContextFactory", AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, AsmUtils.toAsmType(ParsingContextFactory.class), "newContext", "()" + AsmUtils.toTargetTypeDeclaration(ParsingContext.class), false);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "fieldErrorHandler", AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class));
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classType, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(DelayedCellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContext.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", false);
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(8, 3);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : CellSetter(org.simpleflatmapper.csv.mapper.CellSetter) DelayedCellSetter(org.simpleflatmapper.csv.mapper.DelayedCellSetter) ParsingContext(org.simpleflatmapper.csv.ParsingContext) DelayedCellSetter(org.simpleflatmapper.csv.mapper.DelayedCellSetter) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) ParsingContextFactory(org.simpleflatmapper.csv.ParsingContextFactory) Instantiator(org.simpleflatmapper.reflect.Instantiator) CsvMapperCellHandler(org.simpleflatmapper.csv.mapper.CsvMapperCellHandler) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) ClassWriter(org.simpleflatmapper.ow2asm.ClassWriter) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 2 with Instantiator

use of org.simpleflatmapper.reflect.Instantiator in project SimpleFlatMapper by arnaudroger.

the class AsmFactory method createBiInstantiator.

@SuppressWarnings("unchecked")
public <S1, S2, T> BiInstantiator<S1, S2, T> createBiInstantiator(final Class<?> s1, final Class<?> s2, final InstantiatorDefinition instantiatorDefinition, final Map<Parameter, BiFunction<? super S1, ? super S2, ?>> injections, boolean builderIgnoresNullValues) throws Exception {
    BiInstantiatorKey instantiatorKey = new BiInstantiatorKey(instantiatorDefinition, injections, s1, s2);
    Class<? extends BiInstantiator<?, ?, ?>> instantiator = biInstantiatorCache.get(instantiatorKey);
    Instantiator builderInstantiator = null;
    if (instantiator == null) {
        final String className = generateClassNameForBiInstantiator(instantiatorKey);
        final byte[] bytes;
        if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) {
            bytes = BiInstantiatorBuilder.createInstantiator(className, s1, s2, (ExecutableInstantiatorDefinition) instantiatorDefinition, injections);
        } else {
            builderInstantiator = createInstantiator(Void.class, ((BuilderInstantiatorDefinition) instantiatorDefinition).getBuilderInstantiator(), new HashMap<Parameter, Getter<? super Void, ?>>(), builderIgnoresNullValues);
            bytes = BiInstantiatorBuilder.createInstantiator(className, s1, s2, builderInstantiator, (BuilderInstantiatorDefinition) instantiatorDefinition, injections, builderIgnoresNullValues);
        }
        instantiator = (Class<? extends BiInstantiator<?, ?, ?>>) createClass(className, bytes, instantiatorKey.getDeclaringClass().getClassLoader());
        biInstantiatorCache.put(instantiatorKey, instantiator);
    }
    Map<String, BiFunction<? super S1, ? super S2, ?>> factoryPerName = new HashMap<String, BiFunction<? super S1, ? super S2, ?>>();
    for (Entry<Parameter, BiFunction<? super S1, ? super S2, ?>> e : injections.entrySet()) {
        factoryPerName.put(e.getKey().getName(), e.getValue());
    }
    if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) {
        return (BiInstantiator<S1, S2, T>) instantiator.getConstructor(Map.class).newInstance(factoryPerName);
    } else {
        return (BiInstantiator<S1, S2, T>) instantiator.getConstructor(Map.class, Instantiator.class).newInstance(factoryPerName, builderInstantiator);
    }
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Instantiator(org.simpleflatmapper.reflect.Instantiator) BiInstantiator(org.simpleflatmapper.reflect.BiInstantiator) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) BiInstantiator(org.simpleflatmapper.reflect.BiInstantiator)

Example 3 with Instantiator

use of org.simpleflatmapper.reflect.Instantiator in project SimpleFlatMapper by arnaudroger.

the class ArrayClassMetaTest method instantiate.

private Object instantiate(ClassMeta<?> classMeta) throws Exception {
    InstantiatorDefinition instantiatorDefinition = classMeta.getInstantiatorDefinitions().get(0);
    InstantiatorFactory instantiatorFactory = new InstantiatorFactory(null);
    Instantiator instantiator = instantiatorFactory.getInstantiator(instantiatorDefinition, (Class) Object.class, new HashMap(), false, true);
    return instantiator.newInstance(null);
}
Also used : InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) HashMap(java.util.HashMap) Instantiator(org.simpleflatmapper.reflect.Instantiator)

Aggregations

Instantiator (org.simpleflatmapper.reflect.Instantiator)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 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 ClassWriter (org.simpleflatmapper.ow2asm.ClassWriter)1 MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)1 BiInstantiator (org.simpleflatmapper.reflect.BiInstantiator)1 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)1 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)1 InstantiatorFactory (org.simpleflatmapper.reflect.InstantiatorFactory)1 Parameter (org.simpleflatmapper.reflect.Parameter)1 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)1 BiFunction (org.simpleflatmapper.util.BiFunction)1