Search in sources :

Example 6 with GetterProperty

use of org.simpleflatmapper.map.property.GetterProperty 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

GetterProperty (org.simpleflatmapper.map.property.GetterProperty)6 Test (org.junit.Test)4 DbObject (org.simpleflatmapper.test.beans.DbObject)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 Getter (org.simpleflatmapper.reflect.Getter)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)1 FieldMapper (org.simpleflatmapper.map.FieldMapper)1 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)1 MappingException (org.simpleflatmapper.map.MappingException)1 MapperAsmFactory (org.simpleflatmapper.map.asm.MapperAsmFactory)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1 FieldMapperColumnDefinitionProviderImpl (org.simpleflatmapper.map.mapper.FieldMapperColumnDefinitionProviderImpl)1 DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)1 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)1 SetterProperty (org.simpleflatmapper.map.property.SetterProperty)1 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)1