内省
2015-10-17 22:46
176 查看
使用内省api操纵bean的属性。
package com.fly.intrespect; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import org.junit.Test; //使用内省api操作bean的属性 public class Demo1 { //得到bean的所有属性 @Test public void test1() throws Exception { //对person类进行内省 BeanInfo info = Introspector.getBeanInfo(Person.class, Object.class); //获取bean所有的属性 PropertyDescriptor[] pds = info.getPropertyDescriptors(); for(PropertyDescriptor pd : pds) { System.out.println(pd.getName()); } } //操纵bean的指定属性:age @Test public void test2() throws Exception { Person p = new Person(); PropertyDescriptor pd = new PropertyDescriptor("age", Person.class); //得到属性的写方法,为属性赋值 Method method = pd.getWriteMethod(); //public void setAge(11); method.invoke(p, 12); //获取属性的值 method = pd.getReadMethod(); //public int getAge() System.out.println(method.invoke(p, null)); } //获取属性的类型 @Test public void test3() throws Exception { Person p = new Person(); PropertyDescriptor pd = new PropertyDescriptor("age", Person.class); System.out.println(pd.getPropertyType()); } }
相关文章推荐
- KVM - 虚拟机迁移
- 黑马程序员_JAVA反射学习
- JAVA枚举单例模式
- Virtualbox 无法加载usb解决办法
- 文章标题
- python输出txt 151017
- ubuntu14.04 安装WPS
- 黑马程序员_DOM方式对xml文档进行CRUD
- ldd pvs dis on solaris 10
- jquery表单属性筛选元素
- KVM - 调整cpu内存、网卡
- Linux Shell 通配符、元字符、转义符使用实例介绍
- 黑马程序员_基于XML的学生成绩管理平台
- C#操作指针
- 面试刷题记录
- 面试刷题记录
- tomcat7.0性能优化-挑战极限完整版
- 黑马程序员_SAX解析XML文档
- C语言中的隐式转换
- Repository、IUnitOfWork