use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.
the class AbstractConstantTargetMapperBuilder method mapper.
@SuppressWarnings("unchecked")
public Mapper<T, S> mapper() {
final List<FieldMapper<T, S>> mappers = new ArrayList<FieldMapper<T, S>>();
final MappingContextFactoryBuilder mappingContextFactoryBuilder = new MappingContextFactoryBuilder(new KeySourceGetter<K, T>() {
@Override
public Object getValue(K key, T source) throws Exception {
throw new UnsupportedOperationException();
}
});
propertyMappingsBuilder.forEachProperties(new ForEachCallBack<PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>>>() {
@Override
public void handle(PropertyMapping<T, ?, K, FieldMapperColumnDefinition<K>> pm) {
preFieldProcess(mappers, pm);
FieldMapper<T, S> fieldMapper = fieldAppenderFactory.newFieldMapper(pm, mappingContextFactoryBuilder, mapperConfig.mapperBuilderErrorHandler());
mappers.add(fieldMapper);
postFieldProcess(mappers, pm);
}
});
postMapperProcess(mappers);
Mapper<T, S> mapper;
FieldMapper[] fields = mappers.toArray(new FieldMapper[0]);
BiInstantiator<T, MappingContext<? super T>, S> instantiator = getInstantiator();
if (mappers.size() < 256) {
try {
mapper = reflectionService.getAsmFactory().registerOrCreate(MapperAsmFactory.class, new UnaryFactory<AsmFactory, MapperAsmFactory>() {
@Override
public MapperAsmFactory newInstance(AsmFactory asmFactory) {
return new MapperAsmFactory(asmFactory);
}
}).<T, S>createMapper(getKeys(), fields, new FieldMapper[0], instantiator, TypeHelper.<T>toClass(classMeta.getType()), sourceClass);
} catch (Throwable e) {
if (mapperConfig.failOnAsm()) {
return ErrorHelper.rethrow(e);
} else {
mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
}
}
} else {
mapper = new MapperImpl<T, S>(fields, new FieldMapper[0], instantiator);
}
return new ContextualMapper<T, S>(mapper, mappingContextFactoryBuilder.newFactory());
}
use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.
the class ResultSetMapperFactoryTest method testMapToDbObjectStatic.
@Test
public void testMapToDbObjectStatic() throws Exception {
DBI dbi = new DBI(DbHelper.getHsqlDataSource());
dbi.registerMapper(new SfmResultSetMapperFactory(new UnaryFactory<Class<?>, Mapper<ResultSet, ?>>() {
@Override
public Mapper<ResultSet, ?> newInstance(Class<?> aClass) {
return new Mapper<ResultSet, DbObject>() {
@Override
public DbObject map(ResultSet source) throws MappingException {
return map(source, null);
}
@Override
public DbObject map(ResultSet source, MappingContext<? super ResultSet> context) throws MappingException {
DbObject dbObject = new DbObject();
try {
mapTo(source, dbObject, context);
} catch (Exception e) {
e.printStackTrace();
}
return dbObject;
}
@Override
public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> context) throws Exception {
target.setId(source.getInt("id"));
target.setCreationTime(source.getTimestamp("creation_time"));
target.setEmail(source.getString("email"));
target.setName(source.getString("name"));
String type_name = source.getString("type_name");
if (type_name != null) {
target.setTypeName(DbObject.Type.valueOf(type_name));
}
target.setTypeOrdinal(DbObject.Type.values()[source.getInt("type_ordinal")]);
}
};
}
}));
Handle handle = dbi.open();
try {
DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
DbHelper.assertDbObjectMapping(dbObject);
SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
attach.insert(DbObject.newInstance());
assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
} finally {
handle.close();
}
}
use of org.simpleflatmapper.map.MappingContext in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceMapperBuilder method getConstructorFieldMappersAndInstantiator.
@SuppressWarnings("unchecked")
private InstantiatorAndFieldMappers getConstructorFieldMappersAndInstantiator() throws MapperBuildingException {
InstantiatorFactory instantiatorFactory = reflectionService.getInstantiatorFactory();
try {
ConstructorInjections constructorInjections = constructorInjections();
Map<Parameter, BiFunction<? super S, ? super MappingContext<? super S>, ?>> injections = constructorInjections.parameterGetterMap;
MapperBiInstantiatorFactory mapperBiInstantiatorFactory = new MapperBiInstantiatorFactory(instantiatorFactory);
GetterFactory<? super S, K> getterFactory = fieldMapperAsGetterFactory();
BiInstantiator<S, MappingContext<? super S>, T> instantiator = mapperBiInstantiatorFactory.<S, T, K, FieldMapperColumnDefinition<K>>getBiInstantiator(mapperSource.source(), target, propertyMappingsBuilder, injections, getterFactory, reflectionService.builderIgnoresNullValues());
return new InstantiatorAndFieldMappers(constructorInjections.fieldMappers, instantiator);
} catch (Exception e) {
return ErrorHelper.rethrow(e);
}
}
Aggregations