1 /* java.beans.PropertyDescriptor
2 Copyright (C) 1998, 2001, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
40 import java
.lang
.reflect
.Constructor
;
41 import java
.lang
.reflect
.InvocationTargetException
;
42 import java
.lang
.reflect
.Method
;
45 ** PropertyDescriptor describes information about a JavaBean property,
46 ** by which we mean a property that has been exposed via a pair of
47 ** get and set methods. (There may be no get method, which means
48 ** the property is write-only, or no set method, which means the
49 ** the property is read-only.)<P>
51 ** The constraints put on get and set methods are:<P>
53 ** <LI>A get method must have signature
54 ** <CODE><propertyType> <getMethodName>()</CODE></LI>
55 ** <LI>A set method must have signature
56 ** <CODE>void <setMethodName>(<propertyType>)</CODE></LI>
57 ** <LI>Either method type may throw any exception.</LI>
58 ** <LI>Both methods must be public.</LI>
61 ** @author John Keiser
62 ** @author Robert Schuster (thebohemian@gmx.net)
64 ** @status updated to 1.4
66 public class PropertyDescriptor
extends FeatureDescriptor
72 Class propertyEditorClass
;
76 PropertyDescriptor(String name
)
81 /** Create a new PropertyDescriptor by introspection.
82 ** This form of constructor creates the PropertyDescriptor by
83 ** looking for a getter method named <CODE>get<name>()</CODE>
84 ** (or, optionally, if the property is boolean,
85 ** <CODE>is<name>()</CODE>) and
86 ** <CODE>set<name>()</CODE> in class
87 ** <CODE><beanClass></CODE>, where <name> has its
88 ** first letter capitalized by the constructor.<P>
90 ** Note that using this constructor the given property must be read- <strong>and</strong>
91 ** writeable. If the implementation does not both, a read and a write method, an
92 ** <code>IntrospectionException</code> is thrown.
94 ** <B>Implementation note:</B> If there is both are both isXXX and
95 ** getXXX methods, the former is used in preference to the latter.
96 ** We do not check that an isXXX method returns a boolean. In both
97 ** cases, this matches the behaviour of JDK 1.4<P>
99 ** @param name the programmatic name of the property, usually
100 ** starting with a lowercase letter (e.g. fooManChu
101 ** instead of FooManChu).
102 ** @param beanClass the class the get and set methods live in.
103 ** @exception IntrospectionException if the methods are not found
106 public PropertyDescriptor(String name
, Class beanClass
)
107 throws IntrospectionException
110 if (name
.length() == 0)
112 throw new IntrospectionException("empty property name");
114 String caps
= Character
.toUpperCase(name
.charAt(0)) + name
.substring(1);
115 findMethods(beanClass
, "is" + caps
, "get" + caps
, "set" + caps
);
117 if (getMethod
== null)
119 throw new IntrospectionException(
120 "Cannot find a is" + caps
+ " or get" + caps
+ " method");
123 if (setMethod
== null)
125 throw new IntrospectionException(
126 "Cannot find a " + caps
+ " method");
129 // finally check the methods compatibility
130 propertyType
= checkMethods(getMethod
, setMethod
);
133 /** Create a new PropertyDescriptor by introspection.
134 ** This form of constructor allows you to specify the
135 ** names of the get and set methods to search for.<P>
137 ** <B>Implementation note:</B> If there is a get method (or
138 ** boolean isXXX() method), then the return type of that method
139 ** is used to find the set method. If there is no get method,
140 ** then the set method is searched for exhaustively.<P>
143 ** If there is no get method and multiple set methods with
144 ** the same name and a single parameter (different type of course),
145 ** then an IntrospectionException is thrown. While Sun's spec
146 ** does not state this, it can make Bean behavior different on
147 ** different systems (since method order is not guaranteed) and as
148 ** such, can be treated as a bug in the spec. I am not aware of
149 ** whether Sun's implementation catches this.
151 ** @param name the programmatic name of the property, usually
152 ** starting with a lowercase letter (e.g. fooManChu
153 ** instead of FooManChu).
154 ** @param beanClass the class the get and set methods live in.
155 ** @param getMethodName the name of the get method or <code>null</code> if the property is write-only.
156 ** @param setMethodName the name of the set method or <code>null</code> if the property is read-only.
157 ** @exception IntrospectionException if the methods are not found
160 public PropertyDescriptor(
163 String getMethodName
,
164 String setMethodName
)
165 throws IntrospectionException
168 findMethods(beanClass
, getMethodName
, null, setMethodName
);
170 if (getMethod
== null && getMethodName
!= null)
172 throw new IntrospectionException(
173 "Cannot find a getter method called " + getMethodName
);
176 if (setMethod
== null && setMethodName
!= null)
178 throw new IntrospectionException(
179 "Cannot find a setter method called " + setMethodName
);
182 propertyType
= checkMethods(getMethod
, setMethod
);
185 /** Create a new PropertyDescriptor using explicit Methods.
186 ** Note that the methods will be checked for conformance to standard
187 ** Property method rules, as described above at the top of this class.
189 ** It is possible to call this method with both <code>Method</code> arguments
190 ** being <code>null</code>. In such a case the property type is <code>null</code>.
192 ** @param name the programmatic name of the property, usually
193 ** starting with a lowercase letter (e.g. fooManChu
194 ** instead of FooManChu).
195 ** @param readMethod the read method or <code>null</code> if the property is write-only.
196 ** @param writeMethod the write method or <code>null</code> if the property is read-only.
197 ** @exception IntrospectionException if the methods are not found
200 public PropertyDescriptor(
204 throws IntrospectionException
207 getMethod
= readMethod
;
208 setMethod
= writeMethod
;
209 propertyType
= checkMethods(getMethod
, setMethod
);
212 /** Get the property type.
213 ** This is the type the get method returns and the set method
216 public Class
getPropertyType()
221 /** Get the get method. Why they call it readMethod here and
222 ** get everywhere else is beyond me.
224 public Method
getReadMethod()
229 /** Sets the read method.<br/>
230 * The read method is used to retrieve the value of a property. A legal
231 * read method must have no arguments. Its return type must not be
232 * <code>void</code>. If this methods succeeds the property type
233 * is adjusted to the return type of the read method.<br/>
235 * It is legal to set the read and the write method to <code>null</code>
236 * or provide method which have been declared in distinct classes.
238 * @param readMethod The new method to be used or <code>null</code>.
239 * @throws IntrospectionException If the given method is invalid.
242 public void setReadMethod(Method readMethod
) throws IntrospectionException
244 propertyType
= checkMethods(readMethod
, setMethod
);
246 getMethod
= readMethod
;
249 /** Get the set method. Why they call it writeMethod here and
250 ** set everywhere else is beyond me.
252 public Method
getWriteMethod()
257 /** Sets the write method.<br/>
258 * The write method is used to set the value of a property. A legal write method
259 * must have a single argument which can be assigned to the property. If no
260 * read method exists the property type changes to the argument type of the
263 * It is legal to set the read and the write method to <code>null</code>
264 * or provide method which have been declared in distinct classes.
266 * @param writeMethod The new method to be used or <code>null</code>.
267 * @throws IntrospectionException If the given method is invalid.
270 public void setWriteMethod(Method writeMethod
)
271 throws IntrospectionException
273 propertyType
= checkMethods(getMethod
, writeMethod
);
275 setMethod
= writeMethod
;
278 /** Get whether the property is bound. Defaults to false. **/
279 public boolean isBound()
284 /** Set whether the property is bound.
285 ** As long as the the bean implements addPropertyChangeListener() and
286 ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
287 ** If these things are not true, then the behavior of the system
288 ** will be undefined.<P>
290 ** When a property is bound, its set method is required to fire the
291 ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
292 ** after the value has changed.
293 ** @param bound whether the property is bound or not.
295 public void setBound(boolean bound
)
300 /** Get whether the property is constrained. Defaults to false. **/
301 public boolean isConstrained()
306 /** Set whether the property is constrained.
307 ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
308 ** (or subclass thereof) and the bean implements addVetoableChangeListener()
309 ** and removeVetoableChangeListener(), then setConstrained(true) may safely
310 ** be called. Otherwise, the system behavior is undefined.
311 ** <B>Spec note:</B> given those strict parameters, it would be nice if it
312 ** got set automatically by detection, but oh well.<P>
313 ** When a property is constrained, its set method is required to:<P>
315 ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
316 ** event notifying others of the change and allowing them a chance to
317 ** say it is a bad thing.</LI>
318 ** <LI>If any of the listeners throws a PropertyVetoException, then
319 ** it must fire another vetoableChange() event notifying the others
320 ** of a reversion to the old value (though, of course, the change
321 ** was never made). Then it rethrows the PropertyVetoException and
323 ** <LI>If all has gone well to this point, the value may be changed.</LI>
325 ** @param constrained whether the property is constrained or not.
327 public void setConstrained(boolean constrained
)
329 this.constrained
= constrained
;
332 /** Get the PropertyEditor class. Defaults to null. **/
333 public Class
getPropertyEditorClass()
335 return propertyEditorClass
;
338 /** Set the PropertyEditor class. If the class does not implement
339 ** the PropertyEditor interface, you will likely get an exception
341 ** @param propertyEditorClass the PropertyEditor class for this
344 public void setPropertyEditorClass(Class propertyEditorClass
)
346 this.propertyEditorClass
= propertyEditorClass
;
350 * Instantiate a property editor using the property editor class.
351 * If no property editor class has been set, this will return null.
352 * If the editor class has a public constructor which takes a single
353 * argument, that will be used and the bean parameter will be passed
354 * to it. Otherwise, a public no-argument constructor will be used,
355 * if available. This method will return null if no constructor is
356 * found or if construction fails for any reason.
357 * @param bean the argument to the constructor
358 * @return a new PropertyEditor, or null on error
361 public PropertyEditor
createPropertyEditor(Object bean
)
363 if (propertyEditorClass
== null)
365 Constructor c
= findConstructor(propertyEditorClass
,
366 new Class
[] { Object
.class });
368 return instantiateClass(c
, new Object
[] { bean
});
369 c
= findConstructor(propertyEditorClass
, null);
371 return instantiateClass(c
, null);
375 // Helper method to look up a constructor and return null if it is not
377 private Constructor
findConstructor(Class k
, Class
[] argTypes
)
381 return k
.getConstructor(argTypes
);
383 catch (NoSuchMethodException _
)
389 // Helper method to instantiate an object but return null on error.
390 private PropertyEditor
instantiateClass(Constructor c
, Object
[] args
)
394 return (PropertyEditor
) c
.newInstance(args
);
396 catch (InstantiationException _
)
400 catch (InvocationTargetException _
)
404 catch (IllegalAccessException _
)
408 catch (ClassCastException _
)
414 private void findMethods(
416 String getMethodName1
,
417 String getMethodName2
,
418 String setMethodName
)
419 throws IntrospectionException
423 // Try the first get method name
424 if (getMethodName1
!= null)
429 beanClass
.getMethod(getMethodName1
, new Class
[0]);
431 catch (NoSuchMethodException e
)
435 // Fall back to the second get method name
436 if (getMethod
== null && getMethodName2
!= null)
441 beanClass
.getMethod(getMethodName2
, new Class
[0]);
443 catch (NoSuchMethodException e
)
447 // Try the set method name
448 if (setMethodName
!= null)
450 if (getMethod
!= null)
452 // If there is a get method, use its return type to help
453 // select the corresponding set method.
454 Class propertyType
= getMethod
.getReturnType();
455 if (propertyType
== Void
.TYPE
)
458 "The property's read method has return type 'void'";
459 throw new IntrospectionException(msg
);
462 Class
[] setArgs
= new Class
[] { propertyType
};
465 setMethod
= beanClass
.getMethod(setMethodName
, setArgs
);
467 catch (NoSuchMethodException e
)
470 else if (getMethodName1
== null && getMethodName2
== null)
472 // If this is a write-only property, choose the first set method
473 // with the required name, one parameter and return type 'void'
474 Method
[] methods
= beanClass
.getMethods();
475 for (int i
= 0; i
< methods
.length
; i
++)
477 if (methods
[i
].getName().equals(setMethodName
)
478 && methods
[i
].getParameterTypes().length
== 1
479 && methods
[i
].getReturnType() == Void
.TYPE
)
481 setMethod
= methods
[i
];
488 catch (SecurityException e
)
490 // FIXME -- shouldn't we just allow SecurityException to propagate?
492 "SecurityException thrown on attempt to access methods.";
493 throw new IntrospectionException(msg
);
497 /** Checks whether the given <code>Method</code> instances are legal read and
498 * write methods. The following requirements must be met:<br/>
500 * <li>the read method must not have an argument</li>
501 * <li>the read method must have a non void return type</li>
502 * <li>the read method may not exist</li>
503 * <li>the write method must have a single argument</li>
504 * <li>the property type and the read method's return type must be assignable from the
505 * write method's argument type</li>
506 * <li>the write method may not exist</li>
508 * While checking the methods a common new property type is calculated. If the method
509 * succeeds this property type is returned.<br/>
511 * For compatibility this has to be noted:<br/>
512 * The two methods are allowed to be defined in two distinct classes and may both be null.
514 * @param readMethod The new read method to check.
515 * @param writeMethod The new write method to check.
516 * @return The common property type of the two method.
517 * @throws IntrospectionException If any of the above requirements are not met.
519 private Class
checkMethods(Method readMethod
, Method writeMethod
)
520 throws IntrospectionException
522 Class newPropertyType
= propertyType
;
524 // a valid read method has zero arguments and a non-void return type.
525 if (readMethod
!= null)
527 if (readMethod
.getParameterTypes().length
> 0)
529 throw new IntrospectionException("read method has unexpected parameters");
532 newPropertyType
= readMethod
.getReturnType();
534 if (newPropertyType
== Void
.TYPE
)
536 throw new IntrospectionException("read method return type is void");
540 // a valid write method has one argument which can be assigned to the property
541 if (writeMethod
!= null)
543 if (writeMethod
.getParameterTypes().length
!= 1)
545 String msg
= "write method does not have exactly one parameter";
546 throw new IntrospectionException(msg
);
549 if (readMethod
== null)
551 // changes the property type if there is no read method
552 newPropertyType
= writeMethod
.getParameterTypes()[0];
556 // checks whether the write method can be assigned to the return type of the read
557 // method (if this is not the case, the methods are not compatible)
558 // note: newPropertyType may be null if no methods or method names have been
559 // delivered in the constructor.
560 if (newPropertyType
!= null
561 && !newPropertyType
.isAssignableFrom(
562 writeMethod
.getParameterTypes()[0]))
564 // note: newPropertyType is the same as readMethod.getReturnType() at this point
565 throw new IntrospectionException("read and write method are not compatible");
568 /* note: the check whether both method are defined in related classes makes sense but is not
570 * I leave this code here in case someone at Sun decides to add that functionality in later versions (rschuster)
573 .isAssignableFrom(writeMethod.getDeclaringClass()))
576 .isAssignableFrom(readMethod.getDeclaringClass())))
579 "set and get methods are not in the same class.";
580 throw new IntrospectionException(msg);
587 return newPropertyType
;
591 * Return a hash code for this object, conforming to the contract described
592 * in {@link Object#hashCode()}.
593 * @return the hash code
596 public int hashCode()
598 return ((propertyType
== null ?
0 : propertyType
.hashCode())
599 | (propertyEditorClass
== null ?
0 : propertyEditorClass
.hashCode())
600 | (bound ? Boolean
.TRUE
: Boolean
.FALSE
).hashCode()
601 | (constrained ? Boolean
.TRUE
: Boolean
.FALSE
).hashCode()
602 | (getMethod
== null ?
0 : getMethod
.hashCode())
603 | (setMethod
== null ?
0 : setMethod
.hashCode()));
606 /** Compares this <code>PropertyDescriptor</code> against the
608 * Two PropertyDescriptors are equals if
610 * <li>the read methods are equal</li>
611 * <li>the write methods are equal</li>
612 * <li>the property types are equals</li>
613 * <li>the property editor classes are equal</li>
614 * <li>the flags (constrained and bound) are equal</li>
616 * @return Whether both objects are equal according to the rules given above.
619 public boolean equals(Object o
)
621 if (o
instanceof PropertyDescriptor
)
623 PropertyDescriptor that
= (PropertyDescriptor
) o
;
625 // compares the property types and checks the case where both are null
626 boolean samePropertyType
=
627 (propertyType
== null)
628 ? that
.propertyType
== null
629 : propertyType
.equals(that
.propertyType
);
631 // compares the property editor classes and checks the case where both are null
632 boolean samePropertyEditorClass
=
633 (propertyEditorClass
== null)
634 ? that
.propertyEditorClass
== null
635 : propertyEditorClass
.equals(that
.propertyEditorClass
);
637 // compares the flags for equality
639 bound
== that
.bound
&& constrained
== that
.constrained
;
641 // compares the read methods and checks the case where both are null
642 boolean sameReadMethod
=
644 ? that
.getMethod
== null
645 : getMethod
.equals(that
.getMethod
);
647 boolean sameWriteMethod
=
649 ? that
.setMethod
== null
650 : setMethod
.equals(that
.setMethod
);
652 return samePropertyType
656 && samePropertyEditorClass
;