Search in sources :

Example 1 with FieldMapper

use of org.simpleflatmapper.map.FieldMapper in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilder method constructorInjections.

@SuppressWarnings("unchecked")
private ConstructorInjections constructorInjections() {
    final Map<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>> injections = new HashMap<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>>();
    final List<FieldMapper<S, T>> fieldMappers = new ArrayList<FieldMapper<S, T>>();
    propertyMappingsBuilder.forEachConstructorProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @SuppressWarnings("unchecked")
        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> propertyMapping) {
            if (!isTargetForMapperFieldMapper(propertyMapping)) {
                PropertyMeta<T, ?> pm = propertyMapping.getPropertyMeta();
                ConstructorPropertyMeta<T, ?> cProp = (ConstructorPropertyMeta<T, ?>) pm;
                Parameter parameter = cProp.getParameter();
                Getter<? super S, ?> getter = fieldMapperFactory.getGetterFromSource(propertyMapping.getColumnKey(), pm.getPropertyType(), propertyMapping.getColumnDefinition(), pm.getPropertyClassMetaSupplier());
                if (NullGetter.isNull(getter)) {
                    mapperConfig.mapperBuilderErrorHandler().accessorNotFound("Could not find getter for " + propertyMapping.getColumnKey() + " type " + propertyMapping.getPropertyMeta().getPropertyType() + " path " + propertyMapping.getPropertyMeta().getPath() + " See " + ErrorDoc.toUrl("FMMB_GETTER_NOT_FOUND"));
                } else {
                    injections.put(parameter, new BiFunctionGetter<S, MappingContext<? super S>, Object>(getter));
                }
                if (!NullSetter.isNull(cProp.getSetter())) {
                    fieldMappers.add(fieldMapperFactory.newFieldMapper(propertyMapping, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler()));
                }
            }
        }
    });
    for (PropertyPerOwner e : getSubPropertyPerOwner()) {
        if (e.owner.isConstructorProperty()) {
            final List<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>> properties = e.propertyMappings;
            final MappingContextFactoryBuilder currentBuilder = getMapperContextFactoryBuilder(e.owner, properties);
            final Mapper<S, ?> mapper;
            if (properties.size() == 1 && JoinUtils.isArrayElement(properties.get(0).getPropertyMeta())) {
                mapper = getterPropertyMapper(e.owner, properties.get(0));
            } else {
                mapper = subPropertyMapper(e.owner, properties, currentBuilder);
            }
            ConstructorPropertyMeta<T, ?> meta = (ConstructorPropertyMeta<T, ?>) e.owner;
            injections.put(meta.getParameter(), newMapperGetterAdapter(mapper, currentBuilder));
            fieldMappers.add(newMapperFieldMapper(properties, meta, mapper, currentBuilder));
        }
    }
    return new ConstructorInjections(injections, fieldMappers.toArray(new FieldMapper[0]));
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) NullGetter(org.simpleflatmapper.reflect.getter.NullGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) BiFunction(org.simpleflatmapper.util.BiFunction) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) Parameter(org.simpleflatmapper.reflect.Parameter) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)

Example 2 with FieldMapper

use of org.simpleflatmapper.map.FieldMapper in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperFactoryTest method testFieldErrorHandling.

@Test
public void testFieldErrorHandling() throws SQLException, Exception, ParseException {
    @SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
    final Exception exception = new Exception("Error!");
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {

        @Override
        public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws Exception {
            throw exception;
        }
    }).newBuilder(DbObject.class).addMapping("id").mapper();
    List<DbObject> list = mapper.forEach(new MockDbObjectResultSet(1), new ListCollector<DbObject>()).getList();
    assertNotNull(list.get(0));
    verify(fieldMapperErrorHandler).errorMappingField(eq(new JdbcColumnKey("id", 1)), any(), same(list.get(0)), same(exception));
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) MappingContext(org.simpleflatmapper.map.MappingContext) ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) FieldMapper(org.simpleflatmapper.map.FieldMapper) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 3 with FieldMapper

use of org.simpleflatmapper.map.FieldMapper 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 4 with FieldMapper

use of org.simpleflatmapper.map.FieldMapper in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilder method fields.

@SuppressWarnings("unchecked")
private FieldMapper<S, T>[] fields() {
    final List<FieldMapper<S, T>> fields = new ArrayList<FieldMapper<S, T>>();
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> t) {
            if (t == null || isTargetForMapperFieldMapper(t))
                return;
            PropertyMeta<T, ?> meta = t.getPropertyMeta();
            if (meta == null || (meta instanceof SelfPropertyMeta))
                return;
            if (!meta.isConstructorProperty() && !isTargetForMapperFieldMapper(t)) {
                fields.add(newFieldMapper(t));
            }
        }
    });
    for (PropertyPerOwner e : getSubPropertyPerOwner()) {
        if (!e.owner.isConstructorProperty()) {
            final MappingContextFactoryBuilder currentBuilder = getMapperContextFactoryBuilder(e.owner, e.propertyMappings);
            final Mapper<S, ?> mapper;
            if (e.propertyMappings.size() == 1 && JoinUtils.isArrayElement(e.propertyMappings.get(0).getPropertyMeta())) {
                mapper = getterPropertyMapper(e.owner, e.propertyMappings.get(0));
            } else {
                mapper = subPropertyMapper(e.owner, e.propertyMappings, currentBuilder);
            }
            fields.add(newMapperFieldMapper(e.propertyMappings, e.owner, mapper, currentBuilder));
        }
    }
    for (FieldMapper<S, T> mapper : additionalMappers) {
        fields.add(mapper);
    }
    return fields.toArray(new FieldMapper[0]);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Example 5 with FieldMapper

use of org.simpleflatmapper.map.FieldMapper 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)5 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)4 MappingContext (org.simpleflatmapper.map.MappingContext)3 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)3 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)3 MapperAsmFactory (org.simpleflatmapper.map.asm.MapperAsmFactory)2 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)2 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)2 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)2 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)2 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)2 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)1 GetterProperty (org.simpleflatmapper.map.property.GetterProperty)1 Getter (org.simpleflatmapper.reflect.Getter)1 Parameter (org.simpleflatmapper.reflect.Parameter)1