使用javassist框架进行动态的更改Class类

@Test

	public  void changeClass() throws Exception {

		

		ClassPool pool = ClassPool.getDefault();

		//获取一个Student类的CtClass对象

		CtClass ctClass = pool.get("com.javassist.domain.Student");

		

		//为ctClass设置一个父类

		ctClass.setSuperclass(pool.get("com.javassist.domain.Person"));

		//为cTclass对象添加一个属性name

		ctClass.addField(CtField.make("private String name;", ctClass));

		ctClass.addMethod(CtMethod.make("public void setName(String name){this.name = name;}", ctClass));

		ctClass.addMethod(CtMethod.make("public String getName(){return this.name;}", ctClass));

		

		//获取ctClass对象对应的Class对象student

		Class student = ctClass.toClass();

		//对student类进行内省,得到对象BeanInfo

		BeanInfo beanInfo = Introspector.getBeanInfo(student, Object.class);

		//获取beanInfo的方法描述对象

		MethodDescriptor[] descriptors = beanInfo.getMethodDescriptors();

		

		for (int i = 0; i < descriptors.length; i++) {

			System.out.println(descriptors[i].getName());

		}

		System.out.println(descriptors.length);

		

		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

		for (int i = 0; i < propertyDescriptors.length; i++) {

			System.out.println(propertyDescriptors[i].getDisplayName());

		}

	}


你可能感兴趣的:(javassist)