org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactoryImpl Maven / Gradle / Ivy
Show all versions of sfm-map Show documentation
package org.simpleflatmapper.map.mapper;
import org.simpleflatmapper.converter.ContextFactoryBuilder;
import org.simpleflatmapper.map.FieldKey;
import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.MapperBuilderErrorHandler;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.setter.*;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.SetterFactory;
import org.simpleflatmapper.reflect.primitive.*;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.reflect.meta.ObjectClassMeta;
import org.simpleflatmapper.reflect.meta.PropertyMeta;
import org.simpleflatmapper.map.fieldmapper.*;
import org.simpleflatmapper.util.ErrorDoc;
import org.simpleflatmapper.util.TypeHelper;
import java.lang.reflect.Type;
public class ConstantTargetFieldMapperFactoryImpl> implements ConstantTargetFieldMapperFactory {
private final ContextualSetterFactory> setterFactory;
private final Type targetType;
private ConstantTargetFieldMapperFactoryImpl(ContextualSetterFactory> setterFactory, Type targetType) {
this.setterFactory = setterFactory;
this.targetType = targetType;
}
@SuppressWarnings("unchecked")
@Override
public FieldMapper newFieldMapper(
PropertyMapping pm,
MappingContextFactoryBuilder contextFactoryBuilder,
MapperBuilderErrorHandler mappingErrorHandler
) {
Getter super S, ? extends P> getter =
(Getter super S, ? extends P>) pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType());
if (getter == null) {
getter = pm.getPropertyMeta().getGetter();
}
if (getter == null) {
mappingErrorHandler.accessorNotFound("Could not find getter for " + pm.getColumnKey() + " type "
+ pm.getPropertyMeta().getPropertyType()
+ " path " + pm.getPropertyMeta().getPath() + ", See " + ErrorDoc.CTFM_GETTER_NOT_FOUND.toUrl());
return null;
}
ContextualSetter super T, ? super P> setter = getSetterForTarget(pm, contextFactoryBuilder);
if (setter == null) {
mappingErrorHandler.accessorNotFound("Could not find setter for " + pm.getColumnKey() + " type "
+ pm.getPropertyMeta().getPropertyType()
+ " path " + pm.getPropertyMeta().getPath()
+ " See " + ErrorDoc.CTFM_SETTER_NOT_FOUND.toUrl());
return null;
}
Type propertyType = pm.getPropertyMeta().getPropertyType();
return buildFieldMapper(getter, setter, propertyType);
}
@SuppressWarnings("unchecked")
private FieldMapper buildFieldMapper(Getter super S, ? extends P> getter, ContextualSetter super T, ? super P> setter, Type propertyType) {
if (TypeHelper.isPrimitive(propertyType)) {
if (getter instanceof BooleanGetter && setter instanceof BooleanContextualSetter) {
return new BooleanConstantTargetFieldMapper((BooleanGetter)getter, (BooleanContextualSetter) setter);
} else if (getter instanceof ByteGetter && setter instanceof ByteContextualSetter) {
return new ByteConstantTargetFieldMapper((ByteGetter)getter, (ByteContextualSetter) setter);
} else if (getter instanceof CharacterGetter && setter instanceof CharacterContextualSetter) {
return new CharacterConstantTargetFieldMapper((CharacterGetter)getter, (CharacterContextualSetter) setter);
} else if (getter instanceof ShortGetter && setter instanceof ShortContextualSetter) {
return new ShortConstantTargetFieldMapper((ShortGetter)getter, (ShortContextualSetter) setter);
} else if (getter instanceof IntGetter && setter instanceof IntContextualSetter) {
return new IntConstantTargetFieldMapper((IntGetter)getter, (IntContextualSetter) setter);
} else if (getter instanceof LongGetter && setter instanceof LongContextualSetter) {
return new LongConstantTargetFieldMapper((LongGetter)getter, (LongContextualSetter) setter);
} else if (getter instanceof FloatGetter && setter instanceof FloatContextualSetter) {
return new FloatConstantTargetFieldMapper((FloatGetter)getter, (FloatContextualSetter) setter);
} else if (getter instanceof DoubleGetter && setter instanceof DoubleContextualSetter) {
return new DoubleConstantTargetFieldMapper((DoubleGetter)getter, (DoubleContextualSetter) setter);
}
}
return new ConstantTargetFieldMapper(getter, setter);
}
@SuppressWarnings("unchecked")
private ContextualSetter getSetterForTarget(PropertyMapping pm, ContextFactoryBuilder contextFactoryBuilder) {
ContextualSetter setter = ContextualSetterAdapter.of((Setter) pm.getColumnDefinition().getCustomSetterTo(targetType));
final SetterFactory> customSetterFactory =
(SetterFactory>) pm.getColumnDefinition().getCustomSetterFactoryTo(targetType);
if (customSetterFactory != null) {
setter = ContextualSetterAdapter.of(customSetterFactory.getSetter(pm));
}
if (setter == null){
setter = setterFactory.getSetter(pm, contextFactoryBuilder);
}
if (setter == null) {
if (!pm.getPropertyMeta().isSelf()) {
final ClassMeta> propertyClassMeta = pm.getPropertyMeta().getPropertyClassMeta();
if (propertyClassMeta instanceof ObjectClassMeta) {
ObjectClassMeta
ocm = (ObjectClassMeta
) propertyClassMeta;
if (ocm.getNumberOfProperties() == 1) {
PropertyMeta
subProp = ocm.getFirstProperty();
ContextualSetter subSetter = getSetterForTarget(pm.propertyMeta(subProp), contextFactoryBuilder);
if (subSetter != null) {
return new ContextualSetterOnGetter(subSetter, (Getter) subProp.getGetter());
}
}
}
}
}
return setter;
}
public static > ConstantTargetFieldMapperFactory newInstance(
SetterFactory> setterFactory, Type targetType) {
return newInstance(new ContextualSetterFactoryAdapter>(setterFactory), targetType);
}
public static > ConstantTargetFieldMapperFactory newInstance(
ContextualSetterFactory> setterFactory, Type targetType) {
return new ConstantTargetFieldMapperFactoryImpl(setterFactory, targetType);
}
}