Javassit初探

官网/参考: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/ 

https://github.com/jboss-javassist/javassist

http://www.ibm.com/developerworks/cn/java/coretech/java-dynamic.html

 

ClassPool classPool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(SpringContextHolder.class);
classPool.insertClassPath(classPath);

CtClass strCtClass = classPool.get("java.lang.String");
for (Element ele : typeAliases) {
	Attribute attribute = ele.attribute("type");
	String className = attribute.getValue();
	if (className.endsWith("Dto")) {
		CtClass ctClass = classPool.get(className);
		CtField ctField = new CtField(strCtClass, ShardUtils.SHARD_TABLE_VARIABLE_NAME, ctClass);
		ctField.setModifiers(Modifier.PUBLIC);
		ctClass.addField(ctField);
		ctClass.toClass();
	}
}

 

你可能感兴趣的:(java)