Search in sources :

Example 1 with Getter

use of org.simpleflatmapper.reflect.Getter 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 Getter

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

the class ConstantTargetFieldMapperFactoryImpl method newFieldMapper.

@SuppressWarnings("unchecked")
@Override
public <S, P> FieldMapper<S, T> newFieldMapper(PropertyMapping<S, P, K, FieldMapperColumnDefinition<K>> pm, MappingContextFactoryBuilder contextFactoryBuilder, MapperBuilderErrorHandler mappingErrorHandler) {
    Getter<? super S, ? extends P> getter = (Getter<? super S, ? extends P>) pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType());
    if (getter == null) {
        getter = pm.getPropertyMeta().getGetter();
    }
    if (getter == null) {
        mappingErrorHandler.accessorNotFound("Could not find getter for " + pm.getColumnKey() + " type " + pm.getPropertyMeta().getPropertyType() + " path " + pm.getPropertyMeta().getPath() + ", See " + ErrorDoc.toUrl("CTFM_GETTER_NOT_FOUND"));
        return null;
    }
    Setter<? super T, ? super P> setter = getSetterForTarget(pm);
    if (setter == null) {
        mappingErrorHandler.accessorNotFound("Could not find setter for " + pm.getColumnKey() + " type " + pm.getPropertyMeta().getPropertyType() + " path " + pm.getPropertyMeta().getPath() + " See " + ErrorDoc.toUrl("CTFM_SETTER_NOT_FOUND"));
        return null;
    }
    Type propertyType = pm.getPropertyMeta().getPropertyType();
    return buildFieldMapper(getter, setter, propertyType);
}
Also used : Type(java.lang.reflect.Type) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Getter(org.simpleflatmapper.reflect.Getter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) SetterOnGetter(org.simpleflatmapper.reflect.setter.SetterOnGetter) ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter)

Example 3 with Getter

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

the class CrudTest method testDbObjectCrudWithCustomGetter.

@Test
public void testDbObjectCrudWithCustomGetter() throws Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new GetterProperty(new Getter<ResultSet, String>() {

        @Override
        public String get(ResultSet target) throws Exception {
            return "customname";
        }
    }));
    Connection connection = DbHelper.getDbConnection(targetDB);
    if (connection == null) {
        System.err.println("Db " + targetDB + " not available");
        return;
    }
    try {
        Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection);
        TestDbObject testDbObject = DbObject.newInstance(new TestDbObject());
        objectCrud.create(connection, testDbObject);
        TestDbObject read = objectCrud.read(connection, testDbObject.getId());
        assertEquals("customname", read.getName());
        assertEquals(testDbObject.getEmail(), read.getEmail());
    } finally {
        connection.close();
    }
}
Also used : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) Getter(org.simpleflatmapper.reflect.Getter) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) Test(org.junit.Test)

Example 4 with Getter

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

the class JdbcConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ConverterFactory<?, ?>> consumer) {
    // IFJAVA8_START
    constantConverter(consumer, Time.class, LocalTime.class, new TimeToLocalTimeConverter());
    constantConverter(consumer, Date.class, LocalDate.class, new DateToLocalDateConverter());
    factoryConverter(consumer, new AbstractConverterFactory<Time, OffsetTime>(Time.class, OffsetTime.class) {

        @Override
        public Converter<Time, OffsetTime> newConverter(ConvertingTypes targetedTypes, Object... params) {
            ZoneOffset zoneOffset = getZoneOffset(params);
            return new TimeToOffsetTimeConverter(zoneOffset);
        }

        @SuppressWarnings("unchecked")
        private ZoneOffset getZoneOffset(Object[] params) {
            for (Object prop : params) {
                if (prop instanceof ZoneOffset) {
                    return (ZoneOffset) prop;
                } else if (SupplierHelper.isSupplierOf(prop, ZoneOffset.class)) {
                    return ((Supplier<ZoneOffset>) prop).get();
                }
            }
            return ZoneOffset.UTC;
        }
    });
    // IFJAVA8_END
    factoryConverter(consumer, new AbstractConverterFactory<Array, Object>(Array.class, java.lang.reflect.Array.class) {

        @Override
        public Converter<? super Array, ? extends Object> newConverter(ConvertingTypes targetedTypes, Object... params) {
            Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
            Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
            return new SqlArrayToJavaArrayConverter<Object>(TypeHelper.<Object>toClass(elementType), getter);
        }

        @Override
        public ConvertingScore score(ConvertingTypes targetedTypes) {
            return new ConvertingScore(super.score(targetedTypes).getFromScore(), TypeHelper.isArray(targetedTypes.getTo()) ? 1 : -1);
        }
    });
    factoryConverter(consumer, new AbstractConverterFactory<Array, List>(Array.class, List.class) {

        @Override
        public Converter<? super Array, ? extends List> newConverter(ConvertingTypes targetedTypes, Object... params) {
            Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
            Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
            return new SqlArrayToListConverter<Object>(getter);
        }
    });
    constantConverter(consumer, Calendar.class, Timestamp.class, new CalendarToTimestampConverter());
    constantConverter(consumer, java.util.Date.class, Timestamp.class, new UtilDateToTimestampConverter());
    constantConverter(consumer, java.util.Date.class, Time.class, new UtilDateToTimeConverter());
    constantConverter(consumer, java.util.Date.class, Date.class, new UtilDateToDateConverter());
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) ZoneOffset(java.time.ZoneOffset) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) OffsetTime(java.time.OffsetTime) ResultSet(java.sql.ResultSet) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) Converter(org.simpleflatmapper.converter.Converter) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) Supplier(org.simpleflatmapper.util.Supplier) List(java.util.List) ConvertingScore(org.simpleflatmapper.converter.ConvertingScore) Array(java.sql.Array) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter)

Example 5 with Getter

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

the class ParamNameDeductor method parameters.

private Map<Parameter, Getter<? super Object, ?>> parameters(InstantiatorDefinition instantiatorDefinition, boolean allowNull, boolean builderIgnoresNullValues) throws Exception {
    Map<Parameter, Getter<? super Object, ?>> parameterGetterMap = new HashMap<Parameter, Getter<? super Object, ?>>();
    for (Parameter parameter : instantiatorDefinition.getParameters()) {
        Object value = neutralValue(parameter.getGenericType(), allowNull, builderIgnoresNullValues);
        parameterGetterMap.put(parameter, new ConstantGetter<Object, Object>(value));
    }
    return parameterGetterMap;
}
Also used : HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) Parameter(org.simpleflatmapper.reflect.Parameter)

Aggregations

Getter (org.simpleflatmapper.reflect.Getter)20 Type (java.lang.reflect.Type)6 Test (org.junit.Test)6 Parameter (org.simpleflatmapper.reflect.Parameter)6 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)6 HashMap (java.util.HashMap)4 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ResultSet (java.sql.ResultSet)3 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)3 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Converter (org.simpleflatmapper.converter.Converter)2 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)2 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)2 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)2 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)2