Search in sources :

Example 16 with InstantiatorDefinition

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

Example 17 with InstantiatorDefinition

use of org.simpleflatmapper.reflect.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class JoolTupleTest method testFindPropertyNoAsmJool.

@Test
public void testFindPropertyNoAsmJool() {
    Type type = new TypeReference<org.jooq.lambda.tuple.Tuple2<String, String>>() {
    }.getType();
    ClassMeta<org.jooq.lambda.tuple.Tuple2<String, String>> classMeta = ReflectionService.disableAsm().getClassMeta(type);
    InstantiatorDefinition instantiatorDefinition = classMeta.getInstantiatorDefinitions().get(0);
    assertEquals("v1", instantiatorDefinition.getParameters()[0].getName());
    assertEquals("v2", instantiatorDefinition.getParameters()[1].getName());
    assertEquals(2, instantiatorDefinition.getParameters().length);
}
Also used : InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) Type(java.lang.reflect.Type) Tuple2(org.jooq.lambda.tuple.Tuple2) Test(org.junit.Test)

Example 18 with InstantiatorDefinition

use of org.simpleflatmapper.reflect.InstantiatorDefinition 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 19 with InstantiatorDefinition

use of org.simpleflatmapper.reflect.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class OptionalClassMetaTest method testInstantiatorDefinition.

@Test
public void testInstantiatorDefinition() throws Exception {
    List<InstantiatorDefinition> instantiatorDefinitions = objectClassMeta.getInstantiatorDefinitions();
    assertEquals(1, instantiatorDefinitions.size());
    InstantiatorDefinition id = instantiatorDefinitions.get(0);
    assertEquals(1, id.getParameters().length);
    assertEquals("value", id.getParameters()[0].getName());
}
Also used : InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) Test(org.junit.Test)

Example 20 with InstantiatorDefinition

use of org.simpleflatmapper.reflect.InstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorFactoryTest method testBiInstantiatorOnDifferentGetter.

@Test
public void testBiInstantiatorOnDifferentGetter() throws Exception {
    Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections1 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
    Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections2 = 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, "val1", int.class), new BiFunctionGetter<Object, Object, Integer>(new ConstantGetter<Object, Integer>(12)));
    injections2.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str2")));
    injections2.put(new Parameter(1, "val1", int.class), new BiFunctionGetter<Object, Object, Integer>(new MyConstantGetter()));
    List<InstantiatorDefinition> constructors = ReflectionService.newInstance().extractInstantiator(ClassExample.class);
    final BiInstantiator<Object, Object, ClassExample> instantiator1 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections1, true, true);
    final BiInstantiator<Object, Object, ClassExample> instantiator2 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections2, true, true);
    ClassExample c1 = instantiator1.newInstance(null, null);
    assertEquals("str1", c1.getStr());
    assertEquals(12, c1.getVal1());
    ClassExample c2 = instantiator2.newInstance(null, null);
    assertEquals("str2", c2.getStr());
    assertEquals(13, c2.getVal1());
}
Also used : HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Aggregations

InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)32 Test (org.junit.Test)20 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)20 Parameter (org.simpleflatmapper.reflect.Parameter)17 HashMap (java.util.HashMap)13 BiFunction (org.simpleflatmapper.util.BiFunction)11 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)10 InstantiatorFactory (org.simpleflatmapper.reflect.InstantiatorFactory)10 ConstantBiFunction (org.simpleflatmapper.util.ConstantBiFunction)9 BuilderBiInstantiator (org.simpleflatmapper.reflect.impl.BuilderBiInstantiator)6 Type (java.lang.reflect.Type)4 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)4 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)3 InstantiatorDefinitions (org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions)3 ArrayList (java.util.ArrayList)2 DbFinalPrimitiveObject (org.simpleflatmapper.test.beans.DbFinalPrimitiveObject)2 Tuple2 (org.simpleflatmapper.tuple.Tuple2)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 java.lang.reflect (java.lang.reflect)1