FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / beans / PropertyDescriptor.java
blob00db4166ced79289bc713ec01f4e4b3a7e4b239e
1 /* java.beans.PropertyDescriptor
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package java.beans;
41 import java.util.*;
42 import java.lang.reflect.*;
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>
52 ** The constraints put on get and set methods are:<P>
53 ** <OL>
54 ** <LI>A get method must have signature
55 ** <CODE>&lt;propertyType&gt; &lt;getMethodName&gt;()</CODE></LI>
56 ** <LI>A set method must have signature
57 ** <CODE>void &lt;setMethodName&gt;(&lt;propertyType&gt;)</CODE></LI>
58 ** <LI>Either method type may throw any exception.</LI>
59 ** <LI>Both methods must be public.</LI>
60 ** </OL>
62 ** @author John Keiser
63 ** @since JDK1.1
64 ** @version 1.1.0, 26 Jul 1998
65 **/
67 public class PropertyDescriptor extends FeatureDescriptor {
68 Class propertyType;
69 Method getMethod;
70 Method setMethod;
72 Class propertyEditorClass;
73 boolean bound;
74 boolean constrained;
76 PropertyDescriptor(String name) {
77 setName(name);
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&lt;name&gt;()</CODE>
83 ** (or, optionally, if the property is boolean,
84 ** <CODE>is&lt;name&gt;()</CODE>) and
85 ** <CODE>set&lt;name&gt;()</CODE> in class
86 ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
87 ** first letter capitalized by the constructor.<P>
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>
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.
101 public PropertyDescriptor(String name, Class beanClass)
102 throws IntrospectionException
104 setName(name);
105 if (name.length() == 0) {
106 throw new IntrospectionException("empty property name");
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");
114 if (setMethod == null) {
115 throw new IntrospectionException("Cannot find a " + caps + " method");
117 checkMethods();
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>
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>
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.
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.
147 public PropertyDescriptor(String name, Class beanClass,
148 String getMethodName, String setMethodName)
149 throws IntrospectionException
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);
157 if (setMethod == null && setMethodName != null) {
158 throw new IntrospectionException("Cannot find a setter method called " +
159 setMethodName);
161 checkMethods();
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.
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.
176 public PropertyDescriptor(String name, Method getMethod, Method setMethod)
177 throws IntrospectionException
179 setName(name);
180 this.getMethod = getMethod;
181 this.setMethod = setMethod;
182 if (getMethod != null) {
183 this.propertyType = getMethod.getReturnType();
185 else if (setMethod != null) {
186 this.propertyType = setMethod.getParameterTypes()[0];
188 checkMethods();
191 /** Get the property type.
192 ** This is the type the get method returns and the set method
193 ** takes in.
195 public Class getPropertyType() {
196 return propertyType;
199 /** Get the get method. Why they call it readMethod here and
200 ** get everywhere else is beyond me.
202 public Method getReadMethod() {
203 return getMethod;
206 /** Get the set method. Why they call it writeMethod here and
207 ** set everywhere else is beyond me.
209 public Method getWriteMethod() {
210 return setMethod;
213 /** Get whether the property is bound. Defaults to false. **/
214 public boolean isBound() {
215 return bound;
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>
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.
229 public void setBound(boolean bound) {
230 this.bound = bound;
233 /** Get whether the property is constrained. Defaults to false. **/
234 public boolean isConstrained() {
235 return constrained;
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.
259 public void setConstrained(boolean constrained) {
260 this.constrained = constrained;
263 /** Get the PropertyEditor class. Defaults to null. **/
264 public Class getPropertyEditorClass() {
265 return propertyEditorClass;
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.
274 public void setPropertyEditorClass(Class propertyEditorClass) {
275 this.propertyEditorClass = propertyEditorClass;
278 private void findMethods(Class beanClass, String getMethodName1,
279 String getMethodName2, String setMethodName)
280 throws IntrospectionException
282 try {
283 // Try the first get method name
284 if (getMethodName1 != null) {
285 try {
286 getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
288 catch (NoSuchMethodException e) {
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]);
297 catch (NoSuchMethodException e) {
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);
312 Class[] setArgs = new Class[]{propertyType};
313 try {
314 setMethod = beanClass.getMethod(setMethodName, setArgs);
316 catch (NoSuchMethodException e) {
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;
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);
341 private void checkMethods()
342 throws IntrospectionException
344 if (getMethod != null) {
345 if (getMethod.getParameterTypes().length > 0) {
346 throw new IntrospectionException("get method has parameters");
348 this.propertyType = getMethod.getReturnType();
349 if (propertyType == Void.TYPE) {
350 throw new IntrospectionException("get method has void return type");
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);
358 if (getMethod == null) {
359 propertyType = setMethod.getParameterTypes()[0];
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);
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);