Search in sources :

Example 1 with SelfPropertyMeta

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;
}
Also used : HashMap(java.util.HashMap) ForEachCallBack(org.simpleflatmapper.util.ForEachCallBack) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) CellSetter(org.simpleflatmapper.csv.mapper.CellSetter) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) DelayedCellSetterFactory(org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping)

Example 2 with SelfPropertyMeta

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());
}
Also used : IdentityGetter(org.simpleflatmapper.reflect.getter.IdentityGetter) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) Test(org.junit.Test)

Example 3 with SelfPropertyMeta

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]);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Aggregations

SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)3 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)2 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)2 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)2 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)1 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)1 FieldMapper (org.simpleflatmapper.map.FieldMapper)1 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)1 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)1 IdentityGetter (org.simpleflatmapper.reflect.getter.IdentityGetter)1 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)1 ForEachCallBack (org.simpleflatmapper.util.ForEachCallBack)1