zoukankan      html  css  js  c++  java
  • 使用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());
    		}
    	}


  • 相关阅读:
    std::bind常见的坑
    valgrind使用指南
    Makefile (1) gcc基础
    准备篇(三)Makefile
    win10激活
    C语言结构体篇 结构体
    C语言字符篇(五)内存函数
    C语言字符篇(四)字符串查找函数
    C语言字符篇(三)字符串比较函数
    C语言字符篇(二)字符串处理函数
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3138894.html
Copyright © 2011-2022 走看看