author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 466 | 6acd5ec503a8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
466
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
24 |
import java.beans.BeanDescriptor; |
2 | 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 |
*/ |
|
39 |
private BeanUtils() { |
|
40 |
} |
|
41 |
||
42 |
/** |
|
466
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
43 |
* Returns a bean descriptor for specified class. |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
44 |
* |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
45 |
* @param type the class to introspect |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
46 |
* @return a bean descriptor |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
47 |
*/ |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
48 |
public static BeanDescriptor getBeanDescriptor(Class type) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
49 |
try { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
50 |
return Introspector.getBeanInfo(type).getBeanDescriptor(); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
51 |
} catch (IntrospectionException exception) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
52 |
throw new Error("unexpected exception", exception); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
53 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
54 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
55 |
|
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
56 |
/** |
2 | 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 |
*/ |
|
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 |
/** |
|
466
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
71 |
* Returns an array of event set descriptors for specified class. |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
72 |
* |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
73 |
* @param type the class to introspect |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
74 |
* @return an array of event set descriptors |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
75 |
*/ |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
76 |
public static EventSetDescriptor[] getEventSetDescriptors(Class type) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
77 |
try { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
78 |
return Introspector.getBeanInfo(type).getEventSetDescriptors(); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
79 |
} catch (IntrospectionException exception) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
80 |
throw new Error("unexpected exception", exception); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
81 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
82 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
83 |
|
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
84 |
/** |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
85 |
* Finds an event set descriptor for the class |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
86 |
* that matches the event set name. |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
87 |
* |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
88 |
* @param type the class to introspect |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
89 |
* @param name the name of the event set to search |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
90 |
* @return the {@code EventSetDescriptor} or {@code null} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
91 |
*/ |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
92 |
public static EventSetDescriptor findEventSetDescriptor(Class type, String name) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
93 |
EventSetDescriptor[] esds = getEventSetDescriptors(type); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
94 |
for (EventSetDescriptor esd : esds) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
95 |
if (esd.getName().equals(name)) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
96 |
return esd; |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
97 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
98 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
99 |
return null; |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
100 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
101 |
|
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
102 |
/** |
2 | 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 |
*/ |
|
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 |
/** |
|
466
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
121 |
* Returns a event set descriptor for the class |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
122 |
* that matches the property name. |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
123 |
* |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
124 |
* @param type the class to introspect |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
125 |
* @param name the name of the event set to search |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
126 |
* @return the {@code EventSetDescriptor} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
127 |
*/ |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
128 |
public static EventSetDescriptor getEventSetDescriptor(Class type, String name) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
129 |
EventSetDescriptor esd = findEventSetDescriptor(type, name); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
130 |
if (esd != null) { |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
131 |
return esd; |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
132 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
133 |
throw new Error("could not find event set '" + name + "' in " + type); |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
134 |
} |
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
135 |
|
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
136 |
/** |
2 | 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 |
*/ |
|
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 |
*/ |
|
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 |
*/ |
|
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 |
*/ |
|
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 |
*/ |
|
199 |
public static void reportMethodDescriptor(MethodDescriptor md) { |
|
200 |
System.out.println("method name: " + md.getName()); |
|
201 |
System.out.println(" method: " + md.getMethod()); |
|
202 |
} |
|
203 |
} |