Search in sources :

Example 16 with PropertyMeta

use of org.simpleflatmapper.reflect.meta.PropertyMeta 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());
}
Also used : Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 17 with PropertyMeta

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

the class ArrayClassMetaTest method testArrayPrimitive.

private void testArrayPrimitive(Class<?> component, Object value) throws Exception {
    assertTrue(component.isPrimitive());
    Object array = Array.newInstance(component, 10);
    ClassMeta<?> classMeta = ReflectionService.newInstance().getClassMeta(array.getClass());
    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]);
    p.getSetter().set(array, value);
    assertEquals(value, p.getGetter().get(array));
    assertEquals(value, Array.get(array, 3));
}
Also used : ArrayClassMeta(org.simpleflatmapper.reflect.meta.ArrayClassMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate)

Example 18 with PropertyMeta

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

the class ArrayClassMetaTest method testGetterSetterOnList.

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

Example 19 with PropertyMeta

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

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