Search in sources :

Example 6 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class JoinJdbcMapperCustomKeyTest method test.

@Test
public void test() throws SQLException {
    JdbcMapper<Person> mapper = JdbcMapperFactory.newInstance().addColumnDefinition(new Predicate<JdbcColumnKey>() {

        @Override
        public boolean test(JdbcColumnKey jdbcColumnKey) {
            return jdbcColumnKey.getIndex() <= 2;
        }
    }, FieldMapperColumnDefinition.<JdbcColumnKey>key(new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return propertyMeta.getPath().startsWith("key.");
        }
    })).newBuilder(Person.class).addMapping("key_tag").addMapping("key_n").addMapping("name").addMapping("phones_value").mapper();
    ResultSet rs = JoinJdbcMapperOnTupleTest.setUpResultSetMock(new String[] { "key_tag", "key_n", "name", "phones_value" }, new Object[][] { { "t1", 1, "t11", "p111" }, { "t1", 1, "t11", "p112" }, { "t1", 2, "t12", "p121" } });
    final List<Person> list = mapper.forEach(rs, new ListCollector<Person>()).getList();
    assertEquals(2, list.size());
    assertEquals("t1", list.get(0).key.tag);
    assertEquals(1, list.get(0).key.n);
    assertEquals("t11", list.get(0).name);
    assertEquals(Arrays.asList("p111", "p112"), list.get(0).phones);
    assertEquals("t1", list.get(1).key.tag);
    assertEquals(2, list.get(1).key.n);
    assertEquals("t12", list.get(1).name);
    assertEquals(Arrays.asList("p121"), list.get(1).phones);
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ListCollector(org.simpleflatmapper.util.ListCollector) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 7 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class ArrayClassMetaTest method testSet.

private Object testSet(TypeReference<?> typeReference) throws Exception {
    ClassMeta<?> classMeta = ReflectionService.newInstance().getClassMeta(typeReference.getType());
    assertTrue(classMeta instanceof ArrayClassMeta);
    PropertyMeta p = findProperty(classMeta);
    Set<String> list = (Set<String>) instantiate(classMeta);
    p.getSetter().set(list, "aaa");
    assertEquals(null, p.getGetter().get(list));
    assertEquals("aaa", list.iterator().next());
    return list;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ArrayClassMeta(org.simpleflatmapper.reflect.meta.ArrayClassMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta)

Example 8 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta in project SimpleFlatMapper by arnaudroger.

the class ArrayClassMetaTest method testArrayObject.

@Test
public void testArrayObject() throws Exception {
    ClassMeta<?> classMeta = ReflectionService.newInstance().getClassMeta(Object[].class);
    assertTrue(classMeta instanceof ArrayClassMeta);
    PropertyMeta p = classMeta.newPropertyFinder(new Predicate() {

        @Override
        public boolean test(Object propertyMeta) {
            return true;
        }
    }).findProperty(DefaultPropertyNameMatcher.of("3"), new Object[0]);
    Object[] array = new Object[10];
    p.getSetter().set(array, "aaa");
    assertEquals("aaa", p.getGetter().get(array));
    assertEquals("aaa", array[3]);
}
Also used : ArrayClassMeta(org.simpleflatmapper.reflect.meta.ArrayClassMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 9 with PropertyMeta

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

Example 10 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta 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)

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