Search in sources :

Example 1 with ContextualGetterFactory

use of org.simpleflatmapper.map.getter.ContextualGetterFactory in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceFieldMapperFactoryImpl method getGetterFromSource.

@Override
public <P> ContextualGetter<? super S, ? extends P> getGetterFromSource(K columnKey, Type propertyType, ColumnDefinition<K, ?> columnDefinition, Supplier<ClassMeta<P>> propertyClassMetaSupplier, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder) {
    @SuppressWarnings("unchecked") ContextualGetter<? super S, ? extends P> getter = ContextualGetterAdapter.of((Getter<? super S, ? extends P>) columnDefinition.getCustomGetterFrom(sourceType));
    if (getter == null) {
        ContextualGetterFactory<? super S, K> customGetterFactory = (ContextualGetterFactory<? super S, K>) columnDefinition.getCustomGetterFactoryFrom(sourceType);
        if (customGetterFactory != null) {
            getter = (ContextualGetter<? super S, ? extends P>) customGetterFactory.<P>newGetter(propertyType, columnKey, mappingContextFactoryBuilder, columnDefinition.properties());
        }
    }
    ConverterProperty converterProperty = columnDefinition.lookFor(ConverterProperty.class);
    if (converterProperty != null) {
        Type t = converterProperty.inType;
        if (Object.class.equals(t)) {
            // lost type info... assume sql type is right
            t = columnKey.getType(t);
        }
        getter = getterFactory.<P>newGetter(t, columnKey, mappingContextFactoryBuilder, columnDefinition.properties());
        if (getter == null) {
            return null;
        }
        return new FieldMapperGetterWithConverter(converterProperty.function, getter);
    }
    if (getter == null) {
        getter = getterFactory.newGetter(propertyType, columnKey, mappingContextFactoryBuilder, columnDefinition.properties());
    }
    // try to identify constructor that we could build from
    if (getter == null) {
        getter = lookForAlternativeGetter(propertyClassMetaSupplier.get(), columnKey, columnDefinition, new HashSet<Type>(), mappingContextFactoryBuilder);
    }
    DefaultValueProperty defaultValueProperty = columnDefinition.lookFor(DefaultValueProperty.class);
    if (defaultValueProperty != null) {
        Object value = defaultValueProperty.getValue();
        if (value != null) {
            if (TypeHelper.isAssignable(propertyType, value.getClass())) {
                getter = new ContextualGetterWithDefaultValue<S, P>(getter, (P) value);
            } else {
                throw new IllegalArgumentException("Incompatible default value " + value + " type " + value.getClass() + " with property " + columnKey + " of type " + propertyType);
            }
        }
    }
    return getter;
}
Also used : DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) Type(java.lang.reflect.Type) ConverterProperty(org.simpleflatmapper.map.property.ConverterProperty) ContextualGetterFactory(org.simpleflatmapper.map.getter.ContextualGetterFactory) HashSet(java.util.HashSet)

Aggregations

Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 ContextualGetterFactory (org.simpleflatmapper.map.getter.ContextualGetterFactory)1 ConverterProperty (org.simpleflatmapper.map.property.ConverterProperty)1 DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)1