Search in sources :

Example 11 with Getter

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

the class ResultSetGetterFactory method newGetter.

@SuppressWarnings("unchecked")
@Override
public <P> Getter<ResultSet, P> newGetter(Type genericType, JdbcColumnKey key, Object... properties) {
    Class<?> clazz = TypeHelper.wrap(TypeHelper.toClass(genericType));
    if (Object.class.equals(clazz)) {
        return (Getter<ResultSet, P>) new ObjectResultSetGetter(key.getIndex());
    }
    GetterFactory<ResultSet, JdbcColumnKey> getterFactory = factoryRegistry.findFactoryFor(clazz);
    Getter<ResultSet, P> getter = null;
    if (getterFactory != null) {
        getter = (Getter<ResultSet, P>) getterFactory.newGetter(genericType, key, properties);
    }
    if (getter == null) {
        if (SQLData.class.isAssignableFrom(clazz) || key.getSqlType(properties) == Types.JAVA_OBJECT) {
            return (Getter<ResultSet, P>) new ObjectResultSetGetter(key.getIndex());
        }
    }
    return getter;
}
Also used : BigIntegerFromStringResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.BigIntegerFromStringResultSetGetter) CharacterResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.CharacterResultSetGetter) UndefinedDateResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.UndefinedDateResultSetGetter) ByteArrayResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ByteArrayResultSetGetter) StringUUIDGetter(org.simpleflatmapper.reflect.getter.StringUUIDGetter) Getter(org.simpleflatmapper.reflect.Getter) ObjectResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ObjectResultSetGetter) BigDecimalResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.BigDecimalResultSetGetter) ReaderResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ReaderResultSetGetter) BlobResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.BlobResultSetGetter) SqlArrayResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.SqlArrayResultSetGetter) BigDecimalFromStringResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.BigDecimalFromStringResultSetGetter) IntResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.IntResultSetGetter) ByteResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ByteResultSetGetter) ShortResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ShortResultSetGetter) UrlResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.UrlResultSetGetter) UUIDUnspecifiedTypeGetter(org.simpleflatmapper.reflect.getter.UUIDUnspecifiedTypeGetter) NStringResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.NStringResultSetGetter) RowIdResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.RowIdResultSetGetter) StringResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.StringResultSetGetter) TimestampResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.TimestampResultSetGetter) BooleanResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.BooleanResultSetGetter) NClobResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.NClobResultSetGetter) InputStreamResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.InputStreamResultSetGetter) RefResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.RefResultSetGetter) NReaderResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.NReaderResultSetGetter) BigIntegerResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.BigIntegerResultSetGetter) UrlFromStringResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.UrlFromStringResultSetGetter) ClobResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ClobResultSetGetter) BytesUUIDGetter(org.simpleflatmapper.reflect.getter.BytesUUIDGetter) TimeResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.TimeResultSetGetter) SQLXMLResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.SQLXMLResultSetGetter) DoubleResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.DoubleResultSetGetter) DateResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.DateResultSetGetter) LongResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.LongResultSetGetter) FloatResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.FloatResultSetGetter) CalendarResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.CalendarResultSetGetter) ObjectResultSetGetter(org.simpleflatmapper.jdbc.impl.getter.ObjectResultSetGetter)

Example 12 with Getter

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

the class CsvMapperCellHandlerImpAsmInstantiatorTest method testInstantiateStringWithCharArray.

@SuppressWarnings("unchecked")
@Test
public void testInstantiateStringWithCharArray() throws Exception {
    HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>> injections = new HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>>();
    Parameter parameter = new Parameter(0, "arg0", char[].class);
    DelayedGetter delayedGetter = new DelayedGetter(0);
    injections.put(parameter, delayedGetter);
    Class<CsvMapperCellHandlerImpl<String>> source = (Class) CsvMapperCellHandlerImpl.class;
    Instantiator<CsvMapperCellHandlerImpl<String>, String> instantiator = factory.createInstantiator(source, new ExecutableInstantiatorDefinition(String.class.getConstructor(char[].class), parameter), injections, true);
    DelayedCellSetterImpl delayedCellSetter = new DelayedCellSetterImpl(null, new CellValueReader() {

        @Override
        public char[] read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return new char[] { 'h' };
        }
    });
    delayedCellSetter.set(null, 0, 0, null);
    CsvMapperCellHandlerImpl targetSettersImpl = new CsvMapperCellHandlerImpl(instantiator, new DelayedCellSetter[] { delayedCellSetter }, new CellSetter[] {}, null, null, null);
    Assert.assertNotNull(instantiator.newInstance(targetSettersImpl));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ParsingContext(org.simpleflatmapper.csv.ParsingContext) HashMap(java.util.HashMap) DelayedCellSetterImpl(org.simpleflatmapper.csv.impl.cellreader.DelayedCellSetterImpl) Getter(org.simpleflatmapper.reflect.Getter) CellValueReader(org.simpleflatmapper.csv.CellValueReader) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 13 with Getter

use of org.simpleflatmapper.reflect.Getter 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 14 with Getter

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

the class JdbcMapperFactoryTest method testCustomGetterFactory.

@Test
public void testCustomGetterFactory() throws SQLException {
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().getterFactory(new GetterFactory<ResultSet, JdbcColumnKey>() {

        @SuppressWarnings("unchecked")
        @Override
        public <P> Getter<ResultSet, P> newGetter(Type target, JdbcColumnKey key, Object... properties) {
            return new Getter() {

                @Override
                public Object get(Object target) throws Exception {
                    return "Hello!";
                }
            };
        }
    }).newBuilder(DbObject.class).addMapping("name").mapper();
    ResultSet rs = mock(ResultSet.class);
    when(rs.next()).thenReturn(true, false);
    DbObject object = mapper.iterator(rs).next();
    assertEquals("Hello!", object.getName());
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) GetterFactory(org.simpleflatmapper.reflect.getter.GetterFactory) Type(java.lang.reflect.Type) DbObject(org.simpleflatmapper.test.beans.DbObject) Getter(org.simpleflatmapper.reflect.Getter) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Test(org.junit.Test)

Example 15 with Getter

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

the class ConstantSourceFieldMapperFactoryImpl method getGettetInstantiator.

private <T, P> Getter<? super S, ? extends P> getGettetInstantiator(ClassMeta<P> classMeta, InstantiatorDefinition id, K key, FieldMapperColumnDefinition<K> columnDefinition, Collection<Type> types) {
    Instantiator<? super T, ? extends P> instantiator = classMeta.getReflectionService().getInstantiatorFactory().getOneArgIdentityInstantiator(id, classMeta.getReflectionService().builderIgnoresNullValues());
    final Type paramType = id.getParameters()[0].getGenericType();
    Getter<? super S, ? extends T> subGetter = getterFactory.newGetter(paramType, key, columnDefinition);
    if (subGetter == null) {
        if (types.contains(paramType)) {
            // loop circuit cutter
            return null;
        }
        types.add(paramType);
        // converter?
        Type sourceType = key.getType(paramType);
        Converter converter = converterService.findConverter(sourceType, paramType, columnDefinition.properties());
        if (converter != null) {
            Getter sourceTypeGetter = getterFactory.newGetter(sourceType, key, columnDefinition);
            subGetter = new GetterWithConverter(converter, sourceTypeGetter);
        } else {
            subGetter = lookForInstantiatorGetter(classMeta.getReflectionService().<T>getClassMeta(paramType), key, columnDefinition, types);
        }
    }
    if (subGetter != null) {
        return new InstantiatorGetter<T, S, P>(instantiator, subGetter);
    } else
        return null;
}
Also used : InstantiatorGetter(org.simpleflatmapper.reflect.getter.InstantiatorGetter) Type(java.lang.reflect.Type) Getter(org.simpleflatmapper.reflect.Getter) InstantiatorGetter(org.simpleflatmapper.reflect.getter.InstantiatorGetter) GetterWithConverter(org.simpleflatmapper.reflect.getter.GetterWithConverter) Converter(org.simpleflatmapper.converter.Converter) GetterWithConverter(org.simpleflatmapper.reflect.getter.GetterWithConverter)

Aggregations

Getter (org.simpleflatmapper.reflect.Getter)20 Type (java.lang.reflect.Type)6 Test (org.junit.Test)6 Parameter (org.simpleflatmapper.reflect.Parameter)6 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)6 HashMap (java.util.HashMap)4 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ResultSet (java.sql.ResultSet)3 OrdinalEnumGetter (org.simpleflatmapper.reflect.getter.OrdinalEnumGetter)3 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Converter (org.simpleflatmapper.converter.Converter)2 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 ConstantIntGetter (org.simpleflatmapper.reflect.getter.ConstantIntGetter)2 ConstantLongGetter (org.simpleflatmapper.reflect.getter.ConstantLongGetter)2 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)2 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)2