Search in sources :

Example 1 with Setter

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

the class CtfmSetterNotFoundTest method csvMapperOverrideGetter.

@Test
public void csvMapperOverrideGetter() throws IOException {
    StringBuilder sb = new StringBuilder();
    CsvWriter.from(Foo2.class).column("bar", new SetterProperty(new Setter<Appendable, Bar2Prop>() {

        @Override
        public void set(Appendable target, Bar2Prop value) throws Exception {
            target.append(value.getVal()).append(":").append(String.valueOf(value.getI()));
        }
    })).to(sb).append(new Foo2(new Bar2Prop("val", 3)));
    assertEquals("bar\r\nval:3\r\n", sb.toString());
}
Also used : SetterProperty(org.simpleflatmapper.map.property.SetterProperty) Setter(org.simpleflatmapper.reflect.Setter) Test(org.junit.Test)

Example 2 with Setter

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

the class SettableDataSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<SettableByIndexData, P> getSetter(PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>> arg) {
    Setter<SettableByIndexData, P> setter = null;
    Type propertyType = arg.getPropertyMeta().getPropertyType();
    final DataType dataType = arg.getColumnKey().getDataType();
    Type type = dataType != null ? DataTypeHelper.asJavaClass(dataType) : null;
    if (type == null) {
        type = propertyType;
    }
    if (TypeHelper.isEnum(propertyType)) {
        if (TypeHelper.areEquals(type, String.class)) {
            return (Setter<SettableByIndexData, P>) new StringEnumSettableDataSetter(arg.getColumnKey().getIndex());
        } else {
            return (Setter<SettableByIndexData, P>) new OrdinalEnumSettableDataSetter(arg.getColumnKey().getIndex());
        }
    }
    SetterFactory<SettableByIndexData, PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>>> setterFactory = this.factoryPerClass.get(TypeHelper.toClass(type));
    if (setterFactory != null) {
        setter = setterFactory.getSetter(arg);
        if (!TypeHelper.areEquals(TypeHelper.toBoxedClass(type), TypeHelper.toBoxedClass(propertyType))) {
            Converter<?, ?> converter = getConverter(propertyType, TypeHelper.toClass(type), dataType, arg.getColumnDefinition());
            if (converter != null) {
                setter = (Setter<SettableByIndexData, P>) new ConvertDelegateSetter<SettableByIndexData, Object, P>(setter, (Converter<Object, P>) converter);
            } else {
                setter = null;
            }
        }
    }
    if (setter == null && dataType != null) {
        if (dataType instanceof UserType) {
            if (propertyType.equals(UDTValue.class)) {
                setter = (Setter<SettableByIndexData, P>) new UDTValueSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = (Setter<SettableByIndexData, P>) UDTObjectSettableDataSetter.newInstance(propertyType, (UserType) dataType, arg.getColumnKey().getIndex(), mapperConfig, reflectionService);
            }
        } else if (TypeHelper.isAssignable(List.class, type) && TypeHelper.isAssignable(List.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new ListSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new ListWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Set.class, type) && TypeHelper.isAssignable(Set.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new SetSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new SetWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Map.class, type) && TypeHelper.isAssignable(Map.class, propertyType)) {
            DataType dtKeyType = dataType.getTypeArguments().get(0);
            DataType dtValueType = dataType.getTypeArguments().get(1);
            TypeHelper.MapEntryTypes keyValueTypeOfMap = TypeHelper.getKeyValueTypeOfMap(propertyType);
            if (areSame(dtKeyType, keyValueTypeOfMap.getKeyType()) && areSame(dtValueType, keyValueTypeOfMap.getValueType())) {
                setter = new MapSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = new MapWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), getConverter(keyValueTypeOfMap.getKeyType(), DataTypeHelper.asJavaClass(dtKeyType), dtKeyType, arg.getColumnDefinition()), getConverter(keyValueTypeOfMap.getValueType(), DataTypeHelper.asJavaClass(dtValueType), dtValueType, arg.getColumnDefinition()));
            }
        }
    }
    return setter;
}
Also used : OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) Converter(org.simpleflatmapper.converter.Converter) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ColumnDefinition(org.simpleflatmapper.map.mapper.ColumnDefinition) Type(java.lang.reflect.Type) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) DoubleSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter) BigDecimalSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter) TimeSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimeSettableDataSetter) BigIntegerSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter) IntSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.IntSettableDataSetter) UUIDSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UUIDSettableDataSetter) TupleValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TupleValueSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) InetAddressSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.InetAddressSettableDataSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) ShortSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ShortSettableDataSetter) UDTObjectSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter) DateSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter) TimestampSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimestampSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) FloatSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.FloatSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) LongSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.LongSettableDataSetter) OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) Setter(org.simpleflatmapper.reflect.Setter) ByteSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) StringSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter)

Example 3 with Setter

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

the class CsvMapperBuilder method buildDelayedSetters.

@SuppressWarnings({ "unchecked" })
private DelayedCellSetterFactory<T, ?>[] buildDelayedSetters(final ParsingContextFactoryBuilder parsingContextFactoryBuilder, int delayedSetterEnd, boolean hasKeys) {
    final Map<String, CsvMapperBuilder<?>> delegateMapperBuilders = new HashMap<String, CsvMapperBuilder<?>>();
    final Map<String, Integer> propertyToMapperIndex = new HashMap<String, Integer>();
    final DelayedCellSetterFactory<T, ?>[] delayedSetters = new DelayedCellSetterFactory[delayedSetterEnd];
    final int newMinDelayedSetter = minDelayedSetter != 0 ? minDelayedSetter : hasKeys ? delayedSetterEnd : 0;
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {

        final CellSetterFactory cellSetterFactory = new CellSetterFactory(cellValueReaderFactory, mapperConfig.mapperBuilderErrorHandler());

        @Override
        public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
            if (propMapping != null) {
                PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
                CsvColumnKey key = propMapping.getColumnKey();
                if (prop != null) {
                    if (prop.isSubProperty()) {
                        addSubProperty(delegateMapperBuilders, prop, key, propMapping.getColumnDefinition());
                    } else {
                        delayedSetters[propMapping.getColumnKey().getIndex()] = cellSetterFactory.getDelayedCellSetter(prop, key.getIndex(), propMapping.getColumnDefinition(), parsingContextFactoryBuilder);
                    }
                }
            }
        }

        private <I, P> void addSubProperty(Map<String, CsvMapperBuilder<?>> delegateMapperBuilders, PropertyMeta<T, P> prop, CsvColumnKey key, CsvColumnDefinition columnDefinition) {
            SubPropertyMeta<T, I, P> subPropertyMeta = (SubPropertyMeta<T, I, P>) prop;
            final PropertyMeta<T, I> propOwner = subPropertyMeta.getOwnerProperty();
            CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propOwner.getName());
            if (delegateMapperBuilder == null) {
                delegateMapperBuilder = new CsvMapperBuilder<I>(propOwner.getPropertyType(), propOwner.getPropertyClassMeta(), newMinDelayedSetter, cellValueReaderFactory, mapperConfig);
                delegateMapperBuilders.put(propOwner.getName(), delegateMapperBuilder);
            }
            Integer currentIndex = propertyToMapperIndex.get(propOwner.getName());
            if (currentIndex == null || currentIndex < key.getIndex()) {
                propertyToMapperIndex.put(propOwner.getName(), key.getIndex());
            }
            delegateMapperBuilder.addMapping(subPropertyMeta.getSubProperty(), key, columnDefinition);
        }
    }, 0, delayedSetterEnd);
    final Map<String, CsvMapper<?>> mappers = new HashMap<String, CsvMapper<?>>();
    propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition>>() {

        @Override
        public void handle(PropertyMapping<T, ?, CsvColumnKey, CsvColumnDefinition> propMapping) {
            if (propMapping == null)
                return;
            PropertyMeta<T, ?> prop = propMapping.getPropertyMeta();
            if (prop.isSubProperty()) {
                addSubPropertyDelayedSetter(delegateMapperBuilders, delayedSetters, propMapping.getColumnKey().getIndex(), prop);
            }
        }

        private <I, P> void addSubPropertyDelayedSetter(Map<String, CsvMapperBuilder<?>> delegateMapperBuilders, DelayedCellSetterFactory<T, ?>[] delayedSetters, int setterIndex, PropertyMeta<T, P> prop) {
            PropertyMeta<T, I> subProp = ((SubPropertyMeta<T, I, P>) prop).getOwnerProperty();
            final String propName = subProp.getName();
            CsvMapper<I> mapper = (CsvMapper<I>) mappers.get(propName);
            if (mapper == null) {
                CsvMapperBuilder<I> delegateMapperBuilder = (CsvMapperBuilder<I>) delegateMapperBuilders.get(propName);
                mapper = delegateMapperBuilder.mapper();
                mappers.put(propName, mapper);
            }
            int indexOfMapper = propertyToMapperIndex.get(propName);
            Setter<? super T, ? super I> setter = null;
            if (!subProp.isConstructorProperty()) {
                setter = subProp.getSetter();
            }
            delayedSetters[setterIndex] = new DelegateMarkerDelayedCellSetterFactory<T, I>(mapper, setter, setterIndex, indexOfMapper);
        }
    }, 0, delayedSetterEnd);
    return delayedSetters;
}
Also used : HashMap(java.util.HashMap) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) ConstructorPropertyMeta(org.simpleflatmapper.reflect.meta.ConstructorPropertyMeta) SelfPropertyMeta(org.simpleflatmapper.reflect.meta.SelfPropertyMeta) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) DelayedCellSetterFactory(org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory) DelayedCellSetterFactory(org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) CellSetter(org.simpleflatmapper.csv.mapper.CellSetter) Setter(org.simpleflatmapper.reflect.Setter)

Example 4 with Setter

use of org.simpleflatmapper.reflect.Setter 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 5 with Setter

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

the class PreparedStatementFieldMapperFactoryTest method testCustomSetter.

@Test
public void testCustomSetter() throws Exception {
    Object o = new Object();
    newFieldMapperAndMapToPS(new ConstantGetter<Object, Object>(o), Object.class, new SetterProperty(new Setter<PreparedStatement, Object>() {

        @Override
        public void set(PreparedStatement target, Object value) throws Exception {
            ps.setString(35, "aa");
        }
    }));
    verify(ps).setString(35, "aa");
}
Also used : SetterProperty(org.simpleflatmapper.map.property.SetterProperty) Setter(org.simpleflatmapper.reflect.Setter) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test) ResultSetGetterFactoryTest(org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)

Aggregations

Setter (org.simpleflatmapper.reflect.Setter)7 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)4 Test (org.junit.Test)3 Type (java.lang.reflect.Type)2 ResultSetGetterFactoryTest (org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 SetterFactoryProperty (org.simpleflatmapper.map.property.SetterFactoryProperty)2 SetterProperty (org.simpleflatmapper.map.property.SetterProperty)2 SetterFactory (org.simpleflatmapper.reflect.SetterFactory)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 HashMap (java.util.HashMap)1 Converter (org.simpleflatmapper.converter.Converter)1 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)1 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)1 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)1 BigDecimalSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter)1 BigIntegerSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter)1 ByteSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter)1 DateSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter)1 DoubleSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter)1