Commit b20fcd47 by Stephen Crawley Committed by Mark Wielaard

Stephen Crawley <crawley@dstc.edu.au>

       * java/beans/PropertyDescriptor.java
       (PropertyDescriptor(String, Class)): Sanity check getter and setter
       methods.
       (PropertyDescriptor(String, Class, String, String)): Likewise.
       (PropertyDescriptor(String, Method, Method): Factor out getter and
       setter method sanity checks into new method.
       (findMethods): Don't do parameter sanity checking of get method here.
       (checkMethods): New method.

2003-02-07  Stephen Crawley  <crawley@dstc.edu.au>

       * java/beans/PropertyDescriptor.java: Reformat.

From-SVN: r62537
parent 7bf0a593
2003-02-07 Stephen Crawley <crawley@dstc.edu.au>
* java/beans/PropertyDescriptor.java
(PropertyDescriptor(String, Class)): Sanity check getter and setter
methods.
(PropertyDescriptor(String, Class, String, String)): Likewise.
(PropertyDescriptor(String, Method, Method): Factor out getter and
setter method sanity checks into new method.
(findMethods): Don't do parameter sanity checking of get method here.
(checkMethods): New method.
2003-02-07 Stephen Crawley <crawley@dstc.edu.au>
* java/beans/PropertyDescriptor.java: Reformat.
2003-02-04 Tom Tromey <tromey@redhat.com> 2003-02-04 Tom Tromey <tromey@redhat.com>
* java/io/PipedOutputStream.java (flush): Declare as throwing * java/io/PipedOutputStream.java (flush): Declare as throwing
......
...@@ -65,280 +65,312 @@ import java.lang.reflect.*; ...@@ -65,280 +65,312 @@ import java.lang.reflect.*;
**/ **/
public class PropertyDescriptor extends FeatureDescriptor { public class PropertyDescriptor extends FeatureDescriptor {
Class propertyType; Class propertyType;
Method getMethod; Method getMethod;
Method setMethod; Method setMethod;
Class propertyEditorClass; Class propertyEditorClass;
boolean bound; boolean bound;
boolean constrained; boolean constrained;
PropertyDescriptor(String name) { PropertyDescriptor(String name) {
setName(name); setName(name);
} }
/** Create a new PropertyDescriptor by introspection. /** Create a new PropertyDescriptor by introspection.
** This form of constructor creates the PropertyDescriptor by ** This form of constructor creates the PropertyDescriptor by
** looking for a getter method named <CODE>get&lt;name&gt;()</CODE> ** looking for a getter method named <CODE>get&lt;name&gt;()</CODE>
** (or, optionally, if the property is boolean, ** (or, optionally, if the property is boolean,
** <CODE>is&lt;name&gt;()</CODE>) and ** <CODE>is&lt;name&gt;()</CODE>) and
** <CODE>set&lt;name&gt;()</CODE> in class ** <CODE>set&lt;name&gt;()</CODE> in class
** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
** first letter capitalized by the constructor.<P> ** first letter capitalized by the constructor.<P>
** **
** <B>Implementation note:</B> If there is a get method (or ** <B>Implementation note:</B> If there is both are both isXXX and
** boolean isXXX() method), then the return type of that method ** getXXX methods, the former is used in preference to the latter.
** is used to find the set method. If there is no get method, ** We do not check that an isXXX method returns a boolean. In both
** then the set method is searched for exhaustively.<P> ** cases, this matches the behaviour of JDK 1.4<P>
** **
** <B>Spec note:</B> ** @param name the programmatic name of the property, usually
** If there is no get method and multiple set methods with ** starting with a lowercase letter (e.g. fooManChu
** the same name and a single parameter (different type of course), ** instead of FooManChu).
** then an IntrospectionException is thrown. While Sun's spec ** @param beanClass the class the get and set methods live in.
** does not state this, it can make Bean behavior different on ** @exception IntrospectionException if the methods are not found
** different systems (since method order is not guaranteed) and as ** or invalid.
** such, can be treated as a bug in the spec. I am not aware of **/
** whether Sun's implementation catches this. public PropertyDescriptor(String name, Class beanClass)
** throws IntrospectionException
** @param name the programmatic name of the property, usually {
** starting with a lowercase letter (e.g. fooManChu setName(name);
** instead of FooManChu). if (name.length() == 0) {
** @param beanClass the class the get and set methods live in. throw new IntrospectionException("empty property name");
** @exception IntrospectionException if the methods are not found or invalid. }
**/ String caps = Character.toUpperCase(name.charAt(0)) + name.substring(1);
public PropertyDescriptor(String name, Class beanClass) throws IntrospectionException { findMethods(beanClass, "is" + caps, "get" + caps, "set" + caps);
setName(name); if (getMethod == null) {
String capitalized; throw new IntrospectionException("Cannot find an is" + caps +
try { " or get" + caps + " method");
capitalized = Character.toUpperCase(name.charAt(0)) + name.substring(1); }
} catch(StringIndexOutOfBoundsException e) { if (setMethod == null) {
capitalized = ""; throw new IntrospectionException("Cannot find a " + caps + " method");
} }
findMethods(beanClass, "is" + capitalized, "get" + capitalized, "set" + capitalized); checkMethods();
} }
/** Create a new PropertyDescriptor by introspection. /** Create a new PropertyDescriptor by introspection.
** This form of constructor allows you to specify the ** This form of constructor allows you to specify the
** names of the get and set methods to search for.<P> ** names of the get and set methods to search for.<P>
** **
** <B>Implementation note:</B> If there is a get method (or ** <B>Implementation note:</B> If there is a get method (or
** boolean isXXX() method), then the return type of that method ** boolean isXXX() method), then the return type of that method
** is used to find the set method. If there is no get method, ** is used to find the set method. If there is no get method,
** then the set method is searched for exhaustively.<P> ** then the set method is searched for exhaustively.<P>
** **
** <B>Spec note:</B> ** <B>Spec note:</B>
** If there is no get method and multiple set methods with ** If there is no get method and multiple set methods with
** the same name and a single parameter (different type of course), ** the same name and a single parameter (different type of course),
** then an IntrospectionException is thrown. While Sun's spec ** then an IntrospectionException is thrown. While Sun's spec
** does not state this, it can make Bean behavior different on ** does not state this, it can make Bean behavior different on
** different systems (since method order is not guaranteed) and as ** different systems (since method order is not guaranteed) and as
** such, can be treated as a bug in the spec. I am not aware of ** such, can be treated as a bug in the spec. I am not aware of
** whether Sun's implementation catches this. ** whether Sun's implementation catches this.
** **
** @param name the programmatic name of the property, usually ** @param name the programmatic name of the property, usually
** starting with a lowercase letter (e.g. fooManChu ** starting with a lowercase letter (e.g. fooManChu
** instead of FooManChu). ** instead of FooManChu).
** @param beanClass the class the get and set methods live in. ** @param beanClass the class the get and set methods live in.
** @param getMethodName the name of the get method. ** @param getMethodName the name of the get method.
** @param setMethodName the name of the set method. ** @param setMethodName the name of the set method.
** @exception IntrospectionException if the methods are not found or invalid. ** @exception IntrospectionException if the methods are not found
**/ ** or invalid.
public PropertyDescriptor(String name, Class beanClass, String getMethodName, String setMethodName) throws IntrospectionException { **/
setName(name); public PropertyDescriptor(String name, Class beanClass,
findMethods(beanClass, getMethodName, null, setMethodName); String getMethodName, String setMethodName)
} throws IntrospectionException
{
/** Create a new PropertyDescriptor using explicit Methods. setName(name);
** Note that the methods will be checked for conformance to standard findMethods(beanClass, getMethodName, null, setMethodName);
** Property method rules, as described above at the top of this class. if (getMethod == null && getMethodName != null) {
** throw new IntrospectionException("Cannot find a getter method called " +
** @param name the programmatic name of the property, usually getMethodName);
** starting with a lowercase letter (e.g. fooManChu }
** instead of FooManChu). if (setMethod == null && setMethodName != null) {
** @param getMethod the get method. throw new IntrospectionException("Cannot find a setter method called " +
** @param setMethod the set method. setMethodName);
** @exception IntrospectionException if the methods are not found or invalid. }
**/ checkMethods();
public PropertyDescriptor(String name, Method getMethod, Method setMethod) throws IntrospectionException { }
setName(name);
if(getMethod != null && getMethod.getParameterTypes().length > 0) { /** Create a new PropertyDescriptor using explicit Methods.
throw new IntrospectionException("get method has parameters"); ** Note that the methods will be checked for conformance to standard
} ** Property method rules, as described above at the top of this class.
if(setMethod != null && setMethod.getParameterTypes().length != 1) { **
throw new IntrospectionException("set method does not have exactly one parameter"); ** @param name the programmatic name of the property, usually
} ** starting with a lowercase letter (e.g. fooManChu
if(getMethod != null && setMethod != null) { ** instead of FooManChu).
if(!getMethod.getReturnType().equals(setMethod.getParameterTypes()[0])) { ** @param getMethod the get method.
throw new IntrospectionException("set and get methods do not share the same type"); ** @param setMethod the set method.
} ** @exception IntrospectionException if the methods are not found
if(!getMethod.getDeclaringClass().isAssignableFrom(setMethod.getDeclaringClass()) ** or invalid.
&& !setMethod.getDeclaringClass().isAssignableFrom(getMethod.getDeclaringClass())) { **/
throw new IntrospectionException("set and get methods are not in the same class."); public PropertyDescriptor(String name, Method getMethod, Method setMethod)
} throws IntrospectionException
} {
this.getMethod = getMethod; setName(name);
this.setMethod = setMethod; this.getMethod = getMethod;
if(getMethod != null) { this.setMethod = setMethod;
this.propertyType = getMethod.getReturnType(); if (getMethod != null) {
} else { this.propertyType = getMethod.getReturnType();
this.propertyType = setMethod.getParameterTypes()[0]; }
} else if (setMethod != null) {
} this.propertyType = setMethod.getParameterTypes()[0];
}
/** Get the property type. checkMethods();
** This is the type the get method returns and the set method }
** takes in.
**/ /** Get the property type.
public Class getPropertyType() { ** This is the type the get method returns and the set method
return propertyType; ** takes in.
} **/
public Class getPropertyType() {
/** Get the get method. Why they call it readMethod here and return propertyType;
** get everywhere else is beyond me. }
**/
public Method getReadMethod() { /** Get the get method. Why they call it readMethod here and
return getMethod; ** get everywhere else is beyond me.
} **/
public Method getReadMethod() {
/** Get the set method. Why they call it writeMethod here and return getMethod;
** set everywhere else is beyond me. }
**/
public Method getWriteMethod() { /** Get the set method. Why they call it writeMethod here and
return setMethod; ** set everywhere else is beyond me.
**/
public Method getWriteMethod() {
return setMethod;
}
/** Get whether the property is bound. Defaults to false. **/
public boolean isBound() {
return bound;
}
/** Set whether the property is bound.
** As long as the the bean implements addPropertyChangeListener() and
** removePropertyChangeListener(), setBound(true) may safely be called.<P>
** If these things are not true, then the behavior of the system
** will be undefined.<P>
**
** When a property is bound, its set method is required to fire the
** <CODE>PropertyChangeListener.propertyChange())</CODE> event
** after the value has changed.
** @param bound whether the property is bound or not.
**/
public void setBound(boolean bound) {
this.bound = bound;
}
/** Get whether the property is constrained. Defaults to false. **/
public boolean isConstrained() {
return constrained;
}
/** Set whether the property is constrained.
** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
** (or subclass thereof) and the bean implements addVetoableChangeListener()
** and removeVetoableChangeListener(), then setConstrained(true) may safely
** be called. Otherwise, the system behavior is undefined.
** <B>Spec note:</B> given those strict parameters, it would be nice if it
** got set automatically by detection, but oh well.<P>
** When a property is constrained, its set method is required to:<P>
** <OL>
** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
** event notifying others of the change and allowing them a chance to
** say it is a bad thing.</LI>
** <LI>If any of the listeners throws a PropertyVetoException, then
** it must fire another vetoableChange() event notifying the others
** of a reversion to the old value (though, of course, the change
** was never made). Then it rethrows the PropertyVetoException and
** exits.</LI>
** <LI>If all has gone well to this point, the value may be changed.</LI>
** </OL>
** @param constrained whether the property is constrained or not.
**/
public void setConstrained(boolean constrained) {
this.constrained = constrained;
}
/** Get the PropertyEditor class. Defaults to null. **/
public Class getPropertyEditorClass() {
return propertyEditorClass;
}
/** Set the PropertyEditor class. If the class does not implement
** the PropertyEditor interface, you will likely get an exception
** late in the game.
** @param propertyEditorClass the PropertyEditor class for this
** class to use.
**/
public void setPropertyEditorClass(Class propertyEditorClass) {
this.propertyEditorClass = propertyEditorClass;
}
private void findMethods(Class beanClass, String getMethodName1,
String getMethodName2, String setMethodName)
throws IntrospectionException
{
try {
// Try the first get method name
if (getMethodName1 != null) {
try {
getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
}
catch (NoSuchMethodException e) {
} }
}
/** Get whether the property is bound. Defaults to false. **/ // Fall back to the second get method name
public boolean isBound() { if (getMethod == null && getMethodName2 != null) {
return bound; try {
getMethod = beanClass.getMethod(getMethodName2, new Class[0]);
}
catch (NoSuchMethodException e) {
} }
}
/** Set whether the property is bound. // Try the set method name
** As long as the the bean implements addPropertyChangeListener() and if (setMethodName != null) {
** removePropertyChangeListener(), setBound(true) may safely be called.<P> if (getMethod != null) {
** If these things are not true, then the behavior of the system // If there is a get method, use its return type to help
** will be undefined.<P> // select the corresponding set method.
** Class propertyType = getMethod.getReturnType();
** When a property is bound, its set method is required to fire the if (propertyType == Void.TYPE) {
** <CODE>PropertyChangeListener.propertyChange())</CODE> event String msg = "The property's read method has return type 'void'";
** after the value has changed. throw new IntrospectionException(msg);
** @param bound whether the property is bound or not. }
**/
public void setBound(boolean bound) { Class[] setArgs = new Class[]{propertyType};
this.bound = bound; try {
setMethod = beanClass.getMethod(setMethodName, setArgs);
}
catch (NoSuchMethodException e) {
}
} }
else if (getMethodName1 == null && getMethodName2 == null) {
/** Get whether the property is constrained. Defaults to false. **/ // If this is a write-only property, choose the first set method
public boolean isConstrained() { // with the required name, one parameter and return type 'void'
return constrained; Method[] methods = beanClass.getMethods();
} for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(setMethodName) &&
/** Set whether the property is constrained. methods[i].getParameterTypes().length == 1 &&
** If the set method throws <CODE>java.beans.PropertyVetoException</CODE> methods[i].getReturnType() == Void.TYPE) {
** (or subclass thereof) and the bean implements addVetoableChangeListener() setMethod = methods[i];
** and removeVetoableChangeListener(), then setConstrained(true) may safely break;
** be called. Otherwise, the system behavior is undefined. }
** <B>Spec note:</B> given those strict parameters, it would be nice if it }
** got set automatically by detection, but oh well.<P>
** When a property is constrained, its set method is required to:<P>
** <OL>
** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
** event notifying others of the change and allowing them a chance to
** say it is a bad thing.</LI>
** <LI>If any of the listeners throws a PropertyVetoException, then
** it must fire another vetoableChange() event notifying the others
** of a reversion to the old value (though, of course, the change
** was never made). Then it rethrows the PropertyVetoException and
** exits.</LI>
** <LI>If all has gone well to this point, the value may be changed.</LI>
** </OL>
** @param constrained whether the property is constrained or not.
**/
public void setConstrained(boolean constrained) {
this.constrained = constrained;
} }
}
}
catch (SecurityException e) {
// FIXME -- shouldn't we just allow SecurityException to propagate?
String msg = "SecurityException thrown on attempt to access methods.";
throw new IntrospectionException(msg);
}
}
/** Get the PropertyEditor class. Defaults to null. **/ private void checkMethods()
public Class getPropertyEditorClass() { throws IntrospectionException
return propertyEditorClass; {
if (getMethod != null) {
if (getMethod.getParameterTypes().length > 0) {
throw new IntrospectionException("get method has parameters");
}
this.propertyType = getMethod.getReturnType();
if (propertyType == Void.TYPE) {
throw new IntrospectionException("get method has void return type");
}
}
if (setMethod != null) {
if (setMethod.getParameterTypes().length != 1) {
String msg = "set method does not have exactly one parameter";
throw new IntrospectionException(msg);
}
if (getMethod == null) {
propertyType = setMethod.getParameterTypes()[0];
}
else {
if (!propertyType.equals(setMethod.getParameterTypes()[0])) {
String msg = "set and get methods do not share the same type";
throw new IntrospectionException(msg);
} }
if ((!getMethod.getDeclaringClass().
/** Set the PropertyEditor class. If the class does not implement isAssignableFrom(setMethod.getDeclaringClass())) &&
** the PropertyEditor interface, you will likely get an exception (!setMethod.getDeclaringClass().
** late in the game. isAssignableFrom(getMethod.getDeclaringClass()))) {
** @param propertyEditorClass the PropertyEditor class for this class to use. String msg = "set and get methods are not in the same class.";
**/ throw new IntrospectionException(msg);
public void setPropertyEditorClass(Class propertyEditorClass) {
this.propertyEditorClass = propertyEditorClass;
}
private void findMethods(Class beanClass, String getMethodName1, String getMethodName2, String setMethodName) throws IntrospectionException {
try {
if(getMethodName1 != null) {
try {
getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
} catch(NoSuchMethodException E) {
}
if(getMethodName2 != null) {
if(getMethod != null && !getMethod.getReturnType().equals(java.lang.Boolean.TYPE)) {
// If the is() method exists but isn't boolean, we'll just go on and look for
// an ordinary get() method.
getMethod = null;
}
Method getMethod2;
try {
getMethod2 = beanClass.getMethod(getMethodName2, new Class[0]);
} catch(NoSuchMethodException E) {
getMethod2 = null;
}
if(getMethod2 != null) {
if(getMethod != null) {
if(!getMethod.getReturnType().equals(getMethod2.getReturnType())) {
throw new IntrospectionException("Both " + getMethodName1 + " and " + getMethodName2 + " exist, and have contradictory return types.");
}
} else {
getMethod = getMethod2;
}
}
}
}
if(getMethod != null) {
propertyType = getMethod.getReturnType();
if(setMethodName != null) {
Class[] setArgs = new Class[1];
setArgs[0] = propertyType;
try {
setMethod = beanClass.getMethod(setMethodName, setArgs);
if(!setMethod.getReturnType().equals(java.lang.Void.TYPE)) {
throw new IntrospectionException(setMethodName + " has non-void return type");
}
} catch(NoSuchMethodException E) {
}
}
} else if(setMethodName != null) {
Method[] m = beanClass.getMethods();
for(int i=0;i<m.length;i++) {
Method current = m[i];
if(current.getName().equals(setMethodName)
&& current.getParameterTypes().length == 1
&& current.getReturnType().equals(java.lang.Void.TYPE)) {
if(setMethod != null) {
throw new IntrospectionException("Multiple, different set methods found that fit the bill!");
} else {
setMethod = current;
propertyType = current.getParameterTypes()[0];
}
}
}
if(setMethod == null) {
throw new IntrospectionException("Cannot find get or set methods.");
}
} else {
throw new IntrospectionException("Cannot find get or set methods.");
}
} catch(SecurityException E) {
throw new IntrospectionException("SecurityException thrown on attempt to access methods.");
}
} }
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment