use of org.simpleflatmapper.reflect.getter.ConstantGetter in project SimpleFlatMapper by arnaudroger.
the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethodAsmBoxing.
@Test
public void testBuilderFromMethodAsmBoxing() throws Exception {
final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
assertEquals(1, instantiatorDefinitions.size());
BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
final Parameter[] parameters = b.getParameters();
Arrays.sort(parameters, new Comparator<Parameter>() {
@Override
public int compare(Parameter o1, Parameter o2) {
return o1.getName().compareTo(o2.getName());
}
});
Map<Parameter, Getter<? super Void, ?>> params = new HashMap<Parameter, Getter<? super Void, ?>>();
params.put(parameters[1], new ConstantGetter<Void, Object>("myname"));
params.put(parameters[0], new ConstantGetter<Void, Integer>(3));
final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
final Instantiator<Void, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, ClassBuilderWithMethod>getInstantiator(b, Void.class, params, true, true);
final ClassBuilderWithMethod o = instantiator.newInstance(null);
assertFalse((instantiator instanceof BuilderInstantiator));
assertEquals("myname", o.getName());
assertEquals(3, o.getId());
}
use of org.simpleflatmapper.reflect.getter.ConstantGetter 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