Search in sources :

Example 1 with CellSetter

use of org.simpleflatmapper.csv.mapper.CellSetter 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 CellSetter

use of org.simpleflatmapper.csv.mapper.CellSetter in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilder method getSetters.

@SuppressWarnings({ "unchecked", "rawtypes" })
private CellSetter<T>[] getSetters(final ParsingContextFactoryBuilder parsingContextFactoryBuilder, final int delayedSetterEnd) {
    final Map<String, CsvMapperBuilder<?>> delegateMapperBuilders = new HashMap<String, CsvMapperBuilder<?>>();
    final Map<String, Integer> propertyToMapperIndex = new HashMap<String, Integer>();
    // calculate maxIndex
    int maxIndex = propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {

        int maxIndex = delayedSetterEnd;

        @Override
        public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
            if (propMapping != null) {
                maxIndex = Math.max(propMapping.getColumnKey().getIndex(), maxIndex);
                PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
                if (prop != null) {
                    CsvColumnKey key = propMapping.getColumnKey();
                    if (prop.isConstructorProperty()) {
                        throw new IllegalStateException("Unexpected ConstructorPropertyMeta at " + key.getIndex());
                    } else if (prop.isSubProperty()) {
                        final PropertyMeta<?, ?> propOwner = ((SubPropertyMeta) prop).getOwnerProperty();
                        CsvMapperBuilder<?> delegateMapperBuilder = delegateMapperBuilders.get(propOwner.getName());
                        if (delegateMapperBuilder == null) {
                            delegateMapperBuilder = new CsvMapperBuilder(propOwner.getPropertyType(), propOwner.getPropertyClassMeta(), minDelayedSetter, cellValueReaderFactory, mapperConfig);
                            delegateMapperBuilders.put(propOwner.getName(), delegateMapperBuilder);
                        }
                        Integer currentIndex = propertyToMapperIndex.get(propOwner.getName());
                        if (currentIndex == null || currentIndex < key.getIndex()) {
                            propertyToMapperIndex.put(propOwner.getName(), key.getIndex());
                        }
                        delegateMapperBuilder.addMapping(((SubPropertyMeta) prop).getSubProperty(), key, propMapping.getColumnDefinition());
                    }
                }
            }
        }
    }, delayedSetterEnd).maxIndex;
    // builder se setters
    final CellSetter<T>[] setters = new CellSetter[maxIndex + 1 - delayedSetterEnd];
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {

        final Map<String, CsvMapperImpl<?>> mappers = new HashMap<String, CsvMapperImpl<?>>();

        final CellSetterFactory cellSetterFactory = new CellSetterFactory(cellValueReaderFactory, mapperConfig.mapperBuilderErrorHandler());

        @Override
        public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
            if (propMapping == null) {
                return;
            }
            PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
            if (prop == null || prop instanceof SelfPropertyMeta) {
                return;
            }
            if (prop instanceof SubPropertyMeta) {
                DelegateMarkerSetter<T, ?> delegateMarkerSetter = getDelegateMarkerSetter((SubPropertyMeta) prop);
                setters[propMapping.getColumnKey().getIndex() - delayedSetterEnd] = delegateMarkerSetter;
            } else {
                setters[propMapping.getColumnKey().getIndex() - delayedSetterEnd] = cellSetterFactory.getCellSetter(prop, propMapping.getColumnKey().getIndex(), propMapping.getColumnDefinition(), parsingContextFactoryBuilder);
            }
        }

        private <I, P> DelegateMarkerSetter<T, I> getDelegateMarkerSetter(SubPropertyMeta<T, I, P> prop) {
            final String propName = prop.getOwnerProperty().getName();
            CsvMapperImpl<I> mapper = (CsvMapperImpl<I>) mappers.get(propName);
            if (mapper == null) {
                CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propName);
                mapper = (CsvMapperImpl<I>) delegateMapperBuilder.mapper();
                mappers.put(propName, mapper);
            }
            int indexOfMapper = propertyToMapperIndex.get(propName);
            return new DelegateMarkerSetter<T, I>(mapper, prop.getOwnerProperty().getSetter(), indexOfMapper);
        }
    }, delayedSetterEnd);
    return setters;
}
Also used : HashMap(java.util.HashMap) ForEachCallBack(org.simpleflatmapper.util.ForEachCallBack) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) CellSetter(org.simpleflatmapper.csv.mapper.CellSetter) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) DelayedCellSetterFactory(org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping)

Aggregations

CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)2 HashMap (java.util.HashMap)1 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)1 ParsingContext (org.simpleflatmapper.csv.ParsingContext)1 ParsingContextFactory (org.simpleflatmapper.csv.ParsingContextFactory)1 CsvMapperCellHandler (org.simpleflatmapper.csv.mapper.CsvMapperCellHandler)1 DelayedCellSetter (org.simpleflatmapper.csv.mapper.DelayedCellSetter)1 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)1 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)1 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)1 ClassWriter (org.simpleflatmapper.ow2asm.ClassWriter)1 MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)1 Instantiator (org.simpleflatmapper.reflect.Instantiator)1 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)1 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)1 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)1 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)1 ForEachCallBack (org.simpleflatmapper.util.ForEachCallBack)1