Search in sources :

Example 11 with Predicate

use of org.simpleflatmapper.util.Predicate 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 12 with Predicate

use of org.simpleflatmapper.util.Predicate 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 13 with Predicate

use of org.simpleflatmapper.util.Predicate in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilder method mapper.

public Mapper<S, T> mapper() {
    // look for property with a default value property but no definition.
    mapperConfig.columnDefinitions().forEach(DefaultValueProperty.class, new BiConsumer<Predicate<? super K>, DefaultValueProperty>() {

        @Override
        public void accept(Predicate<? super K> predicate, DefaultValueProperty columnProperty) {
            if (propertyMappingsBuilder.hasKey(predicate)) {
                return;
            }
            if (predicate instanceof Named) {
                String name = ((Named) predicate).getName();
                GetterProperty getterProperty = new GetterProperty(new ConstantGetter<S, Object>(columnProperty.getValue()), mapperSource.source(), columnProperty.getValue().getClass());
                final FieldMapperColumnDefinition<K> columnDefinition = FieldMapperColumnDefinition.<K>identity().add(columnProperty, getterProperty);
                propertyMappingsBuilder.addPropertyIfPresent(keyFactory.newKey(name, propertyMappingsBuilder.maxIndex() + 1), columnDefinition);
            }
        }
    });
    FieldMapper<S, T>[] fields = fields();
    InstantiatorAndFieldMappers constructorFieldMappersAndInstantiator = getConstructorFieldMappersAndInstantiator();
    Mapper<S, T> mapper;
    if (isEligibleForAsmMapper()) {
        try {
            mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {

                @Override
                public MapperAsmFactory newInstance(AsmFactory asmFactory) {
                    return new MapperAsmFactory(asmFactory);
                }
            }).createMapper(getKeys(), fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator, mapperSource.source(), getTargetClass());
        } catch (Throwable e) {
            if (mapperConfig.failOnAsm()) {
                return ErrorHelper.rethrow(e);
            } else {
                mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
            }
        }
    } else {
        mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
    }
    return mapper;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) Named(org.simpleflatmapper.util.Named) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Predicate(org.simpleflatmapper.util.Predicate) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper)

Aggregations

Predicate (org.simpleflatmapper.util.Predicate)13 Test (org.junit.Test)9 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)7 ArrayList (java.util.ArrayList)3 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)3 Tuple2 (org.simpleflatmapper.tuple.Tuple2)3 StringReader (java.io.StringReader)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)2 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 ArrayClassMeta (org.simpleflatmapper.reflect.meta.ArrayClassMeta)2 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 Tuple3 (org.simpleflatmapper.tuple.Tuple3)2 TypeReference (org.simpleflatmapper.util.TypeReference)2 ResultSet (java.sql.ResultSet)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1