码迷,mamicode.com
首页 > 其他好文 > 详细

Introspector

时间:2014-08-25 14:46:54      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   java   os   io   for   ar   

 1 import java.beans.BeanInfo;
 2 import java.beans.Introspector;
 3 import java.beans.PropertyDescriptor;
 4 import java.lang.reflect.Method;
 5 public class IntrospectorDemo {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         IntrospectorDemo id=new IntrospectorDemo();
10         try {
11             id.test1();
12             id.test2();
13             id.test3();
14             id.test4();
15         } catch (Exception e) {
16             // TODO Auto-generated catch block
17             e.printStackTrace();
18         }
19     }
20     
21 
22     public class Person
23     {
24         public Person(){
25         }
26         public Person(String name,int age){
27             this.name=name;
28             this.age=age;
29         }
30         private String name;
31         private int age;
32         public String getName() {
33             return name;
34         }
35         public void setName(String name) {
36             this.name = name;
37         }
38         public int getAge() {
39             return age;
40         }
41         public void setAge(int age) {
42             this.age = age;
43         }
44         
45     }
46      
47         //获得person类的所有bean属性
48          
49         public void test1() throws Exception{
50             BeanInfo info = Introspector.getBeanInfo(Person.class);
51             PropertyDescriptor[] pds = info.getPropertyDescriptors();
52             for(PropertyDescriptor pd:pds){
53                 System.out.println(pd.getPropertyType()+" "+pd.getName());
54             }
55         }
56         //获得Person(不包含Object类)的所有bean属性
57          
58         public void test2() throws Exception{
59             BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);
60             PropertyDescriptor[] pds = info.getPropertyDescriptors();
61             for(PropertyDescriptor pd:pds){
62                 System.out.println(pd.getPropertyType()+" "+pd.getName());
63             }
64         }
65         //设置Person的age属性
66          
67         public void test3() throws Exception{
68             Person p = new Person();
69             PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
70             Method method = pd.getWriteMethod();
71             method.invoke(p, 20);
72             System.out.println(p.getAge());
73         }
74         //调用Person的getAge()
75          
76         public void test4() throws Exception{
77             Person p = new Person("xiazdong",30);
78             PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
79             Method method = pd.getReadMethod();
80             System.out.println(method.invoke(p, null));
81         }
82      
83 }

 

Introspector

标签:des   style   blog   color   java   os   io   for   ar   

原文地址:http://www.cnblogs.com/rojas/p/3934751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!