Search in sources :

Example 1 with BiFunctionGetter

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

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

the class BiInstantiatorBuilder method getFunctionCall.

private static InjectionPoint getFunctionCall(Parameter parameter, BiFunction biFunction) {
    if (biFunction.getClass().equals(BiFunctionGetter.class)) {
        Getter getter = ((BiFunctionGetter) biFunction).getGetter();
        Class<?> getterClass = getter.getClass();
        if (TypeHelper.isPrimitive(parameter.getType())) {
            Class<?> primitiveGetter = getPrimitiveGetter(parameter.getType());
            if (primitiveGetter == null) {
                throw new IllegalStateException("No primitive getter for primitive " + parameter.getType());
            }
            Type publicGetterClass = AsmUtils.findClosestPublicTypeExposing(getterClass, primitiveGetter);
            if (publicGetterClass != null) {
                return new InjectionPoint(parameter, biFunction, true, "get" + getPrimitiveMethodSuffix(parameter.getType()), publicGetterClass, true);
            }
        }
        Type publicGetterClass = AsmUtils.findClosestPublicTypeExposing(getterClass, Getter.class);
        return new InjectionPoint(parameter, biFunction, true, "get", publicGetterClass, false);
    } else {
        return new InjectionPoint(parameter, biFunction, false, "apply", biFunction.getClass(), false);
    }
}
Also used : Type(java.lang.reflect.Type) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Getter(org.simpleflatmapper.reflect.Getter) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter) ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) BiFunctionGetter(org.simpleflatmapper.reflect.getter.BiFunctionGetter)

Aggregations

Getter (org.simpleflatmapper.reflect.Getter)2 BiFunctionGetter (org.simpleflatmapper.reflect.getter.BiFunctionGetter)2 Type (java.lang.reflect.Type)1 FieldMapper (org.simpleflatmapper.map.FieldMapper)1 MappingContext (org.simpleflatmapper.map.MappingContext)1 MappingContextFactoryBuilder (org.simpleflatmapper.map.context.MappingContextFactoryBuilder)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)1 Parameter (org.simpleflatmapper.reflect.Parameter)1 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)1 NullGetter (org.simpleflatmapper.reflect.getter.NullGetter)1 ConstructorPropertyMeta (org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta)1 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)1 SelfPropertyMeta (org.simpleflatmapper.reflect.meta.SelfPropertyMeta)1 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)1 BooleanGetter (org.simpleflatmapper.reflect.primitive.BooleanGetter)1 ByteGetter (org.simpleflatmapper.reflect.primitive.ByteGetter)1 CharacterGetter (org.simpleflatmapper.reflect.primitive.CharacterGetter)1 DoubleGetter (org.simpleflatmapper.reflect.primitive.DoubleGetter)1 FloatGetter (org.simpleflatmapper.reflect.primitive.FloatGetter)1