OSDN Git Service

Fix comments for doclets
[pf3gnuchains/gcc-fork.git] / libjava / java / beans / PropertyDescriptor.java
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 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
26
27
28 package java.beans;
29
30 import java.util.*;
31 import java.lang.reflect.*;
32
33
34 /**
35  ** PropertyDescriptor describes information about a JavaBean property,
36  ** by which we mean a property that has been exposed via a pair of
37  ** get and set methods.  (There may be no get method, which means
38  ** the property is write-only, or no set method, which means the
39  ** the property is read-only.)<P>
40  **
41  ** The constraints put on get and set methods are:<P>
42  ** <OL>
43  ** <LI>A get method must have signature
44  **     <CODE>&lt;propertyType&gt; &lt;getMethodName&gt;()</CODE></LI>
45  ** <LI>A set method must have signature
46  **     <CODE>void &lt;setMethodName&gt;(&lt;propertyType&gt;)</CODE></LI>
47  ** <LI>Either method type may throw any exception.</LI>
48  ** <LI>Both methods must be public.</LI>
49  ** </OL>
50  **
51  ** @author John Keiser
52  ** @since JDK1.1
53  ** @version 1.1.0, 26 Jul 1998
54  **/
55
56 public class PropertyDescriptor extends FeatureDescriptor {
57         Class propertyType;
58         Method getMethod;
59         Method setMethod;
60
61         Class propertyEditorClass;
62         boolean bound;
63         boolean constrained;
64
65         PropertyDescriptor(String name) {
66                 setName(name);
67         }
68
69         /** Create a new PropertyDescriptor by introspection.
70          ** This form of constructor creates the PropertyDescriptor by
71          ** looking for a getter method named <CODE>get&lt;name&gt;()</CODE>
72          ** (or, optionally, if the property is boolean,
73          ** <CODE>is&lt;name&gt;()</CODE>) and
74          ** <CODE>set&lt;name&gt;()</CODE> in class
75          ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
76          ** first letter capitalized by the constructor.<P>
77          **
78          ** <B>Implementation note:</B> If there is a get method (or
79          ** boolean isXXX() method), then the return type of that method
80          ** is used to find the set method.  If there is no get method,
81          ** then the set method is searched for exhaustively.<P>
82          **
83          ** <B>Spec note:</B>
84          ** If there is no get method and multiple set methods with
85          ** the same name and a single parameter (different type of course),
86          ** then an IntrospectionException is thrown.  While Sun's spec
87          ** does not state this, it can make Bean behavior different on
88          ** different systems (since method order is not guaranteed) and as
89          ** such, can be treated as a bug in the spec.  I am not aware of
90          ** whether Sun's implementation catches this.
91          **
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 or invalid.
97          **/
98         public PropertyDescriptor(String name, Class beanClass) throws IntrospectionException {
99                 setName(name);
100                 String capitalized;
101                 try {
102                         capitalized = Character.toUpperCase(name.charAt(0)) + name.substring(1);
103                 } catch(StringIndexOutOfBoundsException e) {
104                         capitalized = "";
105                 }
106                 findMethods(beanClass, "is" + capitalized, "get" + capitalized, "set" + capitalized);
107         }
108
109         /** Create a new PropertyDescriptor by introspection.
110          ** This form of constructor allows you to specify the
111          ** names of the get and set methods to search for.<P>
112          **
113          ** <B>Implementation note:</B> If there is a get method (or
114          ** boolean isXXX() method), then the return type of that method
115          ** is used to find the set method.  If there is no get method,
116          ** then the set method is searched for exhaustively.<P>
117          **
118          ** <B>Spec note:</B>
119          ** If there is no get method and multiple set methods with
120          ** the same name and a single parameter (different type of course),
121          ** then an IntrospectionException is thrown.  While Sun's spec
122          ** does not state this, it can make Bean behavior different on
123          ** different systems (since method order is not guaranteed) and as
124          ** such, can be treated as a bug in the spec.  I am not aware of
125          ** whether Sun's implementation catches this.
126          **
127          ** @param name the programmatic name of the property, usually
128          **             starting with a lowercase letter (e.g. fooManChu
129          **             instead of FooManChu).
130          ** @param beanClass the class the get and set methods live in.
131          ** @param getMethodName the name of the get method.
132          ** @param setMethodName the name of the set method.
133          ** @exception IntrospectionException if the methods are not found or invalid.
134          **/
135         public PropertyDescriptor(String name, Class beanClass, String getMethodName, String setMethodName) throws IntrospectionException {
136                 setName(name);
137                 findMethods(beanClass, getMethodName, null, setMethodName);
138         }
139
140         /** Create a new PropertyDescriptor using explicit Methods.
141          ** Note that the methods will be checked for conformance to standard
142          ** Property method rules, as described above at the top of this class.
143          ** 
144          ** @param name the programmatic name of the property, usually
145          **             starting with a lowercase letter (e.g. fooManChu
146          **             instead of FooManChu).
147          ** @param getMethod the get method.
148          ** @param setMethod the set method.
149          ** @exception IntrospectionException if the methods are not found or invalid.
150          **/
151         public PropertyDescriptor(String name, Method getMethod, Method setMethod) throws IntrospectionException {
152                 setName(name);
153                 if(getMethod != null && getMethod.getParameterTypes().length > 0) {
154                         throw new IntrospectionException("get method has parameters");
155                 }
156                 if(setMethod != null && setMethod.getParameterTypes().length != 1) {
157                         throw new IntrospectionException("set method does not have exactly one parameter");
158                 }
159                 if(getMethod != null && setMethod != null) {
160                         if(!getMethod.getReturnType().equals(setMethod.getParameterTypes()[0])) {
161                                 throw new IntrospectionException("set and get methods do not share the same type");
162                         }
163                         if(!getMethod.getDeclaringClass().isAssignableFrom(setMethod.getDeclaringClass())
164                            && !setMethod.getDeclaringClass().isAssignableFrom(getMethod.getDeclaringClass())) {
165                                 throw new IntrospectionException("set and get methods are not in the same class.");
166                         }
167                 }
168                 this.getMethod = getMethod;
169                 this.setMethod = setMethod;
170                 if(getMethod != null) {
171                         this.propertyType = getMethod.getReturnType();
172                 } else {
173                         this.propertyType = setMethod.getParameterTypes()[0];
174                 }
175         }
176
177         /** Get the property type.
178          ** This is the type the get method returns and the set method
179          ** takes in.
180          **/
181         public Class getPropertyType() {
182                 return propertyType;
183         }
184
185         /** Get the get method.  Why they call it readMethod here and
186          ** get everywhere else is beyond me.
187          **/
188         public Method getReadMethod() {
189                 return getMethod;
190         }
191
192         /** Get the set method.  Why they call it writeMethod here and
193          ** set everywhere else is beyond me.
194          **/
195         public Method getWriteMethod() {
196                 return setMethod;
197         }
198
199         /** Get whether the property is bound.  Defaults to false. **/
200         public boolean isBound() {
201                 return bound;
202         }
203
204         /** Set whether the property is bound.
205          ** As long as the the bean implements addPropertyChangeListener() and
206          ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
207          ** If these things are not true, then the behavior of the system
208          ** will be undefined.<P>
209          **
210          ** When a property is bound, its set method is required to fire the
211          ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
212          ** after the value has changed.
213          ** @param bound whether the property is bound or not.
214          **/
215         public void setBound(boolean bound) {
216                 this.bound = bound;
217         }
218
219         /** Get whether the property is constrained.  Defaults to false. **/
220         public boolean isConstrained() {
221                 return constrained;
222         }
223
224         /** Set whether the property is constrained.
225          ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
226          ** (or subclass thereof) and the bean implements addVetoableChangeListener()
227          ** and removeVetoableChangeListener(), then setConstrained(true) may safely
228          ** be called.  Otherwise, the system behavior is undefined.
229          ** <B>Spec note:</B> given those strict parameters, it would be nice if it
230          ** got set automatically by detection, but oh well.<P>
231          ** When a property is constrained, its set method is required to:<P>
232          ** <OL>
233          ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
234          **     event notifying others of the change and allowing them a chance to
235          **     say it is a bad thing.</LI>
236          ** <LI>If any of the listeners throws a PropertyVetoException, then
237          **     it must fire another vetoableChange() event notifying the others
238          **     of a reversion to the old value (though, of course, the change
239          **     was never made).  Then it rethrows the PropertyVetoException and
240          **     exits.</LI>
241          ** <LI>If all has gone well to this point, the value may be changed.</LI>
242          ** </OL>
243          ** @param constrained whether the property is constrained or not.
244          **/
245         public void setConstrained(boolean constrained) {
246                 this.constrained = constrained;
247         }
248
249         /** Get the PropertyEditor class.  Defaults to null. **/
250         public Class getPropertyEditorClass() {
251                 return propertyEditorClass;
252         }
253
254         /** Set the PropertyEditor class.  If the class does not implement
255          ** the PropertyEditor interface, you will likely get an exception
256          ** late in the game.
257          ** @param propertyEditorClass the PropertyEditor class for this class to use.
258          **/
259         public void setPropertyEditorClass(Class propertyEditorClass) {
260                 this.propertyEditorClass = propertyEditorClass;
261         }
262
263         private void findMethods(Class beanClass, String getMethodName1, String getMethodName2, String setMethodName) throws IntrospectionException {
264                 try {
265                         if(getMethodName1 != null) {
266                                 try {
267                                         getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
268                                 } catch(NoSuchMethodException E) {
269                                 }
270                                 if(getMethodName2 != null) {
271                                         if(getMethod != null && !getMethod.getReturnType().equals(java.lang.Boolean.TYPE)) {
272                                                 // If the is() method exists but isn't boolean, we'll just go on and look for
273                                                 // an ordinary get() method.
274                                                 getMethod = null;
275                                         }
276
277                                         Method getMethod2;
278                                         try {
279                                                 getMethod2 = beanClass.getMethod(getMethodName2, new Class[0]);
280                                         } catch(NoSuchMethodException E) {
281                                                 getMethod2 = null;
282                                         }
283                                         if(getMethod2 != null) {
284                                                 if(getMethod != null) {
285                                                         if(!getMethod.getReturnType().equals(getMethod2.getReturnType())) {
286                                                                 throw new IntrospectionException("Both " + getMethodName1 + " and " + getMethodName2 + " exist, and have contradictory return types.");
287                                                         }
288                                                 } else {
289                                                         getMethod = getMethod2;
290                                                 }
291                                         }
292                                 }
293                         }
294
295                         if(getMethod != null) {
296                                 propertyType = getMethod.getReturnType();
297                                 if(setMethodName != null) {
298                                         Class[] setArgs = new Class[1];
299                                         setArgs[0] = propertyType;
300                                         try {
301                                                 setMethod = beanClass.getMethod(setMethodName, setArgs);
302                                                 if(!setMethod.getReturnType().equals(java.lang.Void.TYPE)) {
303                                                         throw new IntrospectionException(setMethodName + " has non-void return type");
304                                                 }
305                                         } catch(NoSuchMethodException E) {
306                                         }
307                                 }
308                         } else if(setMethodName != null) {
309                                 Method[] m = beanClass.getMethods();
310                                 for(int i=0;i<m.length;i++) {
311                                         Method current = m[i];
312                                         if(current.getName().equals(setMethodName)
313                                            && current.getParameterTypes().length == 1
314                                            && current.getReturnType().equals(java.lang.Void.TYPE)) {
315                                                 if(setMethod != null) {
316                                                         throw new IntrospectionException("Multiple, different set methods found that fit the bill!");
317                                                 } else {
318                                                         setMethod = current;
319                                                         propertyType = current.getParameterTypes()[0];
320                                                 }
321                                         }
322                                 }
323                                 if(setMethod == null) {
324                                         throw new IntrospectionException("Cannot find get or set methods.");
325                                 }
326                         } else {
327                                 throw new IntrospectionException("Cannot find get or set methods.");
328                         }
329                 } catch(SecurityException E) {
330                         throw new IntrospectionException("SecurityException thrown on attempt to access methods.");
331                 }
332         }
333 }