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;
}
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));
}
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;
}
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());
}
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;
}
Aggregations