Search in sources :

Example 1 with PropertyMeta

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());
}
Also used : DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) Predicate(org.simpleflatmapper.util.Predicate) Tuple3(org.simpleflatmapper.tuple.Tuple3) List(java.util.List) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Example 2 with PropertyMeta

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]));
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) NullGetter(org.simpleflatmapper.reflect.getter.NullGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) BiFunction(org.simpleflatmapper.util.BiFunction) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) Parameter(org.simpleflatmapper.reflect.Parameter) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)

Example 3 with PropertyMeta

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;
    }
}
Also used : PropertyNameMatcher(org.simpleflatmapper.reflect.meta.PropertyNameMatcher) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta)

Example 4 with PropertyMeta

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;
}
Also used : Predicate(org.simpleflatmapper.util.Predicate) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) Type(java.lang.reflect.Type) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) Parameter(org.simpleflatmapper.reflect.Parameter) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta)

Example 5 with PropertyMeta

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);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) LongFieldMapper(org.simpleflatmapper.map.fieldmapper.LongFieldMapper) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Test(org.junit.Test)

Aggregations

PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)19 Test (org.junit.Test)8 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)7 Predicate (org.simpleflatmapper.util.Predicate)7 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)5 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)5 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)5 List (java.util.List)4 ArrayClassMeta (org.simpleflatmapper.reflect.meta.ArrayClassMeta)4 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)4 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)4 TypeReference (org.simpleflatmapper.util.TypeReference)3 Type (java.lang.reflect.Type)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)2 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)2 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)2 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)2 FieldMapper (org.simpleflatmapper.map.FieldMapper)2