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]));
}
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);
}
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();
}
}
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());
}
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;
}
Aggregations