use of org.simpleflatmapper.reflect.getter.ConstantGetter in project SimpleFlatMapper by arnaudroger.
the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethod.
@Test
public void testBuilderFromMethod() throws Exception {
final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
assertEquals(1, instantiatorDefinitions.size());
BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
assertEquals(ClassBuilderWithMethod.Builder.class.getName(), b.getName());
// builder instantiator
final ExecutableInstantiatorDefinition builderInstantiator = (ExecutableInstantiatorDefinition) b.getBuilderInstantiator();
assertEquals(ClassBuilderWithMethod.class.getMethod("builder"), builderInstantiator.getExecutable());
assertEquals(0, builderInstantiator.getParameters().length);
final Parameter[] parameters = b.getParameters();
assertEquals(3, parameters.length);
Arrays.sort(parameters, new Comparator<Parameter>() {
@Override
public int compare(Parameter o1, Parameter o2) {
return o1.getName().compareTo(o2.getName());
}
});
assertEquals("name", parameters[1].getName());
assertEquals("id", parameters[0].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 ConstantIntGetter<Void>(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 SqlParameterSourceBuilder method add.
public SqlParameterSourceBuilder<T> add(JdbcColumnKey key, FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition) {
final FieldMapperColumnDefinition<JdbcColumnKey> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key));
final JdbcColumnKey mappedColumnKey = composedDefinition.rename(key);
if (composedDefinition.has(ConstantValueProperty.class)) {
ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class);
PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), builder.getClassMeta().getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null);
builder.addProperty(key, columnDefinition, meta);
} else {
builder.addProperty(mappedColumnKey, composedDefinition);
}
return this;
}
use of org.simpleflatmapper.reflect.getter.ConstantGetter in project SimpleFlatMapper by arnaudroger.
the class BiInstantiatorFactoryTest method testBiInstantiatorOnDifferentGetter.
@Test
public void testBiInstantiatorOnDifferentGetter() throws Exception {
Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections1 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections2 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
injections1.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str1")));
injections1.put(new Parameter(1, "val1", int.class), new BiFunctionGetter<Object, Object, Integer>(new ConstantGetter<Object, Integer>(12)));
injections2.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str2")));
injections2.put(new Parameter(1, "val1", int.class), new BiFunctionGetter<Object, Object, Integer>(new MyConstantGetter()));
List<InstantiatorDefinition> constructors = ReflectionService.newInstance().extractInstantiator(ClassExample.class);
final BiInstantiator<Object, Object, ClassExample> instantiator1 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections1, true, true);
final BiInstantiator<Object, Object, ClassExample> instantiator2 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections2, true, true);
ClassExample c1 = instantiator1.newInstance(null, null);
assertEquals("str1", c1.getStr());
assertEquals(12, c1.getVal1());
ClassExample c2 = instantiator2.newInstance(null, null);
assertEquals("str2", c2.getStr());
assertEquals(13, c2.getVal1());
}
use of org.simpleflatmapper.reflect.getter.ConstantGetter in project SimpleFlatMapper by arnaudroger.
the class BiInstantiatorFactoryTest method testBiInstantiatorFailOnParameterMissMatch.
@Test
public void testBiInstantiatorFailOnParameterMissMatch() throws Exception {
Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections1 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
injections1.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str1")));
injections1.put(new Parameter(1, "val2", int.class), new BiFunctionGetter<Object, Object, Integer>(new ConstantGetter<Object, Integer>(12)));
List<InstantiatorDefinition> constructors = ReflectionService.newInstance().extractInstantiator(ClassExample.class);
try {
final BiInstantiator<Object, Object, ClassExample> instantiator1 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections1, true, true);
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
use of org.simpleflatmapper.reflect.getter.ConstantGetter in project SimpleFlatMapper by arnaudroger.
the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoidNullHandling.
@Test
public void testBuilderFromMethodVoidNullHandling() 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[2], new ConstantGetter<Void, Object>(null));
params.put(parameters[0], new ConstantGetter<Void, Integer>(null));
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(0, o.getId());
assertEquals(null, o.getZrux());
}
Aggregations