use of org.simpleflatmapper.reflect.InstantiatorDefinition 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.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.
the class TupleClassMeta method getInstantiatorDefinition.
private InstantiatorDefinition getInstantiatorDefinition(Type type, ReflectionService reflectionService) throws java.io.IOException {
final List<InstantiatorDefinition> definitions = reflectionService.extractInstantiator(type);
ListIterator<InstantiatorDefinition> iterator = definitions.listIterator();
while (iterator.hasNext()) {
final InstantiatorDefinition definition = iterator.next();
if (isTupleConstructor(type, definition)) {
return respecifyParameterNames((ExecutableInstantiatorDefinition) definition);
}
}
throw new ConstructorNotFoundException("Cannot find eligible tuple constructor definition for " + type);
}
use of org.simpleflatmapper.reflect.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.
the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodNoAsmNullHandling.
@Test
public void testBuilderFromMethodNoAsmNullHandling() 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>(null));
params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(null));
final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(null);
final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, false, true);
final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
assertEquals(null, o.getName());
assertEquals(0, o.getId());
}
use of org.simpleflatmapper.reflect.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.
the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoidNoAsmNullHandling.
@Test
public void testBuilderFromMethodVoidNoAsmNullHandling() 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(null, 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);
assertTrue((instantiator instanceof BuilderBiInstantiator));
assertEquals(0, o.getId());
assertEquals(null, o.getZrux());
}
use of org.simpleflatmapper.reflect.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.
the class BuilderBiInstantiatorDefinitionFactoryTest 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, 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());
}
Aggregations