2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / beans / PropertyDescriptor.java
blob21ab360e9821a8e140c1c630e2598a0665f8de80
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.lang.reflect.Method;
43 /**
44 ** PropertyDescriptor describes information about a JavaBean property,
45 ** by which we mean a property that has been exposed via a pair of
46 ** get and set methods. (There may be no get method, which means
47 ** the property is write-only, or no set method, which means the
48 ** the property is read-only.)<P>
50 ** The constraints put on get and set methods are:<P>
51 ** <OL>
52 ** <LI>A get method must have signature
53 ** <CODE>&lt;propertyType&gt; &lt;getMethodName&gt;()</CODE></LI>
54 ** <LI>A set method must have signature
55 ** <CODE>void &lt;setMethodName&gt;(&lt;propertyType&gt;)</CODE></LI>
56 ** <LI>Either method type may throw any exception.</LI>
57 ** <LI>Both methods must be public.</LI>
58 ** </OL>
60 ** @author John Keiser
61 ** @since JDK1.1
62 ** @version 1.1.0, 26 Jul 1998
63 **/
65 public class PropertyDescriptor extends FeatureDescriptor {
66 Class propertyType;
67 Method getMethod;
68 Method setMethod;
70 Class propertyEditorClass;
71 boolean bound;
72 boolean constrained;
74 PropertyDescriptor(String name) {
75 setName(name);
78 /** Create a new PropertyDescriptor by introspection.
79 ** This form of constructor creates the PropertyDescriptor by
80 ** looking for a getter method named <CODE>get&lt;name&gt;()</CODE>
81 ** (or, optionally, if the property is boolean,
82 ** <CODE>is&lt;name&gt;()</CODE>) and
83 ** <CODE>set&lt;name&gt;()</CODE> in class
84 ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
85 ** first letter capitalized by the constructor.<P>
87 ** <B>Implementation note:</B> If there is both are both isXXX and
88 ** getXXX methods, the former is used in preference to the latter.
89 ** We do not check that an isXXX method returns a boolean. In both
90 ** cases, this matches the behaviour of JDK 1.4<P>
92 ** @param name the programmatic name of the property, usually
93 ** starting with a lowercase letter (e.g. fooManChu
94 ** instead of FooManChu).
95 ** @param beanClass the class the get and set methods live in.
96 ** @exception IntrospectionException if the methods are not found
97 ** or invalid.
98 **/
99 public PropertyDescriptor(String name, Class beanClass)
100 throws IntrospectionException
102 setName(name);
103 if (name.length() == 0) {
104 throw new IntrospectionException("empty property name");
106 String caps = Character.toUpperCase(name.charAt(0)) + name.substring(1);
107 findMethods(beanClass, "is" + caps, "get" + caps, "set" + caps);
108 if (getMethod == null) {
109 throw new IntrospectionException("Cannot find an is" + caps +
110 " or get" + caps + " method");
112 if (setMethod == null) {
113 throw new IntrospectionException("Cannot find a " + caps + " method");
115 checkMethods();
118 /** Create a new PropertyDescriptor by introspection.
119 ** This form of constructor allows you to specify the
120 ** names of the get and set methods to search for.<P>
122 ** <B>Implementation note:</B> If there is a get method (or
123 ** boolean isXXX() method), then the return type of that method
124 ** is used to find the set method. If there is no get method,
125 ** then the set method is searched for exhaustively.<P>
127 ** <B>Spec note:</B>
128 ** If there is no get method and multiple set methods with
129 ** the same name and a single parameter (different type of course),
130 ** then an IntrospectionException is thrown. While Sun's spec
131 ** does not state this, it can make Bean behavior different on
132 ** different systems (since method order is not guaranteed) and as
133 ** such, can be treated as a bug in the spec. I am not aware of
134 ** whether Sun's implementation catches this.
136 ** @param name the programmatic name of the property, usually
137 ** starting with a lowercase letter (e.g. fooManChu
138 ** instead of FooManChu).
139 ** @param beanClass the class the get and set methods live in.
140 ** @param getMethodName the name of the get method.
141 ** @param setMethodName the name of the set method.
142 ** @exception IntrospectionException if the methods are not found
143 ** or invalid.
145 public PropertyDescriptor(String name, Class beanClass,
146 String getMethodName, String setMethodName)
147 throws IntrospectionException
149 setName(name);
150 findMethods(beanClass, getMethodName, null, setMethodName);
151 if (getMethod == null && getMethodName != null) {
152 throw new IntrospectionException("Cannot find a getter method called " +
153 getMethodName);
155 if (setMethod == null && setMethodName != null) {
156 throw new IntrospectionException("Cannot find a setter method called " +
157 setMethodName);
159 checkMethods();
162 /** Create a new PropertyDescriptor using explicit Methods.
163 ** Note that the methods will be checked for conformance to standard
164 ** Property method rules, as described above at the top of this class.
166 ** @param name the programmatic name of the property, usually
167 ** starting with a lowercase letter (e.g. fooManChu
168 ** instead of FooManChu).
169 ** @param getMethod the get method.
170 ** @param setMethod the set method.
171 ** @exception IntrospectionException if the methods are not found
172 ** or invalid.
174 public PropertyDescriptor(String name, Method getMethod, Method setMethod)
175 throws IntrospectionException
177 setName(name);
178 this.getMethod = getMethod;
179 this.setMethod = setMethod;
180 if (getMethod != null) {
181 this.propertyType = getMethod.getReturnType();
183 else if (setMethod != null) {
184 this.propertyType = setMethod.getParameterTypes()[0];
186 checkMethods();
189 /** Get the property type.
190 ** This is the type the get method returns and the set method
191 ** takes in.
193 public Class getPropertyType() {
194 return propertyType;
197 /** Get the get method. Why they call it readMethod here and
198 ** get everywhere else is beyond me.
200 public Method getReadMethod() {
201 return getMethod;
204 /** Get the set method. Why they call it writeMethod here and
205 ** set everywhere else is beyond me.
207 public Method getWriteMethod() {
208 return setMethod;
211 /** Get whether the property is bound. Defaults to false. **/
212 public boolean isBound() {
213 return bound;
216 /** Set whether the property is bound.
217 ** As long as the the bean implements addPropertyChangeListener() and
218 ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
219 ** If these things are not true, then the behavior of the system
220 ** will be undefined.<P>
222 ** When a property is bound, its set method is required to fire the
223 ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
224 ** after the value has changed.
225 ** @param bound whether the property is bound or not.
227 public void setBound(boolean bound) {
228 this.bound = bound;
231 /** Get whether the property is constrained. Defaults to false. **/
232 public boolean isConstrained() {
233 return constrained;
236 /** Set whether the property is constrained.
237 ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
238 ** (or subclass thereof) and the bean implements addVetoableChangeListener()
239 ** and removeVetoableChangeListener(), then setConstrained(true) may safely
240 ** be called. Otherwise, the system behavior is undefined.
241 ** <B>Spec note:</B> given those strict parameters, it would be nice if it
242 ** got set automatically by detection, but oh well.<P>
243 ** When a property is constrained, its set method is required to:<P>
244 ** <OL>
245 ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
246 ** event notifying others of the change and allowing them a chance to
247 ** say it is a bad thing.</LI>
248 ** <LI>If any of the listeners throws a PropertyVetoException, then
249 ** it must fire another vetoableChange() event notifying the others
250 ** of a reversion to the old value (though, of course, the change
251 ** was never made). Then it rethrows the PropertyVetoException and
252 ** exits.</LI>
253 ** <LI>If all has gone well to this point, the value may be changed.</LI>
254 ** </OL>
255 ** @param constrained whether the property is constrained or not.
257 public void setConstrained(boolean constrained) {
258 this.constrained = constrained;
261 /** Get the PropertyEditor class. Defaults to null. **/
262 public Class getPropertyEditorClass() {
263 return propertyEditorClass;
266 /** Set the PropertyEditor class. If the class does not implement
267 ** the PropertyEditor interface, you will likely get an exception
268 ** late in the game.
269 ** @param propertyEditorClass the PropertyEditor class for this
270 ** class to use.
272 public void setPropertyEditorClass(Class propertyEditorClass) {
273 this.propertyEditorClass = propertyEditorClass;
276 private void findMethods(Class beanClass, String getMethodName1,
277 String getMethodName2, String setMethodName)
278 throws IntrospectionException
280 try {
281 // Try the first get method name
282 if (getMethodName1 != null) {
283 try {
284 getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
286 catch (NoSuchMethodException e) {
290 // Fall back to the second get method name
291 if (getMethod == null && getMethodName2 != null) {
292 try {
293 getMethod = beanClass.getMethod(getMethodName2, new Class[0]);
295 catch (NoSuchMethodException e) {
299 // Try the set method name
300 if (setMethodName != null) {
301 if (getMethod != null) {
302 // If there is a get method, use its return type to help
303 // select the corresponding set method.
304 Class propertyType = getMethod.getReturnType();
305 if (propertyType == Void.TYPE) {
306 String msg = "The property's read method has return type 'void'";
307 throw new IntrospectionException(msg);
310 Class[] setArgs = new Class[]{propertyType};
311 try {
312 setMethod = beanClass.getMethod(setMethodName, setArgs);
314 catch (NoSuchMethodException e) {
317 else if (getMethodName1 == null && getMethodName2 == null) {
318 // If this is a write-only property, choose the first set method
319 // with the required name, one parameter and return type 'void'
320 Method[] methods = beanClass.getMethods();
321 for (int i = 0; i < methods.length; i++) {
322 if (methods[i].getName().equals(setMethodName) &&
323 methods[i].getParameterTypes().length == 1 &&
324 methods[i].getReturnType() == Void.TYPE) {
325 setMethod = methods[i];
326 break;
332 catch (SecurityException e) {
333 // FIXME -- shouldn't we just allow SecurityException to propagate?
334 String msg = "SecurityException thrown on attempt to access methods.";
335 throw new IntrospectionException(msg);
339 private void checkMethods()
340 throws IntrospectionException
342 if (getMethod != null) {
343 if (getMethod.getParameterTypes().length > 0) {
344 throw new IntrospectionException("get method has parameters");
346 this.propertyType = getMethod.getReturnType();
347 if (propertyType == Void.TYPE) {
348 throw new IntrospectionException("get method has void return type");
351 if (setMethod != null) {
352 if (setMethod.getParameterTypes().length != 1) {
353 String msg = "set method does not have exactly one parameter";
354 throw new IntrospectionException(msg);
356 if (getMethod == null) {
357 propertyType = setMethod.getParameterTypes()[0];
359 else {
360 if (!propertyType.equals(setMethod.getParameterTypes()[0])) {
361 String msg = "set and get methods do not share the same type";
362 throw new IntrospectionException(msg);
364 if ((!getMethod.getDeclaringClass().
365 isAssignableFrom(setMethod.getDeclaringClass())) &&
366 (!setMethod.getDeclaringClass().
367 isAssignableFrom(getMethod.getDeclaringClass()))) {
368 String msg = "set and get methods are not in the same class.";
369 throw new IntrospectionException(msg);