Search in sources :

Example 6 with ConstantGetter

use of org.simpleflatmapper.reflect.getter.ConstantGetter 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)

Example 7 with ConstantGetter

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

the class SqlParameterSourceBuilder method add.

public SqlParameterSourceBuilder<T> add(JdbcColumnKey key, FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition) {
    final FieldMapperColumnDefinition<JdbcColumnKey> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key));
    final JdbcColumnKey mappedColumnKey = composedDefinition.rename(key);
    if (composedDefinition.has(ConstantValueProperty.class)) {
        ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class);
        PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), builder.getClassMeta().getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null);
        builder.addProperty(key, columnDefinition, meta);
    } else {
        builder.addProperty(mappedColumnKey, composedDefinition);
    }
    return this;
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) ObjectPropertyMeta(org.simpleflatmapper.reflect.meta.ObjectPropertyMeta)

Example 8 with ConstantGetter

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

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

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

the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoidNullHandling.

@Test
public void testBuilderFromMethodVoidNullHandling() 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, Getter<? super Void, ?>> params = new HashMap<Parameter, Getter<? super Void, ?>>();
    params.put(parameters[2], new ConstantGetter<Void, Object>(null));
    params.put(parameters[0], new ConstantGetter<Void, Integer>(null));
    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(0, o.getId());
    assertEquals(null, o.getZrux());
}
Also used : 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

ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)12 HashMap (java.util.HashMap)9 Test (org.junit.Test)8 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)6 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)6 Parameter (org.simpleflatmapper.reflect.Parameter)5 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)5 BuilderInstantiator (org.simpleflatmapper.reflect.impl.BuilderInstantiator)4 Getter (org.simpleflatmapper.reflect.Getter)3 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)3 ConstantValueProperty (org.simpleflatmapper.map.property.ConstantValueProperty)2 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)2 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)2 ObjectPropertyMeta (org.simpleflatmapper.reflect.meta.ObjectPropertyMeta)2 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)2 DbFinalPrimitiveObject (org.simpleflatmapper.test.beans.DbFinalPrimitiveObject)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 BiFunction (org.simpleflatmapper.util.BiFunction)2 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1