source: trunk/src/gcc/libjava/java/beans/PropertyDescriptor.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.2 KB
Line 
1/* java.beans.PropertyDescriptor
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.beans;
40
41import java.util.*;
42import 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>&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>
61 **
62 ** @author John Keiser
63 ** @since JDK1.1
64 ** @version 1.1.0, 26 Jul 1998
65 **/
66
67public 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&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>
88 **
89 ** <B>Implementation note:</B> If there is a get method (or
90 ** boolean isXXX() method), then the return type of that method
91 ** is used to find the set method. If there is no get method,
92 ** then the set method is searched for exhaustively.<P>
93 **
94 ** <B>Spec note:</B>
95 ** If there is no get method and multiple set methods with
96 ** the same name and a single parameter (different type of course),
97 ** then an IntrospectionException is thrown. While Sun's spec
98 ** does not state this, it can make Bean behavior different on
99 ** different systems (since method order is not guaranteed) and as
100 ** such, can be treated as a bug in the spec. I am not aware of
101 ** whether Sun's implementation catches this.
102 **
103 ** @param name the programmatic name of the property, usually
104 ** starting with a lowercase letter (e.g. fooManChu
105 ** instead of FooManChu).
106 ** @param beanClass the class the get and set methods live in.
107 ** @exception IntrospectionException if the methods are not found or invalid.
108 **/
109 public PropertyDescriptor(String name, Class beanClass) throws IntrospectionException {
110 setName(name);
111 String capitalized;
112 try {
113 capitalized = Character.toUpperCase(name.charAt(0)) + name.substring(1);
114 } catch(StringIndexOutOfBoundsException e) {
115 capitalized = "";
116 }
117 findMethods(beanClass, "is" + capitalized, "get" + capitalized, "set" + capitalized);
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 or invalid.
145 **/
146 public PropertyDescriptor(String name, Class beanClass, String getMethodName, String setMethodName) throws IntrospectionException {
147 setName(name);
148 findMethods(beanClass, getMethodName, null, setMethodName);
149 }
150
151 /** Create a new PropertyDescriptor using explicit Methods.
152 ** Note that the methods will be checked for conformance to standard
153 ** Property method rules, as described above at the top of this class.
154 **
155 ** @param name the programmatic name of the property, usually
156 ** starting with a lowercase letter (e.g. fooManChu
157 ** instead of FooManChu).
158 ** @param getMethod the get method.
159 ** @param setMethod the set method.
160 ** @exception IntrospectionException if the methods are not found or invalid.
161 **/
162 public PropertyDescriptor(String name, Method getMethod, Method setMethod) throws IntrospectionException {
163 setName(name);
164 if(getMethod != null && getMethod.getParameterTypes().length > 0) {
165 throw new IntrospectionException("get method has parameters");
166 }
167 if(setMethod != null && setMethod.getParameterTypes().length != 1) {
168 throw new IntrospectionException("set method does not have exactly one parameter");
169 }
170 if(getMethod != null && setMethod != null) {
171 if(!getMethod.getReturnType().equals(setMethod.getParameterTypes()[0])) {
172 throw new IntrospectionException("set and get methods do not share the same type");
173 }
174 if(!getMethod.getDeclaringClass().isAssignableFrom(setMethod.getDeclaringClass())
175 && !setMethod.getDeclaringClass().isAssignableFrom(getMethod.getDeclaringClass())) {
176 throw new IntrospectionException("set and get methods are not in the same class.");
177 }
178 }
179 this.getMethod = getMethod;
180 this.setMethod = setMethod;
181 if(getMethod != null) {
182 this.propertyType = getMethod.getReturnType();
183 } else {
184 this.propertyType = setMethod.getParameterTypes()[0];
185 }
186 }
187
188 /** Get the property type.
189 ** This is the type the get method returns and the set method
190 ** takes in.
191 **/
192 public Class getPropertyType() {
193 return propertyType;
194 }
195
196 /** Get the get method. Why they call it readMethod here and
197 ** get everywhere else is beyond me.
198 **/
199 public Method getReadMethod() {
200 return getMethod;
201 }
202
203 /** Get the set method. Why they call it writeMethod here and
204 ** set everywhere else is beyond me.
205 **/
206 public Method getWriteMethod() {
207 return setMethod;
208 }
209
210 /** Get whether the property is bound. Defaults to false. **/
211 public boolean isBound() {
212 return bound;
213 }
214
215 /** Set whether the property is bound.
216 ** As long as the the bean implements addPropertyChangeListener() and
217 ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
218 ** If these things are not true, then the behavior of the system
219 ** will be undefined.<P>
220 **
221 ** When a property is bound, its set method is required to fire the
222 ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
223 ** after the value has changed.
224 ** @param bound whether the property is bound or not.
225 **/
226 public void setBound(boolean bound) {
227 this.bound = bound;
228 }
229
230 /** Get whether the property is constrained. Defaults to false. **/
231 public boolean isConstrained() {
232 return constrained;
233 }
234
235 /** Set whether the property is constrained.
236 ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
237 ** (or subclass thereof) and the bean implements addVetoableChangeListener()
238 ** and removeVetoableChangeListener(), then setConstrained(true) may safely
239 ** be called. Otherwise, the system behavior is undefined.
240 ** <B>Spec note:</B> given those strict parameters, it would be nice if it
241 ** got set automatically by detection, but oh well.<P>
242 ** When a property is constrained, its set method is required to:<P>
243 ** <OL>
244 ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
245 ** event notifying others of the change and allowing them a chance to
246 ** say it is a bad thing.</LI>
247 ** <LI>If any of the listeners throws a PropertyVetoException, then
248 ** it must fire another vetoableChange() event notifying the others
249 ** of a reversion to the old value (though, of course, the change
250 ** was never made). Then it rethrows the PropertyVetoException and
251 ** exits.</LI>
252 ** <LI>If all has gone well to this point, the value may be changed.</LI>
253 ** </OL>
254 ** @param constrained whether the property is constrained or not.
255 **/
256 public void setConstrained(boolean constrained) {
257 this.constrained = constrained;
258 }
259
260 /** Get the PropertyEditor class. Defaults to null. **/
261 public Class getPropertyEditorClass() {
262 return propertyEditorClass;
263 }
264
265 /** Set the PropertyEditor class. If the class does not implement
266 ** the PropertyEditor interface, you will likely get an exception
267 ** late in the game.
268 ** @param propertyEditorClass the PropertyEditor class for this class to use.
269 **/
270 public void setPropertyEditorClass(Class propertyEditorClass) {
271 this.propertyEditorClass = propertyEditorClass;
272 }
273
274 private void findMethods(Class beanClass, String getMethodName1, String getMethodName2, String setMethodName) throws IntrospectionException {
275 try {
276 if(getMethodName1 != null) {
277 try {
278 getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
279 } catch(NoSuchMethodException E) {
280 }
281 if(getMethodName2 != null) {
282 if(getMethod != null && !getMethod.getReturnType().equals(java.lang.Boolean.TYPE)) {
283 // If the is() method exists but isn't boolean, we'll just go on and look for
284 // an ordinary get() method.
285 getMethod = null;
286 }
287
288 Method getMethod2;
289 try {
290 getMethod2 = beanClass.getMethod(getMethodName2, new Class[0]);
291 } catch(NoSuchMethodException E) {
292 getMethod2 = null;
293 }
294 if(getMethod2 != null) {
295 if(getMethod != null) {
296 if(!getMethod.getReturnType().equals(getMethod2.getReturnType())) {
297 throw new IntrospectionException("Both " + getMethodName1 + " and " + getMethodName2 + " exist, and have contradictory return types.");
298 }
299 } else {
300 getMethod = getMethod2;
301 }
302 }
303 }
304 }
305
306 if(getMethod != null) {
307 propertyType = getMethod.getReturnType();
308 if(setMethodName != null) {
309 Class[] setArgs = new Class[1];
310 setArgs[0] = propertyType;
311 try {
312 setMethod = beanClass.getMethod(setMethodName, setArgs);
313 if(!setMethod.getReturnType().equals(java.lang.Void.TYPE)) {
314 throw new IntrospectionException(setMethodName + " has non-void return type");
315 }
316 } catch(NoSuchMethodException E) {
317 }
318 }
319 } else if(setMethodName != null) {
320 Method[] m = beanClass.getMethods();
321 for(int i=0;i<m.length;i++) {
322 Method current = m[i];
323 if(current.getName().equals(setMethodName)
324 && current.getParameterTypes().length == 1
325 && current.getReturnType().equals(java.lang.Void.TYPE)) {
326 if(setMethod != null) {
327 throw new IntrospectionException("Multiple, different set methods found that fit the bill!");
328 } else {
329 setMethod = current;
330 propertyType = current.getParameterTypes()[0];
331 }
332 }
333 }
334 if(setMethod == null) {
335 throw new IntrospectionException("Cannot find get or set methods.");
336 }
337 } else {
338 throw new IntrospectionException("Cannot find get or set methods.");
339 }
340 } catch(SecurityException E) {
341 throw new IntrospectionException("SecurityException thrown on attempt to access methods.");
342 }
343 }
344}
Note: See TracBrowser for help on using the repository browser.