Search in sources :

Example 1 with SetterFactory

use of org.simpleflatmapper.reflect.SetterFactory in project SimpleFlatMapper by arnaudroger.

the class FieldMapperToAppendableFactory method setterFromFactory.

@SuppressWarnings("unchecked")
private <S, P> Setter<Appendable, ? super P> setterFromFactory(PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> pm) {
    Setter<Appendable, ? super P> setter = null;
    final SetterFactoryProperty setterFactoryProperty = pm.getColumnDefinition().lookFor(SetterFactoryProperty.class);
    if (setterFactoryProperty != null) {
        final SetterFactory<Appendable, PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>>> setterFactory = (SetterFactory<Appendable, PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>>>) setterFactoryProperty.getSetterFactory();
        setter = setterFactory.getSetter(pm);
    }
    if (setter == null) {
        if (!pm.getPropertyMeta().isSelf()) {
            final ClassMeta<P> classMeta = pm.getPropertyMeta().getPropertyClassMeta();
            if (classMeta instanceof ObjectClassMeta) {
                ObjectClassMeta<P> ocm = (ObjectClassMeta<P>) classMeta;
                if (ocm.getNumberOfProperties() == 1) {
                    PropertyMeta<P, ?> subProp = ocm.getFirstProperty();
                    Setter<Appendable, Object> subSetter = (Setter<Appendable, Object>) setterFromFactory(pm.propertyMeta(subProp));
                    if (subSetter != null) {
                        setter = new SetterOnGetter<Appendable, Object, P>(subSetter, (Getter<P, Object>) subProp.getGetter());
                    } else {
                        return new ObjectToStringSetter<P>(subProp.getGetter());
                    }
                }
            }
        }
    }
    return setter;
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Getter(org.simpleflatmapper.reflect.Getter) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) SetterOnGetter(org.simpleflatmapper.reflect.setter.SetterOnGetter) ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) SetterFactory(org.simpleflatmapper.reflect.SetterFactory) SetterFactoryProperty(org.simpleflatmapper.map.property.SetterFactoryProperty) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) Setter(org.simpleflatmapper.reflect.Setter) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta)

Example 2 with SetterFactory

use of org.simpleflatmapper.reflect.SetterFactory in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementFieldMapperFactoryTest method testCustomSetterFactory.

@Test
public void testCustomSetterFactory() throws Exception {
    Object o = new Object();
    newFieldMapperAndMapToPS(new ConstantGetter<Object, Object>(o), Object.class, new SetterFactoryProperty(new SetterFactory<PreparedStatement, PropertyMapping<?, ?, ?, ?>>() {

        @Override
        public <P> Setter<PreparedStatement, P> getSetter(PropertyMapping<?, ?, ?, ?> arg) {
            return new Setter<PreparedStatement, P>() {

                @Override
                public void set(PreparedStatement target, P value) throws Exception {
                    ps.setString(36, "bb");
                }
            };
        }
    }));
    verify(ps).setString(36, "bb");
}
Also used : SetterFactory(org.simpleflatmapper.reflect.SetterFactory) PreparedStatementSetterFactory(org.simpleflatmapper.jdbc.impl.PreparedStatementSetterFactory) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) Setter(org.simpleflatmapper.reflect.Setter) DbObject(org.simpleflatmapper.test.beans.DbObject) SetterFactoryProperty(org.simpleflatmapper.map.property.SetterFactoryProperty) Test(org.junit.Test) ResultSetGetterFactoryTest(org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)

Aggregations

PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)2 SetterFactoryProperty (org.simpleflatmapper.map.property.SetterFactoryProperty)2 Setter (org.simpleflatmapper.reflect.Setter)2 SetterFactory (org.simpleflatmapper.reflect.SetterFactory)2 Test (org.junit.Test)1 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)1 PreparedStatementSetterFactory (org.simpleflatmapper.jdbc.impl.PreparedStatementSetterFactory)1 ResultSetGetterFactoryTest (org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)1 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)1 Getter (org.simpleflatmapper.reflect.Getter)1 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)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 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)1 LongGetter (org.simpleflatmapper.reflect.primitive.LongGetter)1 ShortGetter (org.simpleflatmapper.reflect.primitive.ShortGetter)1 SetterOnGetter (org.simpleflatmapper.reflect.setter.SetterOnGetter)1