use of org.simpleflatmapper.reflect.getter.ConstantIntGetter in project SimpleFlatMapper by arnaudroger.
the class AsmFactoryTest method testCreateInstantiatorFinalDbObjectNameAndType.
@Test
public void testCreateInstantiatorFinalDbObjectNameAndType() throws Exception {
HashMap<Parameter, Getter<? super Object, ?>> injections = new HashMap<Parameter, Getter<? super Object, ?>>();
ConstantIntGetter<Object> getter = new ConstantIntGetter<Object>(1);
injections.put(new Parameter(4, "typeOrdinal", Type.class), new OrdinalEnumGetter<Object, Type>(getter, Type.class));
injections.put(new Parameter(1, "name", String.class), new ConstantGetter<Object, String>("fdo"));
List<InstantiatorDefinition> instantiatorDefinitions = AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalObject.class);
Instantiator<Object, DbFinalObject> instantiator = asmFactory.createInstantiator(Object.class, (ExecutableInstantiatorDefinition) instantiatorDefinitions.get(0), injections, true);
DbFinalObject fdo = instantiator.newInstance(new Object());
assertNotNull(fdo);
assertNull(fdo.getEmail());
assertNull(fdo.getCreationTime());
assertNull(fdo.getTypeName());
assertEquals(0, fdo.getId());
assertEquals("fdo", fdo.getName());
assertEquals(Type.type2, fdo.getTypeOrdinal());
}
use of org.simpleflatmapper.reflect.getter.ConstantIntGetter 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());
}
Aggregations