Search in sources :

Example 1 with ConstantIntGetter

use of org.simpleflatmapper.reflect.getter.ConstantIntGetter 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 2 with ConstantIntGetter

use of org.simpleflatmapper.reflect.getter.ConstantIntGetter in project SimpleFlatMapper by arnaudroger.

the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethod.

@Test
public void testBuilderFromMethod() throws Exception {
    final List<InstantiatorDefinition> instantiatorDefinitions = BuilderInstantiatorDefinitionFactory.extractDefinitions(ClassBuilderWithMethod.class);
    assertEquals(1, instantiatorDefinitions.size());
    BuilderInstantiatorDefinition b = (BuilderInstantiatorDefinition) instantiatorDefinitions.get(0);
    assertEquals(ClassBuilderWithMethod.Builder.class.getName(), b.getName());
    // builder instantiator
    final ExecutableInstantiatorDefinition builderInstantiator = (ExecutableInstantiatorDefinition) b.getBuilderInstantiator();
    assertEquals(ClassBuilderWithMethod.class.getMethod("builder"), builderInstantiator.getExecutable());
    assertEquals(0, builderInstantiator.getParameters().length);
    final Parameter[] parameters = b.getParameters();
    assertEquals(3, parameters.length);
    Arrays.sort(parameters, new Comparator<Parameter>() {

        @Override
        public int compare(Parameter o1, Parameter o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    assertEquals("name", parameters[1].getName());
    assertEquals("id", parameters[0].getName());
    Map<Parameter, Getter<? super Void, ?>> params = new HashMap<Parameter, Getter<? super Void, ?>>();
    params.put(parameters[1], new ConstantGetter<Void, Object>("myname"));
    params.put(parameters[0], new ConstantIntGetter<Void>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final Instantiator<Void, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, ClassBuilderWithMethod>getInstantiator(b, Void.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null);
    assertFalse((instantiator instanceof BuilderInstantiator));
    assertEquals("myname", o.getName());
    assertEquals(3, o.getId());
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantIntGetter(org.simpleflatmapper.reflect.getter.ConstantIntGetter) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiator(org.simpleflatmapper.reflect.impl.BuilderInstantiator) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)2 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)2 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)2 Getter (org.simpleflatmapper.reflect.Getter)1 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)1 Parameter (org.simpleflatmapper.reflect.Parameter)1 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)1 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)1 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)1 BuilderInstantiator (org.simpleflatmapper.reflect.impl.BuilderInstantiator)1 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)1 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)1 DbObject (org.simpleflatmapper.test.beans.DbObject)1 Type (org.simpleflatmapper.test.beans.DbObject.Type)1