Created
October 2, 2016 14:05
-
-
Save smieras/f4e73959c132241cc93984c949ab3d14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.mieras.sander.sample; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.List; | |
import com.univocity.parsers.common.processor.BeanListProcessor; | |
import com.univocity.parsers.csv.CsvParser; | |
import com.univocity.parsers.csv.CsvParserSettings; | |
import javassist.CannotCompileException; | |
import javassist.ClassPool; | |
import javassist.CtClass; | |
import javassist.CtConstructor; | |
import javassist.CtField; | |
import javassist.CtMethod; | |
import javassist.CtNewConstructor; | |
import javassist.CtNewMethod; | |
import javassist.NotFoundException; | |
import javassist.bytecode.AnnotationsAttribute; | |
import javassist.bytecode.ClassFile; | |
import javassist.bytecode.ConstPool; | |
import javassist.bytecode.annotation.Annotation; | |
public class Example { | |
public interface Domain { | |
Integer getIdentifier(); | |
Object getColumnByIndex(int i); | |
} | |
public static void main(final String[] args) throws Exception { | |
final Class<?> dynamicClass = generateDomain(); | |
parseBean(dynamicClass); | |
} | |
private static Class<?> generateDomain() throws NotFoundException, CannotCompileException, Exception, IOException, | |
InstantiationException, IllegalAccessException { | |
final ClassPool pool = ClassPool.getDefault(); | |
final CtClass cc = pool.makeClass("Person"); | |
cc.addInterface(resolveCtClass(Domain.class)); | |
final CtConstructor constr = CtNewConstructor.defaultConstructor(cc); | |
cc.addConstructor(constr); | |
final CtField idField = new CtField(resolveCtClass(Integer.class), "id", cc); | |
final CtMethod idGetter = CtNewMethod.getter("getId", idField); | |
final CtMethod idSetter = CtNewMethod.setter("setId", idField); | |
cc.addField(idField); | |
cc.addMethod(idGetter); | |
cc.addMethod(idSetter); | |
addAnnotation(cc, "id", "Parsed(index=0)"); | |
final CtField firstNameField = new CtField(resolveCtClass(String.class), "firstName", cc); | |
final CtMethod firstNameGetter = CtNewMethod.getter("getFirstName", firstNameField); | |
final CtMethod firstNameSetter = CtNewMethod.setter("setFirstName", firstNameField); | |
cc.addField(firstNameField); | |
cc.addMethod(firstNameSetter); | |
cc.addMethod(firstNameGetter); | |
addAnnotation(cc, "firstName", "Parsed(index=1)"); | |
final CtMethod getIdentifier = CtNewMethod.make("public Integer getIdentifier () { return id; }", cc); | |
cc.addMethod(getIdentifier); | |
final CtMethod toString = CtNewMethod.make("public String toString() {" | |
+ "return id.toString() + \"|\" + firstName;" | |
+ "}", cc); | |
cc.addMethod(toString); | |
final CtMethod getColumnByIndex = CtNewMethod.make( | |
"public Object getColumnByIndex(int i) {" | |
+ "switch (i) {" | |
+ "case 0:" | |
+ "return id;" | |
+ "case 1:" | |
+ "return firstName;" | |
+ "default: " | |
+ "throw new IllegalArgumentException(\"Tried getting column index i, but this column index does not exist\");" | |
+ "}" | |
+ "}" | |
, cc); | |
cc.addMethod(getColumnByIndex); | |
cc.writeFile(); | |
return cc.toClass(); | |
} | |
private static CtClass resolveCtClass(final Class<?> clazz) throws NotFoundException { | |
final ClassPool pool = ClassPool.getDefault(); | |
return pool.get(clazz.getName()); | |
} | |
private static void addAnnotation(final CtClass clazz, final String fieldName, final String annotationName) throws Exception { | |
final ClassFile cfile = clazz.getClassFile(); | |
final ConstPool cpool = cfile.getConstPool(); | |
final CtField cfield = clazz.getField(fieldName); | |
final AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag); | |
final Annotation annot = new Annotation(annotationName, cpool); | |
attr.addAnnotation(annot); | |
cfield.getFieldInfo().addAttribute(attr); | |
} | |
private static void parseBean(final Class<?> dynamicClass) throws FileNotFoundException { | |
@SuppressWarnings("unchecked") | |
final BeanListProcessor<?> rowProcessor = new BeanListProcessor<Class<?>>((Class<Class<?>>) dynamicClass); | |
final CsvParserSettings parserSettings = new CsvParserSettings(); | |
parserSettings.setProcessor(rowProcessor); | |
parserSettings.setHeaderExtractionEnabled(false); | |
parserSettings.getFormat().setDelimiter('|'); | |
parserSettings.setEmptyValue(""); | |
parserSettings.setNullValue(""); | |
final CsvParser parser = new CsvParser(parserSettings); | |
parser.parse(new FileReader("src/main/resources/person.csv")); | |
final List<?> beans = rowProcessor.getBeans(); | |
for (final Object domain : beans) { | |
final Domain domainImpl = (Domain) domain; | |
System.out.println("Person id is: " + domainImpl.getIdentifier()); | |
System.out.println("Person name is: " + domainImpl.getColumnByIndex(1)); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment