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());
}
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));
}
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;
}
Aggregations