1 /*
2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.beans.BeanDescriptor;
25 import java.beans.EventSetDescriptor;
26 import java.beans.IndexedPropertyDescriptor;
27 import java.beans.IntrospectionException;
28 import java.beans.Introspector;
29 import java.beans.MethodDescriptor;
30 import java.beans.PropertyDescriptor;
31 
32 /**
33  * This class contains utilities useful for JavaBeans regression testing.
34  */
35 public final class BeanUtils {
36     /**
37      * Disables instantiation.
38      */
BeanUtils()39     private BeanUtils() {
40     }
41 
42     /**
43      * Returns a bean descriptor for specified class.
44      *
45      * @param type  the class to introspect
46      * @return a bean descriptor
47      */
getBeanDescriptor(Class type)48     public static BeanDescriptor getBeanDescriptor(Class type) {
49         try {
50             return Introspector.getBeanInfo(type).getBeanDescriptor();
51         } catch (IntrospectionException exception) {
52             throw new Error("unexpected exception", exception);
53         }
54     }
55 
56     /**
57      * Returns an array of property descriptors for specified class.
58      *
59      * @param type  the class to introspect
60      * @return an array of property descriptors
61      */
getPropertyDescriptors(Class type)62     public static PropertyDescriptor[] getPropertyDescriptors(Class type) {
63         try {
64             return Introspector.getBeanInfo(type).getPropertyDescriptors();
65         } catch (IntrospectionException exception) {
66             throw new Error("unexpected exception", exception);
67         }
68     }
69 
70     /**
71      * Returns an array of event set descriptors for specified class.
72      *
73      * @param type  the class to introspect
74      * @return an array of event set descriptors
75      */
getEventSetDescriptors(Class type)76     public static EventSetDescriptor[] getEventSetDescriptors(Class type) {
77         try {
78             return Introspector.getBeanInfo(type).getEventSetDescriptors();
79         } catch (IntrospectionException exception) {
80             throw new Error("unexpected exception", exception);
81         }
82     }
83 
84     /**
85      * Finds an event set descriptor for the class
86      * that matches the event set name.
87      *
88      * @param type  the class to introspect
89      * @param name  the name of the event set to search
90      * @return the {@code EventSetDescriptor} or {@code null}
91      */
findEventSetDescriptor(Class type, String name)92     public static EventSetDescriptor findEventSetDescriptor(Class type, String name) {
93         EventSetDescriptor[] esds = getEventSetDescriptors(type);
94         for (EventSetDescriptor esd : esds) {
95             if (esd.getName().equals(name)) {
96                 return esd;
97             }
98         }
99         return null;
100     }
101 
102     /**
103      * Finds a property descriptor for the class
104      * that matches the property name.
105      *
106      * @param type the class to introspect
107      * @param name the name of the property to search
108      * @return the {@code PropertyDescriptor}, {@code IndexedPropertyDescriptor} or {@code null}
109      */
findPropertyDescriptor(Class type, String name)110     public static PropertyDescriptor findPropertyDescriptor(Class type, String name) {
111         PropertyDescriptor[] pds = getPropertyDescriptors(type);
112         for (PropertyDescriptor pd : pds) {
113             if (pd.getName().equals(name)) {
114                 return pd;
115             }
116         }
117         return null;
118     }
119 
120     /**
121      * Returns a event set descriptor for the class
122      * that matches the property name.
123      *
124      * @param type the class to introspect
125      * @param name the name of the event set to search
126      * @return the {@code EventSetDescriptor}
127      */
getEventSetDescriptor(Class type, String name)128     public static EventSetDescriptor getEventSetDescriptor(Class type, String name) {
129         EventSetDescriptor esd = findEventSetDescriptor(type, name);
130         if (esd != null) {
131             return esd;
132         }
133         throw new Error("could not find event set '" + name + "' in " + type);
134     }
135 
136     /**
137      * Returns a property descriptor for the class
138      * that matches the property name.
139      *
140      * @param type the class to introspect
141      * @param name the name of the property to search
142      * @return the {@code PropertyDescriptor}
143      */
getPropertyDescriptor(Class type, String name)144     public static PropertyDescriptor getPropertyDescriptor(Class type, String name) {
145         PropertyDescriptor pd = findPropertyDescriptor(type, name);
146         if (pd != null) {
147             return pd;
148         }
149         throw new Error("could not find property '" + name + "' in " + type);
150     }
151 
152     /**
153      * Returns an indexed property descriptor for the class
154      * that matches the property name.
155      *
156      * @param type  the class to introspect
157      * @param name  the name of the property to search
158      * @return the {@code IndexedPropertyDescriptor}
159      */
getIndexedPropertyDescriptor(Class type, String name)160     public static IndexedPropertyDescriptor getIndexedPropertyDescriptor(Class type, String name) {
161         PropertyDescriptor pd = findPropertyDescriptor(type, name);
162         if (pd instanceof IndexedPropertyDescriptor) {
163             return (IndexedPropertyDescriptor) pd;
164         }
165         reportPropertyDescriptor(pd);
166         throw new Error("could not find indexed property '" + name + "' in " + type);
167     }
168 
169     /**
170      * Reports all the interesting information in an Indexed/PropertyDescrptor.
171      */
reportPropertyDescriptor(PropertyDescriptor pd)172     public static void reportPropertyDescriptor(PropertyDescriptor pd) {
173         System.out.println("property name:  " + pd.getName());
174         System.out.println("         type:  " + pd.getPropertyType());
175         System.out.println("         read:  " + pd.getReadMethod());
176         System.out.println("         write: " + pd.getWriteMethod());
177         if (pd instanceof IndexedPropertyDescriptor) {
178             IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
179             System.out.println(" indexed type: " + ipd.getIndexedPropertyType());
180             System.out.println(" indexed read: " + ipd.getIndexedReadMethod());
181             System.out.println(" indexed write: " + ipd.getIndexedWriteMethod());
182         }
183     }
184 
185     /**
186      * Reports all the interesting information in an EventSetDescriptor
187      */
reportEventSetDescriptor(EventSetDescriptor esd)188     public static void reportEventSetDescriptor(EventSetDescriptor esd) {
189         System.out.println("event set name:   " + esd.getName());
190         System.out.println(" listener type:   " + esd.getListenerType());
191         System.out.println("   method get:    " + esd.getGetListenerMethod());
192         System.out.println("   method add:    " + esd.getAddListenerMethod());
193         System.out.println("   method remove: " + esd.getRemoveListenerMethod());
194     }
195 
196     /**
197      * Reports all the interesting information in a MethodDescriptor
198      */
reportMethodDescriptor(MethodDescriptor md)199     public static void reportMethodDescriptor(MethodDescriptor md) {
200         System.out.println("method name: " + md.getName());
201         System.out.println("     method: " + md.getMethod());
202     }
203 }
204