Search in sources :

Example 16 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class InstantiatorDefinitionTest method testDefaultCompatibilityScorers.

@Test
public void testDefaultCompatibilityScorers() {
    InstantiatorDefinitions.CompatibilityScorer compatibilityScorer = InstantiatorDefinitions.getCompatibilityScorer(new Object());
    assertEquals(0, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", InputStream.class))));
    assertEquals(1, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Number.class))));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinitions(org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions) InputStream(java.io.InputStream) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 17 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition 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 18 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition 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)

Example 19 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class InstantiatorDefinitionTest method testTypeAffinityCompatibilityScorers.

@Test
public void testTypeAffinityCompatibilityScorers() {
    InstantiatorDefinitions.CompatibilityScorer compatibilityScorer = InstantiatorDefinitions.getCompatibilityScorer(new TypeAffinity() {

        @Override
        public Class<?>[] getAffinities() {
            return new Class<?>[] { Date.class, URL.class };
        }
    });
    assertEquals(0, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", InputStream.class))));
    assertEquals(12, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Date.class))));
    assertEquals(11, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", URL.class))));
    assertEquals(1, compatibilityScorer.score(new ExecutableInstantiatorDefinition(null, new Parameter(0, "", Number.class))));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinitions(org.simpleflatmapper.reflect.instantiator.InstantiatorDefinitions) InputStream(java.io.InputStream) TypeAffinity(org.simpleflatmapper.reflect.TypeAffinity) Parameter(org.simpleflatmapper.reflect.Parameter) Date(java.util.Date) URL(java.net.URL) Test(org.junit.Test)

Example 20 with ExecutableInstantiatorDefinition

use of org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition in project SimpleFlatMapper by arnaudroger.

the class InstantiatorFactoryTest method testOneArgInstantiator.

@Test
public void testOneArgInstantiator() throws Exception {
    InstantiatorDefinition def = new ExecutableInstantiatorDefinition(String.class.getDeclaredMethod("valueOf", Object.class), new Parameter[] { new Parameter(0, "value", Object.class) });
    assertNotNull(def.toString());
    Instantiator<Object, String> instantiator = DISABLE_ASM.getOneArgIdentityInstantiator(def, true);
    assertEquals("12345", instantiator.newInstance(12345));
    Instantiator<Object, String> instantiatorAsm = ASM.getOneArgIdentityInstantiator(def, true);
    assertEquals("12345", instantiatorAsm.newInstance(12345));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) 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