Search in sources :

Example 1 with ConstantGetter

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

the class AbstractConstantTargetMapperBuilder method addColumn.

@SuppressWarnings("unchecked")
public B addColumn(K key, FieldMapperColumnDefinition<K> columnDefinition) {
    final FieldMapperColumnDefinition<K> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key));
    final K 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(), classMeta.getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null);
        propertyMappingsBuilder.addProperty(key, columnDefinition, meta);
    } else {
        propertyMappingsBuilder.addProperty(mappedColumnKey, composedDefinition);
    }
    return (B) this;
}
Also used : ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) ObjectPropertyMeta(org.simpleflatmapper.reflect.meta.ObjectPropertyMeta)

Example 2 with ConstantGetter

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

the class ParamNameDeductor method parameters.

private Map<Parameter, Getter<? super Object, ?>> parameters(InstantiatorDefinition instantiatorDefinition, boolean allowNull, boolean builderIgnoresNullValues) throws Exception {
    Map<Parameter, Getter<? super Object, ?>> parameterGetterMap = new HashMap<Parameter, Getter<? super Object, ?>>();
    for (Parameter parameter : instantiatorDefinition.getParameters()) {
        Object value = neutralValue(parameter.getGenericType(), allowNull, builderIgnoresNullValues);
        parameterGetterMap.put(parameter, new ConstantGetter<Object, Object>(value));
    }
    return parameterGetterMap;
}
Also used : HashMap(java.util.HashMap) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) Parameter(org.simpleflatmapper.reflect.Parameter)

Example 3 with ConstantGetter

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

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

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

the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethodVoid.

@Test
public void testBuilderFromMethodVoid() 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>("zrux"));
    params.put(parameters[0], new ConstantGetter<Void, Integer>(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(3, o.getId());
    assertEquals("zrux", 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