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)
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
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. */
31 import java.lang.reflect.*;
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>
41 ** The constraints put on get and set methods are:<P>
43 ** <LI>A get method must have signature
44 ** <CODE><propertyType> <getMethodName>()</CODE></LI>
45 ** <LI>A set method must have signature
46 ** <CODE>void <setMethodName>(<propertyType>)</CODE></LI>
47 ** <LI>Either method type may throw any exception.</LI>
48 ** <LI>Both methods must be public.</LI>
51 ** @author John Keiser
53 ** @version 1.1.0, 26 Jul 1998
56 public class PropertyDescriptor extends FeatureDescriptor {
61 Class propertyEditorClass;
65 PropertyDescriptor(String name) {
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<name>()</CODE>
72 ** (or, optionally, if the property is boolean,
73 ** <CODE>is<name>()</CODE>) and
74 ** <CODE>set<name>()</CODE> in class
75 ** <CODE><beanClass></CODE>, where <name> has its
76 ** first letter capitalized by the constructor.<P>
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>
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.
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.
98 public PropertyDescriptor(String name, Class beanClass) throws IntrospectionException {
102 capitalized = Character.toUpperCase(name.charAt(0)) + name.substring(1);
103 } catch(StringIndexOutOfBoundsException e) {
106 findMethods(beanClass, "is" + capitalized, "get" + capitalized, "set" + capitalized);
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>
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>
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.
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.
135 public PropertyDescriptor(String name, Class beanClass, String getMethodName, String setMethodName) throws IntrospectionException {
137 findMethods(beanClass, getMethodName, null, setMethodName);
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.
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.
151 public PropertyDescriptor(String name, Method getMethod, Method setMethod) throws IntrospectionException {
153 if(getMethod != null && getMethod.getParameterTypes().length > 0) {
154 throw new IntrospectionException("get method has parameters");
156 if(setMethod != null && setMethod.getParameterTypes().length != 1) {
157 throw new IntrospectionException("set method does not have exactly one parameter");
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");
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.");
168 this.getMethod = getMethod;
169 this.setMethod = setMethod;
170 if(getMethod != null) {
171 this.propertyType = getMethod.getReturnType();
173 this.propertyType = setMethod.getParameterTypes()[0];
177 /** Get the property type.
178 ** This is the type the get method returns and the set method
181 public Class getPropertyType() {
185 /** Get the get method. Why they call it readMethod here and
186 ** get everywhere else is beyond me.
188 public Method getReadMethod() {
192 /** Get the set method. Why they call it writeMethod here and
193 ** set everywhere else is beyond me.
195 public Method getWriteMethod() {
199 /** Get whether the property is bound. Defaults to false. **/
200 public boolean isBound() {
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>
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.
215 public void setBound(boolean bound) {
219 /** Get whether the property is constrained. Defaults to false. **/
220 public boolean isConstrained() {
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>
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
241 ** <LI>If all has gone well to this point, the value may be changed.</LI>
243 ** @param constrained whether the property is constrained or not.
245 public void setConstrained(boolean constrained) {
246 this.constrained = constrained;
249 /** Get the PropertyEditor class. Defaults to null. **/
250 public Class getPropertyEditorClass() {
251 return propertyEditorClass;
254 /** Set the PropertyEditor class. If the class does not implement
255 ** the PropertyEditor interface, you will likely get an exception
257 ** @param propertyEditorClass the PropertyEditor class for this class to use.
259 public void setPropertyEditorClass(Class propertyEditorClass) {
260 this.propertyEditorClass = propertyEditorClass;
263 private void findMethods(Class beanClass, String getMethodName1, String getMethodName2, String setMethodName) throws IntrospectionException {
265 if(getMethodName1 != null) {
267 getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
268 } catch(NoSuchMethodException E) {
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.
279 getMethod2 = beanClass.getMethod(getMethodName2, new Class[0]);
280 } catch(NoSuchMethodException E) {
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.");
289 getMethod = getMethod2;
295 if(getMethod != null) {
296 propertyType = getMethod.getReturnType();
297 if(setMethodName != null) {
298 Class[] setArgs = new Class[1];
299 setArgs[0] = propertyType;
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");
305 } catch(NoSuchMethodException E) {
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!");
319 propertyType = current.getParameterTypes()[0];
323 if(setMethod == null) {
324 throw new IntrospectionException("Cannot find get or set methods.");
327 throw new IntrospectionException("Cannot find get or set methods.");
329 } catch(SecurityException E) {
330 throw new IntrospectionException("SecurityException thrown on attempt to access methods.");