use of org.simpleflatmapper.reflect.meta.SubPropertyMeta 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.SubPropertyMeta in project SimpleFlatMapper by arnaudroger.
the class CsvMapperBuilder method buildDelayedSetters.
@SuppressWarnings({ "unchecked" })
private DelayedCellSetterFactory<T, ?>[] buildDelayedSetters(final ParsingContextFactoryBuilder parsingContextFactoryBuilder, int delayedSetterEnd, boolean hasKeys) {
final Map<String, CsvMapperBuilder<?>> delegateMapperBuilders = new HashMap<String, CsvMapperBuilder<?>>();
final Map<String, Integer> propertyToMapperIndex = new HashMap<String, Integer>();
final DelayedCellSetterFactory<T, ?>[] delayedSetters = new DelayedCellSetterFactory[delayedSetterEnd];
final int newMinDelayedSetter = minDelayedSetter != 0 ? minDelayedSetter : hasKeys ? delayedSetterEnd : 0;
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {
final CellSetterFactory cellSetterFactory = new CellSetterFactory(cellValueReaderFactory, mapperConfig.mapperBuilderErrorHandler());
@Override
public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
if (propMapping != null) {
PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
CsvColumnKey key = propMapping.getColumnKey();
if (prop != null) {
if (prop.isSubProperty()) {
addSubProperty(delegateMapperBuilders, prop, key, propMapping.getColumnDefinition());
} else {
delayedSetters[propMapping.getColumnKey().getIndex()] = cellSetterFactory.getDelayedCellSetter(prop, key.getIndex(), propMapping.getColumnDefinition(), parsingContextFactoryBuilder);
}
}
}
}
private <I, P> void addSubProperty(Map<String, CsvMapperBuilder<?>> delegateMapperBuilders, PropertyMeta<T, P> prop, CsvColumnKey key, CsvColumnDefinition columnDefinition) {
SubPropertyMeta<T, I, P> subPropertyMeta = (SubPropertyMeta<T, I, P>) prop;
final PropertyMeta<T, I> propOwner = subPropertyMeta.getOwnerProperty();
CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propOwner.getName());
if (delegateMapperBuilder == null) {
delegateMapperBuilder = new CsvMapperBuilder<I>(propOwner.getPropertyType(), propOwner.getPropertyClassMeta(), newMinDelayedSetter, cellValueReaderFactory, mapperConfig);
delegateMapperBuilders.put(propOwner.getName(), delegateMapperBuilder);
}
Integer currentIndex = propertyToMapperIndex.get(propOwner.getName());
if (currentIndex == null || currentIndex < key.getIndex()) {
propertyToMapperIndex.put(propOwner.getName(), key.getIndex());
}
delegateMapperBuilder.addMapping(subPropertyMeta.getSubProperty(), key, columnDefinition);
}
}, 0, delayedSetterEnd);
final Map<String, CsvMapper<?>> mappers = new HashMap<String, CsvMapper<?>>();
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {
@Override
public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
if (propMapping == null)
return;
PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
if (prop.isSubProperty()) {
addSubPropertyDelayedSetter(delegateMapperBuilders, delayedSetters, propMapping.getColumnKey().getIndex(), prop);
}
}
private <I, P> void addSubPropertyDelayedSetter(Map<String, CsvMapperBuilder<?>> delegateMapperBuilders, DelayedCellSetterFactory<T, ?>[] delayedSetters, int setterIndex, PropertyMeta<T, P> prop) {
PropertyMeta<T, I> subProp = ((SubPropertyMeta<T, I, P>) prop).getOwnerProperty();
final String propName = subProp.getName();
CsvMapper<I> mapper = (CsvMapper<I>) mappers.get(propName);
if (mapper == null) {
CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propName);
mapper = delegateMapperBuilder.mapper();
mappers.put(propName, mapper);
}
int indexOfMapper = propertyToMapperIndex.get(propName);
Setter<? super T, ? super I> setter = null;
if (!subProp.isConstructorProperty()) {
setter = subProp.getSetter();
}
delayedSetters[setterIndex] = new DelegateMarkerDelayedCellSetterFactory<T, I>(mapper, setter, setterIndex, indexOfMapper);
}
}, 0, delayedSetterEnd);
return delayedSetters;
}
use of org.simpleflatmapper.reflect.meta.SubPropertyMeta in project SimpleFlatMapper by arnaudroger.
the class CsvMapperBuilder method getSetters.
@SuppressWarnings({ "unchecked", "rawtypes" })
private CellSetter<T>[] getSetters(final ParsingContextFactoryBuilder parsingContextFactoryBuilder, final int delayedSetterEnd) {
final Map<String, CsvMapperBuilder<?>> delegateMapperBuilders = new HashMap<String, CsvMapperBuilder<?>>();
final Map<String, Integer> propertyToMapperIndex = new HashMap<String, Integer>();
// calculate maxIndex
int maxIndex = propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {
int maxIndex = delayedSetterEnd;
@Override
public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
if (propMapping != null) {
maxIndex = Math.max(propMapping.getColumnKey().getIndex(), maxIndex);
PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
if (prop != null) {
CsvColumnKey key = propMapping.getColumnKey();
if (prop.isConstructorProperty()) {
throw new IllegalStateException("Unexpected ConstructorPropertyMeta at " + key.getIndex());
} else if (prop.isSubProperty()) {
final PropertyMeta<?, ?> propOwner = ((SubPropertyMeta) prop).getOwnerProperty();
CsvMapperBuilder<?> delegateMapperBuilder = delegateMapperBuilders.get(propOwner.getName());
if (delegateMapperBuilder == null) {
delegateMapperBuilder = new CsvMapperBuilder(propOwner.getPropertyType(), propOwner.getPropertyClassMeta(), minDelayedSetter, cellValueReaderFactory, mapperConfig);
delegateMapperBuilders.put(propOwner.getName(), delegateMapperBuilder);
}
Integer currentIndex = propertyToMapperIndex.get(propOwner.getName());
if (currentIndex == null || currentIndex < key.getIndex()) {
propertyToMapperIndex.put(propOwner.getName(), key.getIndex());
}
delegateMapperBuilder.addMapping(((SubPropertyMeta) prop).getSubProperty(), key, propMapping.getColumnDefinition());
}
}
}
}
}, delayedSetterEnd).maxIndex;
// builder se setters
final CellSetter<T>[] setters = new CellSetter[maxIndex + 1 - delayedSetterEnd];
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {
final Map<String, CsvMapperImpl<?>> mappers = new HashMap<String, CsvMapperImpl<?>>();
final CellSetterFactory cellSetterFactory = new CellSetterFactory(cellValueReaderFactory, mapperConfig.mapperBuilderErrorHandler());
@Override
public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
if (propMapping == null) {
return;
}
PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
if (prop == null || prop instanceof SelfPropertyMeta) {
return;
}
if (prop instanceof SubPropertyMeta) {
DelegateMarkerSetter<T, ?> delegateMarkerSetter = getDelegateMarkerSetter((SubPropertyMeta) prop);
setters[propMapping.getColumnKey().getIndex() - delayedSetterEnd] = delegateMarkerSetter;
} else {
setters[propMapping.getColumnKey().getIndex() - delayedSetterEnd] = cellSetterFactory.getCellSetter(prop, propMapping.getColumnKey().getIndex(), propMapping.getColumnDefinition(), parsingContextFactoryBuilder);
}
}
private <I, P> DelegateMarkerSetter<T, I> getDelegateMarkerSetter(SubPropertyMeta<T, I, P> prop) {
final String propName = prop.getOwnerProperty().getName();
CsvMapperImpl<I> mapper = (CsvMapperImpl<I>) mappers.get(propName);
if (mapper == null) {
CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propName);
mapper = (CsvMapperImpl<I>) delegateMapperBuilder.mapper();
mappers.put(propName, mapper);
}
int indexOfMapper = propertyToMapperIndex.get(propName);
return new DelegateMarkerSetter<T, I>(mapper, prop.getOwnerProperty().getSetter(), indexOfMapper);
}
}, delayedSetterEnd);
return setters;
}
use of org.simpleflatmapper.reflect.meta.SubPropertyMeta in project SimpleFlatMapper by arnaudroger.
the class Issue451 method testPropertyFinderAndAppendSetter.
@Test
public void testPropertyFinderAndAppendSetter() {
ClassMeta<List<Foo>> classMeta = ReflectionService.newInstance().getClassMeta(new TypeReference<List<Foo>>() {
}.getType());
PropertyFinder<List<Foo>> finder = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {
@Override
public boolean test(PropertyMeta<?, ?> propertyMeta) {
return true;
}
});
SubPropertyMeta f = (SubPropertyMeta) finder.findProperty(DefaultPropertyNameMatcher.of("b_f"), new Object[0]);
SubPropertyMeta n = (SubPropertyMeta) finder.findProperty(DefaultPropertyNameMatcher.of("b_n"), new Object[0]);
assertEquals(AppendCollectionSetter.class, n.getOwnerProperty().getSetter().getClass());
}
use of org.simpleflatmapper.reflect.meta.SubPropertyMeta in project SimpleFlatMapper by arnaudroger.
the class SubPropertyMetaTest method testSubProperty.
@Test
@SuppressWarnings("unchecked")
public void testSubProperty() throws Exception {
ClassMeta<Db1DeepObject> classMeta = ReflectionService.newInstance().getClassMeta(Db1DeepObject.class);
PropertyMeta<Db1DeepObject, String> property = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {
@Override
public boolean test(PropertyMeta<?, ?> propertyMeta) {
return true;
}
}).findProperty(new DefaultPropertyNameMatcher("dbObject_name", 0, false, false), new Object[0]);
assertTrue(property instanceof SubPropertyMeta);
assertTrue(property.isSubProperty());
assertTrue(property.toString().startsWith("SubPropertyMeta{" + "ownerProperty=ObjectPropertyMeta{"));
SubPropertyMeta<Db1DeepObject, DbObject, String> subPropertyMeta = (SubPropertyMeta<Db1DeepObject, DbObject, String>) property;
assertEquals(String.class, subPropertyMeta.getPropertyType());
assertEquals("dbObject.name", subPropertyMeta.getPath());
Db1DeepObject object = new Db1DeepObject();
object.setDbObject(new DbObject());
subPropertyMeta.getSetter().set(object, "n1");
Getter<Db1DeepObject, String> getter = subPropertyMeta.getGetter();
assertEquals("n1", getter.get(object));
Db1DeepObject objectNull = new Db1DeepObject();
assertEquals(null, getter.get(objectNull));
assertTrue(getter.toString().startsWith("GetterOnGetter{g1="));
ClassMeta<?> meta = subPropertyMeta.getPropertyClassMeta();
assertEquals(String.class, meta.getType());
}
Aggregations