use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition 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.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.
the class CsvMapperCellHandlerImpAsmInstantiatorTest method testInstantiateStringWithCharArray.
@SuppressWarnings("unchecked")
@Test
public void testInstantiateStringWithCharArray() throws Exception {
HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>> injections = new HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>>();
Parameter parameter = new Parameter(0, "arg0", char[].class);
DelayedGetter delayedGetter = new DelayedGetter(0);
injections.put(parameter, delayedGetter);
Class<CsvMapperCellHandlerImpl<String>> source = (Class) CsvMapperCellHandlerImpl.class;
Instantiator<CsvMapperCellHandlerImpl<String>, String> instantiator = factory.createInstantiator(source, new ExecutableInstantiatorDefinition(String.class.getConstructor(char[].class), parameter), injections, true);
DelayedCellSetterImpl delayedCellSetter = new DelayedCellSetterImpl(null, new CellValueReader() {
@Override
public char[] read(char[] chars, int offset, int length, ParsingContext parsingContext) {
return new char[] { 'h' };
}
});
delayedCellSetter.set(null, 0, 0, null);
CsvMapperCellHandlerImpl targetSettersImpl = new CsvMapperCellHandlerImpl(instantiator, new DelayedCellSetter[] { delayedCellSetter }, new CellSetter[] {}, null, null, null);
Assert.assertNotNull(instantiator.newInstance(targetSettersImpl));
}
use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.
the class ReflectionInstantiatorDefinitionFactoryTest method testExtractStaticFactoryMethodAsm.
@Test
public void testExtractStaticFactoryMethodAsm() throws NoSuchMethodException, IOException {
List<InstantiatorDefinition> factoryMethod = AsmInstantiatorDefinitionFactory.extractDefinitions(ObjectWithFactoryMethod.class);
assertEquals(1, factoryMethod.size());
ExecutableInstantiatorDefinition id = (ExecutableInstantiatorDefinition) factoryMethod.get(0);
assertEquals(ObjectWithFactoryMethod.class.getMethod("valueOf", String.class), id.getExecutable());
assertEquals(1, id.getParameters().length);
assertEquals(new Parameter(0, "value", String.class), id.getParameters()[0]);
}
use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition 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.instantiator.ExecutableInstantiatorDefinition 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