1 | /* java.beans.PropertyDescriptor
|
---|
2 | Copyright (C) 1998, 2001 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.beans;
|
---|
40 |
|
---|
41 | import java.util.*;
|
---|
42 | import java.lang.reflect.*;
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | ** PropertyDescriptor describes information about a JavaBean property,
|
---|
47 | ** by which we mean a property that has been exposed via a pair of
|
---|
48 | ** get and set methods. (There may be no get method, which means
|
---|
49 | ** the property is write-only, or no set method, which means the
|
---|
50 | ** the property is read-only.)<P>
|
---|
51 | **
|
---|
52 | ** The constraints put on get and set methods are:<P>
|
---|
53 | ** <OL>
|
---|
54 | ** <LI>A get method must have signature
|
---|
55 | ** <CODE><propertyType> <getMethodName>()</CODE></LI>
|
---|
56 | ** <LI>A set method must have signature
|
---|
57 | ** <CODE>void <setMethodName>(<propertyType>)</CODE></LI>
|
---|
58 | ** <LI>Either method type may throw any exception.</LI>
|
---|
59 | ** <LI>Both methods must be public.</LI>
|
---|
60 | ** </OL>
|
---|
61 | **
|
---|
62 | ** @author John Keiser
|
---|
63 | ** @since JDK1.1
|
---|
64 | ** @version 1.1.0, 26 Jul 1998
|
---|
65 | **/
|
---|
66 |
|
---|
67 | public class PropertyDescriptor extends FeatureDescriptor {
|
---|
68 | Class propertyType;
|
---|
69 | Method getMethod;
|
---|
70 | Method setMethod;
|
---|
71 |
|
---|
72 | Class propertyEditorClass;
|
---|
73 | boolean bound;
|
---|
74 | boolean constrained;
|
---|
75 |
|
---|
76 | PropertyDescriptor(String name) {
|
---|
77 | setName(name);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** Create a new PropertyDescriptor by introspection.
|
---|
81 | ** This form of constructor creates the PropertyDescriptor by
|
---|
82 | ** looking for a getter method named <CODE>get<name>()</CODE>
|
---|
83 | ** (or, optionally, if the property is boolean,
|
---|
84 | ** <CODE>is<name>()</CODE>) and
|
---|
85 | ** <CODE>set<name>()</CODE> in class
|
---|
86 | ** <CODE><beanClass></CODE>, where <name> has its
|
---|
87 | ** first letter capitalized by the constructor.<P>
|
---|
88 | **
|
---|
89 | ** <B>Implementation note:</B> If there is both are both isXXX and
|
---|
90 | ** getXXX methods, the former is used in preference to the latter.
|
---|
91 | ** We do not check that an isXXX method returns a boolean. In both
|
---|
92 | ** cases, this matches the behaviour of JDK 1.4<P>
|
---|
93 | **
|
---|
94 | ** @param name the programmatic name of the property, usually
|
---|
95 | ** starting with a lowercase letter (e.g. fooManChu
|
---|
96 | ** instead of FooManChu).
|
---|
97 | ** @param beanClass the class the get and set methods live in.
|
---|
98 | ** @exception IntrospectionException if the methods are not found
|
---|
99 | ** or invalid.
|
---|
100 | **/
|
---|
101 | public PropertyDescriptor(String name, Class beanClass)
|
---|
102 | throws IntrospectionException
|
---|
103 | {
|
---|
104 | setName(name);
|
---|
105 | if (name.length() == 0) {
|
---|
106 | throw new IntrospectionException("empty property name");
|
---|
107 | }
|
---|
108 | String caps = Character.toUpperCase(name.charAt(0)) + name.substring(1);
|
---|
109 | findMethods(beanClass, "is" + caps, "get" + caps, "set" + caps);
|
---|
110 | if (getMethod == null) {
|
---|
111 | throw new IntrospectionException("Cannot find an is" + caps +
|
---|
112 | " or get" + caps + " method");
|
---|
113 | }
|
---|
114 | if (setMethod == null) {
|
---|
115 | throw new IntrospectionException("Cannot find a " + caps + " method");
|
---|
116 | }
|
---|
117 | checkMethods();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Create a new PropertyDescriptor by introspection.
|
---|
121 | ** This form of constructor allows you to specify the
|
---|
122 | ** names of the get and set methods to search for.<P>
|
---|
123 | **
|
---|
124 | ** <B>Implementation note:</B> If there is a get method (or
|
---|
125 | ** boolean isXXX() method), then the return type of that method
|
---|
126 | ** is used to find the set method. If there is no get method,
|
---|
127 | ** then the set method is searched for exhaustively.<P>
|
---|
128 | **
|
---|
129 | ** <B>Spec note:</B>
|
---|
130 | ** If there is no get method and multiple set methods with
|
---|
131 | ** the same name and a single parameter (different type of course),
|
---|
132 | ** then an IntrospectionException is thrown. While Sun's spec
|
---|
133 | ** does not state this, it can make Bean behavior different on
|
---|
134 | ** different systems (since method order is not guaranteed) and as
|
---|
135 | ** such, can be treated as a bug in the spec. I am not aware of
|
---|
136 | ** whether Sun's implementation catches this.
|
---|
137 | **
|
---|
138 | ** @param name the programmatic name of the property, usually
|
---|
139 | ** starting with a lowercase letter (e.g. fooManChu
|
---|
140 | ** instead of FooManChu).
|
---|
141 | ** @param beanClass the class the get and set methods live in.
|
---|
142 | ** @param getMethodName the name of the get method.
|
---|
143 | ** @param setMethodName the name of the set method.
|
---|
144 | ** @exception IntrospectionException if the methods are not found
|
---|
145 | ** or invalid.
|
---|
146 | **/
|
---|
147 | public PropertyDescriptor(String name, Class beanClass,
|
---|
148 | String getMethodName, String setMethodName)
|
---|
149 | throws IntrospectionException
|
---|
150 | {
|
---|
151 | setName(name);
|
---|
152 | findMethods(beanClass, getMethodName, null, setMethodName);
|
---|
153 | if (getMethod == null && getMethodName != null) {
|
---|
154 | throw new IntrospectionException("Cannot find a getter method called " +
|
---|
155 | getMethodName);
|
---|
156 | }
|
---|
157 | if (setMethod == null && setMethodName != null) {
|
---|
158 | throw new IntrospectionException("Cannot find a setter method called " +
|
---|
159 | setMethodName);
|
---|
160 | }
|
---|
161 | checkMethods();
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Create a new PropertyDescriptor using explicit Methods.
|
---|
165 | ** Note that the methods will be checked for conformance to standard
|
---|
166 | ** Property method rules, as described above at the top of this class.
|
---|
167 | **
|
---|
168 | ** @param name the programmatic name of the property, usually
|
---|
169 | ** starting with a lowercase letter (e.g. fooManChu
|
---|
170 | ** instead of FooManChu).
|
---|
171 | ** @param getMethod the get method.
|
---|
172 | ** @param setMethod the set method.
|
---|
173 | ** @exception IntrospectionException if the methods are not found
|
---|
174 | ** or invalid.
|
---|
175 | **/
|
---|
176 | public PropertyDescriptor(String name, Method getMethod, Method setMethod)
|
---|
177 | throws IntrospectionException
|
---|
178 | {
|
---|
179 | setName(name);
|
---|
180 | this.getMethod = getMethod;
|
---|
181 | this.setMethod = setMethod;
|
---|
182 | if (getMethod != null) {
|
---|
183 | this.propertyType = getMethod.getReturnType();
|
---|
184 | }
|
---|
185 | else if (setMethod != null) {
|
---|
186 | this.propertyType = setMethod.getParameterTypes()[0];
|
---|
187 | }
|
---|
188 | checkMethods();
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Get the property type.
|
---|
192 | ** This is the type the get method returns and the set method
|
---|
193 | ** takes in.
|
---|
194 | **/
|
---|
195 | public Class getPropertyType() {
|
---|
196 | return propertyType;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** Get the get method. Why they call it readMethod here and
|
---|
200 | ** get everywhere else is beyond me.
|
---|
201 | **/
|
---|
202 | public Method getReadMethod() {
|
---|
203 | return getMethod;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** Get the set method. Why they call it writeMethod here and
|
---|
207 | ** set everywhere else is beyond me.
|
---|
208 | **/
|
---|
209 | public Method getWriteMethod() {
|
---|
210 | return setMethod;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Get whether the property is bound. Defaults to false. **/
|
---|
214 | public boolean isBound() {
|
---|
215 | return bound;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** Set whether the property is bound.
|
---|
219 | ** As long as the the bean implements addPropertyChangeListener() and
|
---|
220 | ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
|
---|
221 | ** If these things are not true, then the behavior of the system
|
---|
222 | ** will be undefined.<P>
|
---|
223 | **
|
---|
224 | ** When a property is bound, its set method is required to fire the
|
---|
225 | ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
|
---|
226 | ** after the value has changed.
|
---|
227 | ** @param bound whether the property is bound or not.
|
---|
228 | **/
|
---|
229 | public void setBound(boolean bound) {
|
---|
230 | this.bound = bound;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Get whether the property is constrained. Defaults to false. **/
|
---|
234 | public boolean isConstrained() {
|
---|
235 | return constrained;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /** Set whether the property is constrained.
|
---|
239 | ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
|
---|
240 | ** (or subclass thereof) and the bean implements addVetoableChangeListener()
|
---|
241 | ** and removeVetoableChangeListener(), then setConstrained(true) may safely
|
---|
242 | ** be called. Otherwise, the system behavior is undefined.
|
---|
243 | ** <B>Spec note:</B> given those strict parameters, it would be nice if it
|
---|
244 | ** got set automatically by detection, but oh well.<P>
|
---|
245 | ** When a property is constrained, its set method is required to:<P>
|
---|
246 | ** <OL>
|
---|
247 | ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
|
---|
248 | ** event notifying others of the change and allowing them a chance to
|
---|
249 | ** say it is a bad thing.</LI>
|
---|
250 | ** <LI>If any of the listeners throws a PropertyVetoException, then
|
---|
251 | ** it must fire another vetoableChange() event notifying the others
|
---|
252 | ** of a reversion to the old value (though, of course, the change
|
---|
253 | ** was never made). Then it rethrows the PropertyVetoException and
|
---|
254 | ** exits.</LI>
|
---|
255 | ** <LI>If all has gone well to this point, the value may be changed.</LI>
|
---|
256 | ** </OL>
|
---|
257 | ** @param constrained whether the property is constrained or not.
|
---|
258 | **/
|
---|
259 | public void setConstrained(boolean constrained) {
|
---|
260 | this.constrained = constrained;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** Get the PropertyEditor class. Defaults to null. **/
|
---|
264 | public Class getPropertyEditorClass() {
|
---|
265 | return propertyEditorClass;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /** Set the PropertyEditor class. If the class does not implement
|
---|
269 | ** the PropertyEditor interface, you will likely get an exception
|
---|
270 | ** late in the game.
|
---|
271 | ** @param propertyEditorClass the PropertyEditor class for this
|
---|
272 | ** class to use.
|
---|
273 | **/
|
---|
274 | public void setPropertyEditorClass(Class propertyEditorClass) {
|
---|
275 | this.propertyEditorClass = propertyEditorClass;
|
---|
276 | }
|
---|
277 |
|
---|
278 | private void findMethods(Class beanClass, String getMethodName1,
|
---|
279 | String getMethodName2, String setMethodName)
|
---|
280 | throws IntrospectionException
|
---|
281 | {
|
---|
282 | try {
|
---|
283 | // Try the first get method name
|
---|
284 | if (getMethodName1 != null) {
|
---|
285 | try {
|
---|
286 | getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
|
---|
287 | }
|
---|
288 | catch (NoSuchMethodException e) {
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | // Fall back to the second get method name
|
---|
293 | if (getMethod == null && getMethodName2 != null) {
|
---|
294 | try {
|
---|
295 | getMethod = beanClass.getMethod(getMethodName2, new Class[0]);
|
---|
296 | }
|
---|
297 | catch (NoSuchMethodException e) {
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | // Try the set method name
|
---|
302 | if (setMethodName != null) {
|
---|
303 | if (getMethod != null) {
|
---|
304 | // If there is a get method, use its return type to help
|
---|
305 | // select the corresponding set method.
|
---|
306 | Class propertyType = getMethod.getReturnType();
|
---|
307 | if (propertyType == Void.TYPE) {
|
---|
308 | String msg = "The property's read method has return type 'void'";
|
---|
309 | throw new IntrospectionException(msg);
|
---|
310 | }
|
---|
311 |
|
---|
312 | Class[] setArgs = new Class[]{propertyType};
|
---|
313 | try {
|
---|
314 | setMethod = beanClass.getMethod(setMethodName, setArgs);
|
---|
315 | }
|
---|
316 | catch (NoSuchMethodException e) {
|
---|
317 | }
|
---|
318 | }
|
---|
319 | else if (getMethodName1 == null && getMethodName2 == null) {
|
---|
320 | // If this is a write-only property, choose the first set method
|
---|
321 | // with the required name, one parameter and return type 'void'
|
---|
322 | Method[] methods = beanClass.getMethods();
|
---|
323 | for (int i = 0; i < methods.length; i++) {
|
---|
324 | if (methods[i].getName().equals(setMethodName) &&
|
---|
325 | methods[i].getParameterTypes().length == 1 &&
|
---|
326 | methods[i].getReturnType() == Void.TYPE) {
|
---|
327 | setMethod = methods[i];
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 | catch (SecurityException e) {
|
---|
335 | // FIXME -- shouldn't we just allow SecurityException to propagate?
|
---|
336 | String msg = "SecurityException thrown on attempt to access methods.";
|
---|
337 | throw new IntrospectionException(msg);
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | private void checkMethods()
|
---|
342 | throws IntrospectionException
|
---|
343 | {
|
---|
344 | if (getMethod != null) {
|
---|
345 | if (getMethod.getParameterTypes().length > 0) {
|
---|
346 | throw new IntrospectionException("get method has parameters");
|
---|
347 | }
|
---|
348 | this.propertyType = getMethod.getReturnType();
|
---|
349 | if (propertyType == Void.TYPE) {
|
---|
350 | throw new IntrospectionException("get method has void return type");
|
---|
351 | }
|
---|
352 | }
|
---|
353 | if (setMethod != null) {
|
---|
354 | if (setMethod.getParameterTypes().length != 1) {
|
---|
355 | String msg = "set method does not have exactly one parameter";
|
---|
356 | throw new IntrospectionException(msg);
|
---|
357 | }
|
---|
358 | if (getMethod == null) {
|
---|
359 | propertyType = setMethod.getParameterTypes()[0];
|
---|
360 | }
|
---|
361 | else {
|
---|
362 | if (!propertyType.equals(setMethod.getParameterTypes()[0])) {
|
---|
363 | String msg = "set and get methods do not share the same type";
|
---|
364 | throw new IntrospectionException(msg);
|
---|
365 | }
|
---|
366 | if ((!getMethod.getDeclaringClass().
|
---|
367 | isAssignableFrom(setMethod.getDeclaringClass())) &&
|
---|
368 | (!setMethod.getDeclaringClass().
|
---|
369 | isAssignableFrom(getMethod.getDeclaringClass()))) {
|
---|
370 | String msg = "set and get methods are not in the same class.";
|
---|
371 | throw new IntrospectionException(msg);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|