Search in sources :

Example 11 with ConstantGetter

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

the class BuilderInstantiatorDefinitionFactoryTest method testBuilderFromMethodAsmBoxing.

@Test
public void testBuilderFromMethodAsmBoxing() 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[1], new ConstantGetter<Void, Object>("myname"));
    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("myname", o.getName());
    assertEquals(3, o.getId());
}
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)

Example 12 with ConstantGetter

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

the class ConstantSourceMapperBuilder method mapper.

public Mapper<S, T> mapper() {
    // look for property with a default value property but no definition.
    mapperConfig.columnDefinitions().forEach(DefaultValueProperty.class, new BiConsumer<Predicate<? super K>, DefaultValueProperty>() {

        @Override
        public void accept(Predicate<? super K> predicate, DefaultValueProperty columnProperty) {
            if (propertyMappingsBuilder.hasKey(predicate)) {
                return;
            }
            if (predicate instanceof Named) {
                String name = ((Named) predicate).getName();
                GetterProperty getterProperty = new GetterProperty(new ConstantGetter<S, Object>(columnProperty.getValue()), mapperSource.source(), columnProperty.getValue().getClass());
                final FieldMapperColumnDefinition<K> columnDefinition = FieldMapperColumnDefinition.<K>identity().add(columnProperty, getterProperty);
                propertyMappingsBuilder.addPropertyIfPresent(keyFactory.newKey(name, propertyMappingsBuilder.maxIndex() + 1), columnDefinition);
            }
        }
    });
    FieldMapper<S, T>[] fields = fields();
    InstantiatorAndFieldMappers constructorFieldMappersAndInstantiator = getConstructorFieldMappersAndInstantiator();
    Mapper<S, T> mapper;
    if (isEligibleForAsmMapper()) {
        try {
            mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {

                @Override
                public MapperAsmFactory newInstance(AsmFactory asmFactory) {
                    return new MapperAsmFactory(asmFactory);
                }
            }).createMapper(getKeys(), fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator, mapperSource.source(), getTargetClass());
        } catch (Throwable e) {
            if (mapperConfig.failOnAsm()) {
                return ErrorHelper.rethrow(e);
            } else {
                mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
            }
        }
    } else {
        mapper = new MapperImpl<S, T>(fields, constructorFieldMappersAndInstantiator.fieldMappers, constructorFieldMappersAndInstantiator.instantiator);
    }
    return mapper;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) Named(org.simpleflatmapper.util.Named) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) MapperAsmFactory(org.simpleflatmapper.map.asm.MapperAsmFactory) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Predicate(org.simpleflatmapper.util.Predicate) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper)

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