Search in sources :

Example 6 with BiFunction

use of org.simpleflatmapper.util.BiFunction in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorFactoryTest method testInstantiateConstructorWithArgsAllPr.

@Test
public void testInstantiateConstructorWithArgsAllPr() throws Exception {
    BiInstantiator<Object, Object, DbFinalPrimitiveObject> instantiator = DISABLE_ASM.getBiInstantiator(DbFinalPrimitiveObject.class, Object.class, Object.class, AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalPrimitiveObject.class), new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>(), true, true);
    DbFinalPrimitiveObject object = instantiator.newInstance(null, null);
    assertNotNull(object);
}
Also used : DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Example 7 with BiFunction

use of org.simpleflatmapper.util.BiFunction in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorFactoryTest method testInstantiateConstructorWithArgsAllPrAsm.

@Test
public void testInstantiateConstructorWithArgsAllPrAsm() throws Exception {
    BiInstantiator<Object, Object, DbFinalPrimitiveObject> instantiator = ASM.getBiInstantiator(DbFinalPrimitiveObject.class, Object.class, Object.class, AsmInstantiatorDefinitionFactory.extractDefinitions(DbFinalPrimitiveObject.class), new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>(), true, true);
    DbFinalPrimitiveObject object = instantiator.newInstance(null, null);
    assertNotNull(object);
}
Also used : DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Example 8 with BiFunction

use of org.simpleflatmapper.util.BiFunction in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorFactoryTest method testBiInstantiatorOnDifferentGetter.

@Test
public void testBiInstantiatorOnDifferentGetter() throws Exception {
    Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections1 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
    Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections2 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
    injections1.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str1")));
    injections1.put(new Parameter(1, "val1", int.class), new BiFunctionGetter<Object, Object, Integer>(new ConstantGetter<Object, Integer>(12)));
    injections2.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str2")));
    injections2.put(new Parameter(1, "val1", int.class), new BiFunctionGetter<Object, Object, Integer>(new MyConstantGetter()));
    List<InstantiatorDefinition> constructors = ReflectionService.newInstance().extractInstantiator(ClassExample.class);
    final BiInstantiator<Object, Object, ClassExample> instantiator1 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections1, true, true);
    final BiInstantiator<Object, Object, ClassExample> instantiator2 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections2, true, true);
    ClassExample c1 = instantiator1.newInstance(null, null);
    assertEquals("str1", c1.getStr());
    assertEquals(12, c1.getVal1());
    ClassExample c2 = instantiator2.newInstance(null, null);
    assertEquals("str2", c2.getStr());
    assertEquals(13, c2.getVal1());
}
Also used : HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Example 9 with BiFunction

use of org.simpleflatmapper.util.BiFunction in project SimpleFlatMapper by arnaudroger.

the class BiInstantiatorFactoryTest method testBiInstantiatorFailOnParameterMissMatch.

@Test
public void testBiInstantiatorFailOnParameterMissMatch() throws Exception {
    Map<Parameter, BiFunction<? super Object, ? super Object, ?>> injections1 = new HashMap<Parameter, BiFunction<? super Object, ? super Object, ?>>();
    injections1.put(new Parameter(0, "str", String.class), new BiFunctionGetter<Object, Object, String>(new ConstantGetter<Object, String>("str1")));
    injections1.put(new Parameter(1, "val2", int.class), new BiFunctionGetter<Object, Object, Integer>(new ConstantGetter<Object, Integer>(12)));
    List<InstantiatorDefinition> constructors = ReflectionService.newInstance().extractInstantiator(ClassExample.class);
    try {
        final BiInstantiator<Object, Object, ClassExample> instantiator1 = ASM.getBiInstantiator(ClassExample.class, Object.class, Object.class, constructors, injections1, true, true);
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) DbFinalPrimitiveObject(org.simpleflatmapper.test.beans.DbFinalPrimitiveObject) Test(org.junit.Test)

Example 10 with BiFunction

use of org.simpleflatmapper.util.BiFunction in project SimpleFlatMapper by arnaudroger.

the class BuilderBiInstantiatorDefinitionFactoryTest method testBuilderFromMethodNoAsmNullHandling.

@Test
public void testBuilderFromMethodNoAsmNullHandling() 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, false, 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)

Aggregations

Parameter (org.simpleflatmapper.reflect.Parameter)16 BiFunction (org.simpleflatmapper.util.BiFunction)16 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)11 BuilderInstantiatorDefinition (org.simpleflatmapper.reflect.BuilderInstantiatorDefinition)10 InstantiatorFactory (org.simpleflatmapper.reflect.InstantiatorFactory)10 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)10 ConstantBiFunction (org.simpleflatmapper.util.ConstantBiFunction)9 BuilderBiInstantiator (org.simpleflatmapper.reflect.impl.BuilderBiInstantiator)6 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)4 DbFinalPrimitiveObject (org.simpleflatmapper.test.beans.DbFinalPrimitiveObject)4 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)3 MappingContext (org.simpleflatmapper.map.MappingContext)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 FieldMapper (org.simpleflatmapper.map.FieldMapper)1 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)1 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1