Search in sources :

Example 11 with ExecutableInstantiatorDefinition

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());
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiator(org.simpleflatmapper.reflect.impl.BuilderInstantiator) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) Test(org.junit.Test)

Example 12 with ExecutableInstantiatorDefinition

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));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ParsingContext(org.simpleflatmapper.csv.ParsingContext) HashMap(java.util.HashMap) DelayedCellSetterImpl(org.simpleflatmapper.csv.impl.cellreader.DelayedCellSetterImpl) Getter(org.simpleflatmapper.reflect.Getter) CellValueReader(org.simpleflatmapper.csv.CellValueReader) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 13 with ExecutableInstantiatorDefinition

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]);
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 14 with ExecutableInstantiatorDefinition

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);
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ConstructorNotFoundException(org.simpleflatmapper.reflect.ConstructorNotFoundException)

Example 15 with ExecutableInstantiatorDefinition

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());
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ConstantBiFunction(org.simpleflatmapper.util.ConstantBiFunction) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Aggregations

ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)23 Test (org.junit.Test)12 Parameter (org.simpleflatmapper.reflect.Parameter)12 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)11 HashMap (java.util.HashMap)6 InstantiatorDefinitions (org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions)4 InputStream (java.io.InputStream)3 Getter (org.simpleflatmapper.reflect.Getter)3 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)3 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)3 ArrayList (java.util.ArrayList)2 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)2 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)2 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)2 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)2 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 BiFunction (org.simpleflatmapper.util.BiFunction)2 IOException (java.io.IOException)1