use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceMapperBuilderTest method testIssue495Simple.
@Test
public void testIssue495Simple() throws Exception {
TypeReference<List<Tuple3<Foo, Foo, Foo>>> typeReference = new TypeReference<List<Tuple3<Foo, Foo, Foo>>>() {
};
ClassMeta<List<Tuple3<Foo, Foo, Foo>>> classMeta = this.classMeta.getReflectionService().getClassMeta(typeReference.getType());
Predicate<PropertyMeta<?, ?>> propertyFilter = new Predicate<PropertyMeta<?, ?>>() {
@Override
public boolean test(PropertyMeta<?, ?> propertyMeta) {
return true;
}
};
PropertyFinder<?> propertyFinder = classMeta.newPropertyFinder(propertyFilter);
PropertyMeta<?, ?> p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("bar", 0, false, false), new Object[0]);
PropertyMeta<?, ?> p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("bar", 0, false, false), new Object[0]);
assertEquals("[0].element0.bar", p1.getPath());
assertEquals("[0].element1.bar", p2.getPath());
propertyFinder = classMeta.newPropertyFinder(propertyFilter);
p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("elt0_elt0_bar", 0, false, false), new Object[0]);
p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("elt1_elt0_bar", 0, false, false), new Object[0]);
assertEquals("[0].element0.bar", p1.getPath());
assertEquals("[1].element0.bar", p2.getPath());
}
use of org.simpleflatmapper.reflect.meta.PropertyMeta 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.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.
the class PropertyMappingsBuilder method _addProperty.
@SuppressWarnings("unchecked")
private <P> PropertyMapping<T, P, K, D> _addProperty(final K key, final D columnDefinition, Consumer<? super K> propertyNotFound) {
if (!modifiable)
throw new IllegalStateException("Builder not modifiable");
if (columnDefinition.ignore()) {
propertyMappingsBuilderProbe.ignore(key, columnDefinition);
properties.add(null);
return null;
}
PropertyFinder<T> effectivePropertyFinder = wrapPropertyFinder(this.propertyFinder);
PropertyNameMatcher propertyNameMatcher = propertyNameMatcherFactory.newInstance(key);
final PropertyMeta<T, P> prop = (PropertyMeta<T, P>) effectivePropertyFinder.findProperty(propertyNameMatcher, columnDefinition.properties(), propertyMappingsBuilderProbe.propertyFinderProbe(propertyNameMatcher));
if (prop == null) {
propertyNotFound.accept(key);
properties.add(null);
return null;
} else {
PropertyMapping<T, P, K, D> propertyMapping = addProperty(key, columnDefinition, prop);
propertyMappingsBuilderProbe.map(key, columnDefinition, prop);
handleSelfPropertyMetaInvalidation(propertyNotFound);
return propertyMapping;
}
}
use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.
the class CellSetterFactory method cellValueReaderFromFactory.
private <P> CellValueReader<P> cellValueReaderFromFactory(PropertyMeta<?, ?> pm, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder parsingContextFactoryBuilder) {
Type propertyType = pm.getPropertyType();
CellValueReader<P> reader = null;
if (columnDefinition.hasCustomReaderFactory()) {
CellValueReaderFactory factory = columnDefinition.getCustomCellValueReaderFactory();
reader = factory.getReader(propertyType, index, columnDefinition, parsingContextFactoryBuilder);
}
if (reader == null) {
reader = cellValueReaderFactory.getReader(propertyType, index, columnDefinition, parsingContextFactoryBuilder);
}
if (reader == null) {
if (!pm.isSelf()) {
final ClassMeta<?> classMeta = pm.getPropertyClassMeta();
InstantiatorDefinition id = InstantiatorDefinitions.lookForCompatibleOneArgument(classMeta.getInstantiatorDefinitions(), COMPATIBILITY_SCORER);
if (id != null) {
final Parameter parameter = id.getParameters()[0];
// look for constructor property matching name
final PropertyMeta<?, Object> property = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {
@Override
public boolean test(PropertyMeta<?, ?> propertyMeta) {
return propertyMeta.isConstructorProperty() || propertyMeta.isSubProperty() && ((SubPropertyMeta) propertyMeta).getOwnerProperty().isConstructorProperty();
}
}).findProperty(DefaultPropertyNameMatcher.exact(parameter.getName()), columnDefinition.properties());
reader = cellValueReaderFromFactory(property, index, columnDefinition, parsingContextFactoryBuilder);
if (reader != null) {
Instantiator<P, P> instantiator = classMeta.getReflectionService().getInstantiatorFactory().getOneArgIdentityInstantiator(id, classMeta.getReflectionService().builderIgnoresNullValues());
return new InstantiatorOnReader<P, P>(instantiator, reader);
}
}
}
}
return reader;
}
use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.
the class ResultSetFieldMapperFactoryTest method testPrimitiveField.
@Test
public void testPrimitiveField() {
ClassMeta<DbObject> classMeta = ReflectionService.newInstance(false).getClassMeta(DbObject.class);
PropertyMeta<DbObject, Long> id = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).<Long>findProperty(new DefaultPropertyNameMatcher("id", 0, false, false), new Object[0]);
FieldMapperColumnDefinition<JdbcColumnKey> identity = FieldMapperColumnDefinition.identity();
PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 1), identity);
FieldMapper<ResultSet, DbObject> fieldMapper = factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
assertTrue(fieldMapper instanceof LongFieldMapper);
PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping1 = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 0), identity);
fieldMapper = factory.newFieldMapper(propertyMapping1, null, RethrowMapperBuilderErrorHandler.INSTANCE);
assertTrue(fieldMapper instanceof LongFieldMapper);
}
Aggregations