Search in sources :

Example 6 with InstantiatorDefinition

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

the class OptionalClassMeta method getInstantiatorDefinition.

private InstantiatorDefinition getInstantiatorDefinition(Type type) throws NoSuchMethodException {
    ParameterizedType pt = (ParameterizedType) type;
    InstantiatorDefinition id = new ExecutableInstantiatorDefinition(Optional.class.getMethod("ofNullable", Object.class), new Parameter(0, "value", Object.class, pt.getActualTypeArguments()[0]));
    return id;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) Parameter(org.simpleflatmapper.reflect.Parameter)

Example 7 with InstantiatorDefinition

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

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

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

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

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