use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.
the class CsvMapperCellHandlerBuilder method createTargetSetterFactory.
public static byte[] createTargetSetterFactory(String factoryName, String className, Type target) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
String factoryType = AsmUtils.toAsmType(factoryName);
String classType = AsmUtils.toAsmType(className);
String targetType = AsmUtils.toAsmType(target);
cw.visit(Opcodes.V1_6, Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, factoryType, "L" + CELL_HANDLER_FACTORY_TYPE + "<L" + targetType + ";>;", CELL_HANDLER_FACTORY_TYPE, null);
{
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", "(" + "L" + AsmUtils.toAsmType(Instantiator.class) + "<L" + AsmUtils.toAsmType(CsvMapperCellHandler.class) + "<L" + targetType + ";>;L" + targetType + ";>;" + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + "L" + AsmUtils.toAsmType(FieldMapperErrorHandler.class) + "<L" + AsmUtils.toAsmType(CsvColumnKey.class) + ";>;" + ")V", null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitVarInsn(Opcodes.ALOAD, 3);
mv.visitVarInsn(Opcodes.ALOAD, 4);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, CELL_HANDLER_FACTORY_TYPE, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(5, 5);
mv.visitEnd();
}
{
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "newInstance", "(" + AsmUtils.toTargetTypeDeclaration(DelayedCellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CellSetter[].class) + ")" + AsmUtils.toTargetTypeDeclaration(CsvMapperCellHandler.class), "(" + "[L" + DELAYED_CELL_SETTER_TYPE + "<L" + targetType + ";*>;" + "[L" + CELL_SETTER_TYPE + "<L" + targetType + ";>;" + ")" + "L" + AsmUtils.toAsmType(CsvMapperCellHandler.class) + "<L" + targetType + ";>;", null);
mv.visitCode();
mv.visitTypeInsn(Opcodes.NEW, classType);
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "instantiator", AsmUtils.toTargetTypeDeclaration(Instantiator.class));
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "keys", AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class));
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "parsingContextFactory", AsmUtils.toTargetTypeDeclaration(ParsingContextFactory.class));
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, AsmUtils.toAsmType(ParsingContextFactory.class), "newContext", "()" + AsmUtils.toTargetTypeDeclaration(ParsingContext.class), false);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, factoryType, "fieldErrorHandler", AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class));
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classType, "<init>", "(" + AsmUtils.toTargetTypeDeclaration(Instantiator.class) + AsmUtils.toTargetTypeDeclaration(DelayedCellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CellSetter[].class) + AsmUtils.toTargetTypeDeclaration(CsvColumnKey[].class) + AsmUtils.toTargetTypeDeclaration(ParsingContext.class) + AsmUtils.toTargetTypeDeclaration(FieldMapperErrorHandler.class) + ")V", false);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(8, 3);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.
the class ColumnsMapperKeyBuilderCellConsumer method newCell.
@Override
public void newCell(char[] chars, int offset, int length) {
if (!hasNoData)
throw new IllegalArgumentException("Already consume the headers");
columns.add(new CsvColumnKey(new String(chars, offset, length), index));
index++;
}
use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.
the class CsvParserTest method testDSLIgnoreField.
@Test
public void testDSLIgnoreField() throws Exception {
Iterator<Tuple2<String, String>> iterator = CsvParser.mapTo(String.class, String.class).columnDefinition(new Predicate<CsvColumnKey>() {
@Override
public boolean test(CsvColumnKey csvColumnKey) {
return csvColumnKey.getIndex() != 1 && csvColumnKey.getIndex() != 2;
}
}, CsvColumnDefinition.ignoreDefinition()).iterator(new StringReader("-1,0,1,2\nv0,v1,v2,v3"));
Tuple2<String, String> tuple = iterator.next();
assertEquals("v1", tuple.first());
assertEquals("v2", tuple.second());
}
use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.
the class CsvWriterBuilderTest method testWriteCsvOnDbObject.
@Test
public void testWriteCsvOnDbObject() throws Exception {
MapperConfig<CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> config = MapperConfig.<CsvColumnKey>fieldMapperConfig();
CsvWriterBuilder<DbObject> builder = CsvWriterBuilder.newBuilder(DbObject.class);
Mapper<DbObject, Appendable> mapper = builder.addColumn("id").addColumn("name").addColumn("email").addColumn("creation_time", new DateFormatProperty("dd/MM/yyyy HH:mm:ss")).addColumn("type_ordinal", new EnumOrdinalFormatProperty()).addColumn("type_name").mapper();
DbObject dbObject = CsvWriterTest.newDbObject();
assertEquals("13,name,email,06/06/2015 17:46:23,1,type3\r\n", mapper.map(dbObject).toString());
dbObject.setEmail("email,e\" ");
assertEquals("13,name,\"email,e\"\" \",06/06/2015 17:46:23,1,type3\r\n", mapper.map(dbObject).toString());
}
use of org.simpleflatmapper.csv.CsvColumnKey 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;
}
Aggregations