Search in sources :

Example 26 with InstantiatorDefinition

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

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodNoAsm.

@Test
public void testBuilderFromMethodNoAsm() 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(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("myname", o.getName());
    assertEquals(3, o.getId());
}
Also used : HashMap(java.util.HashMap) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) 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 27 with InstantiatorDefinition

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

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoidNoAsm.

@Test
public void testBuilderFromMethodVoidNoAsm() 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(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(3, o.getId());
    assertEquals("zrux", o.getZrux());
}
Also used : HashMap(java.util.HashMap) 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 28 with InstantiatorDefinition

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

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodNullHandling.

@Test
public void testBuilderFromMethodNullHandling() 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, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertEquals(null, o.getName());
    assertEquals(0, o.getId());
}
Also used : HashMap(java.util.HashMap) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) 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 29 with InstantiatorDefinition

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

the class InstantiatorDefinitionTest method testLookForCompatibleOneArgumentIgnoreNegativeScore.

@Test
public void testLookForCompatibleOneArgumentIgnoreNegativeScore() {
    InstantiatorDefinition id1 = new ExecutableInstantiatorDefinition(c, parameter);
    InstantiatorDefinition sid = InstantiatorDefinitions.lookForCompatibleOneArgument(Arrays.asList(id1), new InstantiatorDefinitions.CompatibilityScorer() {

        @Override
        public int score(InstantiatorDefinition id) {
            return -1;
        }
    });
    assertNull(sid);
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinitions(org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions) Test(org.junit.Test)

Example 30 with InstantiatorDefinition

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

the class InstantiatorDefinitionTest method testLookForCompatibleOneArgumentReturnBestScore.

@Test
public void testLookForCompatibleOneArgumentReturnBestScore() {
    InstantiatorDefinition id1 = new ExecutableInstantiatorDefinition(c, parameter);
    InstantiatorDefinition id2 = new ExecutableInstantiatorDefinition(c, parameter);
    InstantiatorDefinition sid = InstantiatorDefinitions.lookForCompatibleOneArgument(Arrays.asList(id1, id2), new InstantiatorDefinitions.CompatibilityScorer() {

        int i;

        @Override
        public int score(InstantiatorDefinition id) {
            return i++;
        }
    });
    assertSame(sid, id2);
    sid = InstantiatorDefinitions.lookForCompatibleOneArgument(Arrays.asList(id1, id2), new InstantiatorDefinitions.CompatibilityScorer() {

        int i = 10;

        @Override
        public int score(InstantiatorDefinition id) {
            return i--;
        }
    });
    assertSame(sid, id1);
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinitions(org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions) 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