/Users/lyon/j4p/src/classUtils/reflection/IntrospectUtil.java

1    package classUtils.reflection; 
2     
3    import classUtils.delegate.DelegateSynthesizer; 
4     
5    public class IntrospectUtil extends ReflectUtil { 
6        private java.beans.MethodDescriptor methodDescriptors[]; 
7        private java.beans.BeanInfo bi = null; 
8        private java.beans.PropertyDescriptor propertyDescriptors[]; 
9     
10       public IntrospectUtil(Object _o) { 
11           super(_o); 
12           try { 
13               bi = java.beans.Introspector.getBeanInfo(getC()); 
14               methodDescriptors = bi.getMethodDescriptors(); 
15               propertyDescriptors = bi.getPropertyDescriptors(); 
16    
17           } catch (java.beans.IntrospectionException e) { 
18               e.printStackTrace(); 
19           } 
20       } 
21    
22       public static void testIndexedPropertyDescriptors() 
23               throws java.beans.IntrospectionException { 
24           print(getIndexedPropertyDescriptors(new java.util.Date())); 
25       } 
26    
27       public static void print(java.beans.IndexedPropertyDescriptor ipd[]) { 
28           System.out.println("Property Names\tProperty Type"); 
29           for (int i = 0; i < ipd.length; i++) { 
30               System.out.println(ipd[i].getName() 
31                       + 
32                       "\t\t" 
33                       + ipd[i].getPropertyType()); 
34           } 
35       } 
36    
37       public static java.beans.PropertyDescriptor[] getIndexedPropertyDescriptors( 
38               Object o) 
39               throws java.beans.IntrospectionException { 
40           Class c = o.getClass(); 
41           java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(c); 
42           return bi.getPropertyDescriptors(); 
43       } 
44    
45    
46       public static void testPropertyDescriptors() 
47               throws java.beans.IntrospectionException { 
48           print(getPropertyDescriptors(new java.util.Date())); 
49       } 
50    
51       public static void print(java.beans.PropertyDescriptor pd[]) { 
52           System.out.println("Property Names\tProperty Type"); 
53           for (int i = 0; i < pd.length; i++) { 
54               System.out.println(pd[i].getName() 
55                       + 
56                       "\t\t" 
57                       + pd[i].getPropertyType()); 
58           } 
59       } 
60    
61       public static java.beans.PropertyDescriptor[] getPropertyDescriptors( 
62               Object o) 
63               throws java.beans.IntrospectionException { 
64           Class c = o.getClass(); 
65           java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(c); 
66           return bi.getPropertyDescriptors(); 
67       } 
68    
69    
70       public static void testMethodDescriptors() 
71               throws java.beans.IntrospectionException { 
72           print(getMethodDescriptors(new java.util.Date())); 
73       } 
74    
75       public static void print(java.beans.MethodDescriptor md[]) { 
76           System.out.println("Method Names"); 
77           for (int i = 0; i < md.length; i++) { 
78               System.out.print(md[i].getName() + " "); 
79           } 
80       } 
81    
82       public static java.beans.MethodDescriptor[] getMethodDescriptors( 
83               Object o) 
84               throws java.beans.IntrospectionException { 
85           Class c = o.getClass(); 
86           java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(c); 
87           return bi.getMethodDescriptors(); 
88       } 
89    
90    
91       public String getBeanInfo() { 
92           return "BeanInfo:" 
93                   + 
94                   getBeanDescriptorString(bi) + 
95                   "\n" 
96                   + 
97                   bi.getEventSetDescriptors() + 
98                   "\n" 
99                   + bi.getPropertyDescriptors(); 
100      } 
101   
102      // cutils.reflection.IntrospectUtil 
103      public static void main(String args[]) { 
104          try { 
105              testPropertyDescriptors(); 
106          } catch (java.beans.IntrospectionException e) { 
107              e.printStackTrace(); 
108          } 
109      } 
110   
111      public static void testEventSetDescriptors() 
112              throws java.beans.IntrospectionException { 
113          print(getEventSetDescriptors(new javax.swing.JButton())); 
114      } 
115   
116      public static java.beans.EventSetDescriptor[] getEventSetDescriptors( 
117              Object o) 
118              throws java.beans.IntrospectionException { 
119          Class c = o.getClass(); 
120          java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo(c); 
121          return bi.getEventSetDescriptors(); 
122      } 
123   
124   
125      public static void print(java.beans.EventSetDescriptor esd[]) { 
126          System.out.println("\n\tListeners\n\t---------"); 
127          for (int i = 0; i < esd.length; i++) { 
128              System.out.println("(" + 
129                      (i + 1) + 
130                      ")\t" 
131                      + 
132                      esd[i].getName() 
133                      + 
134                      " - " 
135                      + esd[i].getListenerType()); 
136          } 
137      } 
138   
139   
140      public static String getBeanDescriptorString(java.beans.BeanInfo bi) { 
141          java.beans.BeanDescriptor bd = bi.getBeanDescriptor(); 
142          return bd.getBeanClass().toString() 
143                  + 
144                  "\n" 
145                  + 
146                  "name=" + 
147                  bd.getName() 
148                  + 
149                  "\ngetDisplayName=" + 
150                  bd.getDisplayName() 
151                  + 
152                  "\nisHidden=" + 
153                  bd.isHidden() 
154                  + "\ngetShortDescription=" + bd.getShortDescription(); 
155      } 
156   
157      public void print() { 
158          print(getBeanInfo()); 
159      } 
160   
161      public void print(Object o) { 
162          System.out.println(o); 
163      } 
164   
165      public void print(Object o[]) { 
166          for (int i = 0; i < o.length; i++) 
167              System.out.println(o[i]); 
168      } 
169   
170      public static void testIntrospectUtil() { 
171          IntrospectUtil iu = new IntrospectUtil(new java.util.Vector()); 
172          iu.print(); 
173          DelegateSynthesizer ds = new DelegateSynthesizer(); 
174          ds.add(iu.bi); 
175          ds.add(iu.methodDescriptors[0]); 
176          ds.add(iu.propertyDescriptors[0]); 
177          ds.print(); 
178      } 
179  } 
180