Search in sources :

Example 1 with BiFunction

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

the class ConstantSourceMapperBuilder method constructorInjections.

@SuppressWarnings("unchecked")
private ConstructorInjections constructorInjections() {
    final Map<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>> injections = new HashMap<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>>();
    final List<FieldMapper<S, T>> fieldMappers = new ArrayList<FieldMapper<S, T>>();
    propertyMappingsBuilder.forEachConstructorProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {

        @SuppressWarnings("unchecked")
        @Override
        public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> propertyMapping) {
            if (!isTargetForMapperFieldMapper(propertyMapping)) {
                PropertyMeta<T, ?> pm = propertyMapping.getPropertyMeta();
                ConstructorPropertyMeta<T, ?> cProp = (ConstructorPropertyMeta<T, ?>) pm;
                Parameter parameter = cProp.getParameter();
                Getter<? super S, ?> getter = fieldMapperFactory.getGetterFromSource(propertyMapping.getColumnKey(), pm.getPropertyType(), propertyMapping.getColumnDefinition(), pm.getPropertyClassMetaSupplier());
                if (NullGetter.isNull(getter)) {
                    mapperConfig.mapperBuilderErrorHandler().accessorNotFound("Could not find getter for " + propertyMapping.getColumnKey() + " type " + propertyMapping.getPropertyMeta().getPropertyType() + " path " + propertyMapping.getPropertyMeta().getPath() + " See " + ErrorDoc.toUrl("FMMB_GETTER_NOT_FOUND"));
                } else {
                    injections.put(parameter, new BiFunctionGetter<S, MappingContext<? super S>, Object>(getter));
                }
                if (!NullSetter.isNull(cProp.getSetter())) {
                    fieldMappers.add(fieldMapperFactory.newFieldMapper(propertyMapping, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler()));
                }
            }
        }
    });
    for (PropertyPerOwner e : getSubPropertyPerOwner()) {
        if (e.owner.isConstructorProperty()) {
            final List<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>> properties = e.propertyMappings;
            final MappingContextFactoryBuilder currentBuilder = getMapperContextFactoryBuilder(e.owner, properties);
            final Mapper<S, ?> mapper;
            if (properties.size() == 1 && JoinUtils.isArrayElement(properties.get(0).getPropertyMeta())) {
                mapper = getterPropertyMapper(e.owner, properties.get(0));
            } else {
                mapper = subPropertyMapper(e.owner, properties, currentBuilder);
            }
            ConstructorPropertyMeta<T, ?> meta = (ConstructorPropertyMeta<T, ?>) e.owner;
            injections.put(meta.getParameter(), newMapperGetterAdapter(mapper, currentBuilder));
            fieldMappers.add(newMapperFieldMapper(properties, meta, mapper, currentBuilder));
        }
    }
    return new ConstructorInjections(injections, fieldMappers.toArray(new FieldMapper[0]));
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) Getter(org.simpleflatmapper.reflect.Getter) NullGetter(org.simpleflatmapper.reflect.getter.NullGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) MappingContextFactoryBuilder(org.simpleflatmapper.map.context.MappingContextFactoryBuilder) MappingContext(org.simpleflatmapper.map.MappingContext) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) BiFunction(org.simpleflatmapper.util.BiFunction) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) Parameter(org.simpleflatmapper.reflect.Parameter) MapperFieldMapper(org.simpleflatmapper.map.fieldmapper.MapperFieldMapper) FieldMapper(org.simpleflatmapper.map.FieldMapper) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)

Example 2 with BiFunction

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

the class AsmFactory method createBiInstantiator.

@SuppressWarnings("unchecked")
public <S1, S2, T> BiInstantiator<S1, S2, T> createBiInstantiator(final Class<?> s1, final Class<?> s2, final InstantiatorDefinition instantiatorDefinition, final Map<Parameter, BiFunction<? super S1, ? super S2, ?>> injections, boolean builderIgnoresNullValues) throws Exception {
    BiInstantiatorKey instantiatorKey = new BiInstantiatorKey(instantiatorDefinition, injections, s1, s2);
    Class<? extends BiInstantiator<?, ?, ?>> instantiator = biInstantiatorCache.get(instantiatorKey);
    Instantiator builderInstantiator = null;
    if (instantiator == null) {
        final String className = generateClassNameForBiInstantiator(instantiatorKey);
        final byte[] bytes;
        if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) {
            bytes = BiInstantiatorBuilder.createInstantiator(className, s1, s2, (ExecutableInstantiatorDefinition) instantiatorDefinition, injections);
        } else {
            builderInstantiator = createInstantiator(Void.class, ((BuilderInstantiatorDefinition) instantiatorDefinition).getBuilderInstantiator(), new HashMap<Parameter, Getter<? super Void, ?>>(), builderIgnoresNullValues);
            bytes = BiInstantiatorBuilder.createInstantiator(className, s1, s2, builderInstantiator, (BuilderInstantiatorDefinition) instantiatorDefinition, injections, builderIgnoresNullValues);
        }
        instantiator = (Class<? extends BiInstantiator<?, ?, ?>>) createClass(className, bytes, instantiatorKey.getDeclaringClass().getClassLoader());
        biInstantiatorCache.put(instantiatorKey, instantiator);
    }
    Map<String, BiFunction<? super S1, ? super S2, ?>> factoryPerName = new HashMap<String, BiFunction<? super S1, ? super S2, ?>>();
    for (Entry<Parameter, BiFunction<? super S1, ? super S2, ?>> e : injections.entrySet()) {
        factoryPerName.put(e.getKey().getName(), e.getValue());
    }
    if (instantiatorDefinition instanceof ExecutableInstantiatorDefinition) {
        return (BiInstantiator<S1, S2, T>) instantiator.getConstructor(Map.class).newInstance(factoryPerName);
    } else {
        return (BiInstantiator<S1, S2, T>) instantiator.getConstructor(Map.class, Instantiator.class).newInstance(factoryPerName, builderInstantiator);
    }
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Instantiator(org.simpleflatmapper.reflect.Instantiator) BiInstantiator(org.simpleflatmapper.reflect.BiInstantiator) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) BiFunction(org.simpleflatmapper.util.BiFunction) Parameter(org.simpleflatmapper.reflect.Parameter) BiInstantiator(org.simpleflatmapper.reflect.BiInstantiator)

Example 3 with BiFunction

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

the class BuilderBiInstantiatorDefinitionFactoryTest 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, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
    params.put(parameters[1], new ConstantBiFunction<Void, Object, String>("myname"));
    params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertFalse((instantiator instanceof BuilderBiInstantiator));
    assertEquals("myname", o.getName());
    assertEquals(3, o.getId());
}
Also used : HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) 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)

Example 4 with BiFunction

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

the class BuilderBiInstantiatorDefinitionFactoryTest 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, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
    params.put(parameters[2], new ConstantBiFunction<Void, Object, String>("zrux"));
    params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(3));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertFalse((instantiator instanceof BuilderBiInstantiator));
    assertEquals(3, o.getId());
    assertEquals("zrux", o.getZrux());
}
Also used : HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) 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)

Example 5 with BiFunction

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

the class BuilderBiInstantiatorDefinitionFactoryTest 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, BiFunction<? super Void, ? super Object, ?>> params = new HashMap<Parameter, BiFunction<? super Void, ? super Object, ?>>();
    params.put(parameters[2], new ConstantBiFunction<Void, Object, String>(null));
    params.put(parameters[0], new ConstantBiFunction<Void, Object, Integer>(null));
    final InstantiatorFactory instantiatorFactory = new InstantiatorFactory(new AsmFactory(getClass().getClassLoader()), true);
    final BiInstantiator<Void, Object, ClassBuilderWithMethod> instantiator = instantiatorFactory.<Void, Object, ClassBuilderWithMethod>getBiInstantiator(b, Void.class, Object.class, params, true, true);
    final ClassBuilderWithMethod o = instantiator.newInstance(null, null);
    assertFalse((instantiator instanceof BuilderBiInstantiator));
    assertEquals(0, o.getId());
    assertEquals(null, o.getZrux());
}
Also used : HashMap(java.util.HashMap) AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) BuilderInstantiatorDefinition(org.simpleflatmapper.reflect.BuilderInstantiatorDefinition) InstantiatorFactory(org.simpleflatmapper.reflect.InstantiatorFactory) BuilderBiInstantiator(org.simpleflatmapper.reflect.impl.BuilderBiInstantiator) 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