use of org.simpleflatmapper.reflect.meta.SelfPropertyMeta 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.SelfPropertyMeta in project SimpleFlatMapper by arnaudroger.
the class SelfPropertyMetaTest method testDirect.
@Test
public void testDirect() {
ClassMeta<String> direct = ReflectionService.newInstance().getClassMeta(String.class);
PropertyMeta<String, Object> property = direct.newPropertyFinder(isValidPropertyMeta).findProperty(new DefaultPropertyNameMatcher("bbb", 0, true, true), new Object[0]);
assertTrue("Expect SelfPropertyMeta " + property, property instanceof SelfPropertyMeta);
assertEquals("SelfPropertyMeta{type=class java.lang.String,name=self}", property.toString());
assertTrue(property.getGetter() instanceof IdentityGetter);
assertTrue(NullSetter.isNull(property.getSetter()));
assertEquals("{this}", property.getPath());
assertEquals(String.class, direct.getType());
}
use of org.simpleflatmapper.reflect.meta.SelfPropertyMeta in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceMapperBuilder method fields.
@SuppressWarnings("unchecked")
private FieldMapper<S, T>[] fields() {
final List<FieldMapper<S, T>> fields = new ArrayList<FieldMapper<S, T>>();
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {
@Override
public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> t) {
if (t == null || isTargetForMapperFieldMapper(t))
return;
PropertyMeta<T, ?> meta = t.getPropertyMeta();
if (meta == null || (meta instanceof SelfPropertyMeta))
return;
if (!meta.isConstructorProperty() && !isTargetForMapperFieldMapper(t)) {
fields.add(newFieldMapper(t));
}
}
});
for (PropertyPerOwner e : getSubPropertyPerOwner()) {
if (!e.owner.isConstructorProperty()) {
final MappingContextFactoryBuilder currentBuilder = getMapperContextFactoryBuilder(e.owner, e.propertyMappings);
final Mapper<S, ?> mapper;
if (e.propertyMappings.size() == 1 && JoinUtils.isArrayElement(e.propertyMappings.get(0).getPropertyMeta())) {
mapper = getterPropertyMapper(e.owner, e.propertyMappings.get(0));
} else {
mapper = subPropertyMapper(e.owner, e.propertyMappings, currentBuilder);
}
fields.add(newMapperFieldMapper(e.propertyMappings, e.owner, mapper, currentBuilder));
}
}
for (FieldMapper<S, T> mapper : additionalMappers) {
fields.add(mapper);
}
return fields.toArray(new FieldMapper[0]);
}
Aggregations