Search in sources :

Example 6 with ExecutableInstantiatorDefinition

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

the class ReflectionInstantiatorDefinitionFactoryTest method testExtractStaticFactoryMethod.

@Test
public void testExtractStaticFactoryMethod() throws NoSuchMethodException {
    List<InstantiatorDefinition> factoryMethod = ReflectionInstantiatorDefinitionFactory.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);
    Assert.assertEquals(new Parameter(0, null, 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 7 with ExecutableInstantiatorDefinition

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

the class AsmFactoryTest method testCreateInstantiatorFinalDbObjectInjectIdAndName.

@Test
public void testCreateInstantiatorFinalDbObjectInjectIdAndName() throws Exception {
    ExecutableInstantiatorDefinition instantiatorDefinition = (ExecutableInstantiatorDefinition) AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalObject.class).get(0);
    HashMap<Parameter, Getter<? super Object, ?>> injections = new HashMap<Parameter, Getter<? super Object, ?>>();
    injections.put(new Parameter(0, "id", long.class), new ConstantLongGetter<Object>(33l));
    injections.put(new Parameter(1, "name", String.class), new ConstantGetter<Object, String>("fdo"));
    Instantiator<Object, DbFinalObject> instantiator = asmFactory.createInstantiator(Object.class, instantiatorDefinition, injections, true);
    DbFinalObject fdo = instantiator.newInstance(new Object());
    assertNotNull(fdo);
    assertNull(fdo.getEmail());
    assertNull(fdo.getCreationTime());
    assertNull(fdo.getTypeName());
    assertNull(fdo.getTypeOrdinal());
    assertEquals(33l, fdo.getId());
    assertEquals("fdo", fdo.getName());
    assertSame(instantiator.getClass(), asmFactory.createInstantiator(Object.class, instantiatorDefinition, injections, true).getClass());
}
Also used : DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) ConstantLongGetter(org.simpleflatmapper.reflect.getter.ConstantLongGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) OrdinalEnumGetter(org.simpleflatmapper.reflect.getter.OrdinalEnumGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) Parameter(org.simpleflatmapper.reflect.Parameter) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Test(org.junit.Test)

Example 8 with ExecutableInstantiatorDefinition

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

the class AsmFactoryTest method testCreateInstantiatorFinalDbObjectNameAndType.

@Test
public void testCreateInstantiatorFinalDbObjectNameAndType() throws Exception {
    HashMap<Parameter, Getter<? super Object, ?>> injections = new HashMap<Parameter, Getter<? super Object, ?>>();
    ConstantIntGetter<Object> getter = new ConstantIntGetter<Object>(1);
    injections.put(new Parameter(4, "typeOrdinal", Type.class), new OrdinalEnumGetter<Object, Type>(getter, Type.class));
    injections.put(new Parameter(1, "name", String.class), new ConstantGetter<Object, String>("fdo"));
    List<InstantiatorDefinition> instantiatorDefinitions = AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalObject.class);
    Instantiator<Object, DbFinalObject> instantiator = asmFactory.createInstantiator(Object.class, (ExecutableInstantiatorDefinition) instantiatorDefinitions.get(0), injections, true);
    DbFinalObject fdo = instantiator.newInstance(new Object());
    assertNotNull(fdo);
    assertNull(fdo.getEmail());
    assertNull(fdo.getCreationTime());
    assertNull(fdo.getTypeName());
    assertEquals(0, fdo.getId());
    assertEquals("fdo", fdo.getName());
    assertEquals(Type.type2, fdo.getTypeOrdinal());
}
Also used : DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) ConstantLongGetter(org.simpleflatmapper.reflect.getter.ConstantLongGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) OrdinalEnumGetter(org.simpleflatmapper.reflect.getter.OrdinalEnumGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) Type(org.simpleflatmapper.test.beans.DbObject.Type) Parameter(org.simpleflatmapper.reflect.Parameter) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Test(org.junit.Test)

Example 9 with ExecutableInstantiatorDefinition

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

the class ListClassMetaTest method hasOneInstantiatorDefinitionWithEmptyConstructorOnImpl.

private void hasOneInstantiatorDefinitionWithEmptyConstructorOnImpl(ClassMeta<?> classMeta, Class<?> impl) throws NoSuchMethodException {
    assertTrue(classMeta instanceof ArrayClassMeta);
    final List<InstantiatorDefinition> instantiatorDefinitions = classMeta.getInstantiatorDefinitions();
    assertEquals(1, instantiatorDefinitions.size());
    final ExecutableInstantiatorDefinition instantiatorDefinition = (ExecutableInstantiatorDefinition) instantiatorDefinitions.get(0);
    assertEquals(0, instantiatorDefinition.getParameters().length);
    assertEquals(impl.getDeclaredConstructor(), instantiatorDefinition.getExecutable());
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ArrayClassMeta(org.simpleflatmapper.reflect.meta.ArrayClassMeta)

Example 10 with ExecutableInstantiatorDefinition

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

the class MapClassMetaTest method hasOneInstantiatorDefinitionWithEmptyConstructorOnImpl.

private void hasOneInstantiatorDefinitionWithEmptyConstructorOnImpl(ClassMeta<?> classMeta, Class<?> impl) throws NoSuchMethodException {
    assertTrue(classMeta instanceof MapClassMeta);
    final List<InstantiatorDefinition> instantiatorDefinitions = classMeta.getInstantiatorDefinitions();
    assertEquals(1, instantiatorDefinitions.size());
    final ExecutableInstantiatorDefinition instantiatorDefinition = (ExecutableInstantiatorDefinition) instantiatorDefinitions.get(0);
    assertEquals(0, instantiatorDefinition.getParameters().length);
    assertEquals(impl.getDeclaredConstructor(), instantiatorDefinition.getExecutable());
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)

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