Search in sources :

Example 1 with MapperAsmFactory

use of org.simpleflatmapper.map.asm.MapperAsmFactory in project SimpleFlatMapper by arnaudroger.

the class AbstractConstantTargetMapperBuilder method mapper.

@SuppressWarnings("unchecked")
public Mapper<T, S> mapper() {
    final List<FieldMapper<T, S>> mappers = new ArrayList<FieldMapper<T, S>>();
    final MappingContextFactoryBuilder mappingContextFactoryBuilder = new MappingContextFactoryBuilder(new KeySourceGetter<K, T>() {

        @Override
        public Object getValue(K key, T source) throws Exception {
            throw new UnsupportedOperationException();
        }
    });
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> pm) {
            preFieldProcess(mappers, pm);
            FieldMapper<T, S> fieldMapper = fieldAppenderFactory.newFieldMapper(pm, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler());
            mappers.add(fieldMapper);
            postFieldProcess(mappers, pm);
        }
    });
    postMapperProcess(mappers);
    Mapper<T, S> mapper;
    FieldMapper[] fields = mappers.toArray(new FieldMapper[0]);
    BiInstantiator<T, MappingContext<? super T>, S> instantiator = getInstantiator();
    if (mappers.size() < 256) {
        try {
            mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {

                @Override
                public MapperAsmFactory newInstance(AsmFactory asmFactory) {
                    return new MapperAsmFactory(asmFactory);
                }
            }).<T, S>createMapper(getKeys(), fields, new FieldMapper[0], instantiator, TypeHelper.<T>toClass(classMeta.getType()), sourceClass);
        } catch (Throwable e) {
            if (mapperConfig.failOnAsm()) {
                return ErrorHelper.rethrow(e);
            } else {
                mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
            }
        }
    } else {
        mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
    }
    return new ContextualMapper<T, S>(mapper, mappingContextFactoryBuilder.newFactory());
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ArrayList(java.util.ArrayList) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Example 2 with MapperAsmFactory

use of org.simpleflatmapper.map.asm.MapperAsmFactory in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilder method mapper.

public Mapper<S, T> mapper() {
    // look for property with a default value property but no definition.
    mapperConfig.columnDefinitions().forEach(DefaultValueProperty.class, new BiConsumer<Predicate<? super K>, DefaultValueProperty>() {

        @Override
        public void accept(Predicate<? super K> predicate, DefaultValueProperty columnProperty) {
            if (propertyMappingsBuilder.hasKey(predicate)) {
                return;
            }
            if (predicate instanceof Named) {
                String name = ((Named) predicate).getName();
                GetterProperty getterProperty = new GetterProperty(new ConstantGetter<S, Object>(columnProperty.getValue()), mapperSource.source(), columnProperty.getValue().getClass());
                final FieldMapperColumnDefinition<K> columnDefinition = FieldMapperColumnDefinition.<K>identity().add(columnProperty, getterProperty);
                propertyMappingsBuilder.addPropertyIfPresent(keyFactory.newKey(name, propertyMappingsBuilder.maxIndex() + 1), columnDefinition);
            }
        }
    });
    FieldMapper<S, T>[] fields = fields();
    InstantiatorAndFieldMappers constructorFieldMappersAndInstantiator = getConstructorFieldMappersAndInstantiator();
    Mapper<S, T> mapper;
    if (isEligibleForAsmMapper()) {
        try {
            mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {

                @Override
                public MapperAsmFactory newInstance(AsmFactory asmFactory) {
                    return new MapperAsmFactory(asmFactory);
                }
            }).createMapper(getKeys(), fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator, mapperSource.source(), getTargetClass());
        } catch (Throwable e) {
            if (mapperConfig.failOnAsm()) {
                return ErrorHelper.rethrow(e);
            } else {
                mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
            }
        }
    } else {
        mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
    }
    return mapper;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) Named(org.simpleflatmapper.util.Named) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Predicate(org.simpleflatmapper.util.Predicate) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Aggregations

FieldMapper (org.simpleflatmapper.map.FieldMapper)2 MapperAsmFactory (org.simpleflatmapper.map.asm.MapperAsmFactory)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)2 ArrayList (java.util.ArrayList)1 MappingContext (org.simpleflatmapper.map.MappingContext)1 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1 DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)1 GetterProperty (org.simpleflatmapper.map.property.GetterProperty)1 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)1 Named (org.simpleflatmapper.util.Named)1 Predicate (org.simpleflatmapper.util.Predicate)1