由于没钱下载javaassist工具包,官网又打不开。所以没做实操。
来看看老师做的:
package com.bjsxt.test; import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtClass; import javassist.CtConstructor; import javassist.CtField; import javassist.CtMethod; /** * 测试使用javassist生成一个新的类 * @author 尚学堂高淇 www.sxt.cn * */ public class Demo01 { public static void main(String[] args) throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.makeClass("com.bjsxt.bean.Emp"); //创建属性 CtField f1 = CtField.make("private int empno;", cc); CtField f2 = CtField.make("private String ename;", cc); cc.addField(f1); cc.addField(f2); //创建方法 CtMethod m1 = CtMethod.make("public int getEmpno(){return empno;}", cc); CtMethod m2 = CtMethod.make("public void setEmpno(int empno){this.empno=empno;}", cc); cc.addMethod(m1); cc.addMethod(m2); //添加构造器 CtConstructor constructor = new CtConstructor(new CtClass[]{CtClass.intType,pool.get("java.lang.String")}, cc); constructor.setBody("{this.empno=empno; this.ename=ename;}"); cc.addConstructor(constructor); cc.writeFile("c:/myjava"); //将上面构造好的类写入到c:/myjava中 System.out.println("生成类,成功!"); } }