use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class AsmFactoryTest method testCreateInstantiatorFinalDbObjectInjectIdAndName.
@Test
public void testCreateInstantiatorFinalDbObjectInjectIdAndName() throws Exception {
ExecutableInstantiatorDefinition instantiatorDefinition = (ExecutableInstantiatorDefinition) AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalObject.class).get(0);
HashMap<Parameter, Getter<? super Object, ?>> injections = new HashMap<Parameter, Getter<? super Object, ?>>();
injections.put(new Parameter(0, "id", long.class), new ConstantLongGetter<Object>(33l));
injections.put(new Parameter(1, "name", String.class), new ConstantGetter<Object, String>("fdo"));
Instantiator<Object, DbFinalObject> instantiator = asmFactory.createInstantiator(Object.class, instantiatorDefinition, injections, true);
DbFinalObject fdo = instantiator.newInstance(new Object());
assertNotNull(fdo);
assertNull(fdo.getEmail());
assertNull(fdo.getCreationTime());
assertNull(fdo.getTypeName());
assertNull(fdo.getTypeOrdinal());
assertEquals(33l, fdo.getId());
assertEquals("fdo", fdo.getName());
assertSame(instantiator.getClass(), asmFactory.createInstantiator(Object.class, instantiatorDefinition, injections, true).getClass());
}
use of org.simpleflatmapper.reflect.Parameter 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.Parameter in project SimpleFlatMapper by arnaudroger.
the class BuilderBiInstantiatorDefinitionFactoryTest 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, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
params.put(parameters[1], new ConstantBiFunction<Void, Object, String>("myname"));
params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(3));
final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
assertFalse((instantiator instanceof BuilderBiInstantiator));
assertEquals("myname", o.getName());
assertEquals(3, o.getId());
}
use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoid.
@Test
public void testBuilderFromMethodVoid() 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, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
params.put(parameters[2], new ConstantBiFunction<Void, Object, String>("zrux"));
params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(3));
final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
assertFalse((instantiator instanceof BuilderBiInstantiator));
assertEquals(3, o.getId());
assertEquals("zrux", o.getZrux());
}
use of org.simpleflatmapper.reflect.Parameter in project SimpleFlatMapper by arnaudroger.
the class BuilderBiInstantiatorDefinitionFactoryTest 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, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
params.put(parameters[2], new ConstantBiFunction<Void, Object, String>(null));
params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(null));
final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
assertFalse((instantiator instanceof BuilderBiInstantiator));
assertEquals(0, o.getId());
assertEquals(null, o.getZrux());
}
Aggregations