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
......
...@@ -86,35 +86,35 @@ public class PropertyDescriptor extends FeatureDescriptor { ...@@ -86,35 +86,35 @@ public class PropertyDescriptor extends FeatureDescriptor {
** <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>
** If there is no get method and multiple set methods with
** the same name and a single parameter (different type of course),
** then an IntrospectionException is thrown. While Sun's spec
** does not state this, it can make Bean behavior different on
** 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
** 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.
** @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) throws IntrospectionException { public PropertyDescriptor(String name, Class beanClass)
throws IntrospectionException
{
setName(name); setName(name);
String capitalized; if (name.length() == 0) {
try { throw new IntrospectionException("empty property name");
capitalized = Character.toUpperCase(name.charAt(0)) + name.substring(1); }
} catch(StringIndexOutOfBoundsException e) { String caps = Character.toUpperCase(name.charAt(0)) + name.substring(1);
capitalized = ""; findMethods(beanClass, "is" + caps, "get" + caps, "set" + caps);
if (getMethod == null) {
throw new IntrospectionException("Cannot find an is" + caps +
" or get" + caps + " method");
}
if (setMethod == null) {
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.
...@@ -141,11 +141,24 @@ public class PropertyDescriptor extends FeatureDescriptor { ...@@ -141,11 +141,24 @@ public class PropertyDescriptor extends FeatureDescriptor {
** @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 { public PropertyDescriptor(String name, Class beanClass,
String getMethodName, String setMethodName)
throws IntrospectionException
{
setName(name); setName(name);
findMethods(beanClass, getMethodName, null, setMethodName); findMethods(beanClass, getMethodName, null, setMethodName);
if (getMethod == null && getMethodName != null) {
throw new IntrospectionException("Cannot find a getter method called " +
getMethodName);
}
if (setMethod == null && setMethodName != null) {
throw new IntrospectionException("Cannot find a setter method called " +
setMethodName);
}
checkMethods();
} }
/** Create a new PropertyDescriptor using explicit Methods. /** Create a new PropertyDescriptor using explicit Methods.
...@@ -157,32 +170,22 @@ public class PropertyDescriptor extends FeatureDescriptor { ...@@ -157,32 +170,22 @@ public class PropertyDescriptor extends FeatureDescriptor {
** instead of FooManChu). ** instead of FooManChu).
** @param getMethod the get method. ** @param getMethod the get method.
** @param setMethod the set method. ** @param setMethod 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, Method getMethod, Method setMethod) throws IntrospectionException { public PropertyDescriptor(String name, Method getMethod, Method setMethod)
throws IntrospectionException
{
setName(name); setName(name);
if(getMethod != null && getMethod.getParameterTypes().length > 0) {
throw new IntrospectionException("get method has parameters");
}
if(setMethod != null && setMethod.getParameterTypes().length != 1) {
throw new IntrospectionException("set method does not have exactly one parameter");
}
if(getMethod != null && setMethod != null) {
if(!getMethod.getReturnType().equals(setMethod.getParameterTypes()[0])) {
throw new IntrospectionException("set and get methods do not share the same type");
}
if(!getMethod.getDeclaringClass().isAssignableFrom(setMethod.getDeclaringClass())
&& !setMethod.getDeclaringClass().isAssignableFrom(getMethod.getDeclaringClass())) {
throw new IntrospectionException("set and get methods are not in the same class.");
}
}
this.getMethod = getMethod; this.getMethod = getMethod;
this.setMethod = setMethod; this.setMethod = setMethod;
if(getMethod != null) { if (getMethod != null) {
this.propertyType = getMethod.getReturnType(); this.propertyType = getMethod.getReturnType();
} else { }
else if (setMethod != null) {
this.propertyType = setMethod.getParameterTypes()[0]; this.propertyType = setMethod.getParameterTypes()[0];
} }
checkMethods();
} }
/** Get the property type. /** Get the property type.
...@@ -265,80 +268,109 @@ public class PropertyDescriptor extends FeatureDescriptor { ...@@ -265,80 +268,109 @@ public class PropertyDescriptor extends FeatureDescriptor {
/** Set the PropertyEditor class. If the class does not implement /** Set the PropertyEditor class. If the class does not implement
** the PropertyEditor interface, you will likely get an exception ** the PropertyEditor interface, you will likely get an exception
** late in the game. ** late in the game.
** @param propertyEditorClass the PropertyEditor class for this class to use. ** @param propertyEditorClass the PropertyEditor class for this
** class to use.
**/ **/
public void setPropertyEditorClass(Class propertyEditorClass) { public void setPropertyEditorClass(Class propertyEditorClass) {
this.propertyEditorClass = propertyEditorClass; this.propertyEditorClass = propertyEditorClass;
} }
private void findMethods(Class beanClass, String getMethodName1, String getMethodName2, String setMethodName) throws IntrospectionException { private void findMethods(Class beanClass, String getMethodName1,
String getMethodName2, String setMethodName)
throws IntrospectionException
{
try { try {
if(getMethodName1 != null) { // Try the first get method name
if (getMethodName1 != null) {
try { try {
getMethod = beanClass.getMethod(getMethodName1, new Class[0]); getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
} catch(NoSuchMethodException E) {
} }
if(getMethodName2 != null) { catch (NoSuchMethodException e) {
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; // Fall back to the second get method name
if (getMethod == null && getMethodName2 != null) {
try { try {
getMethod2 = beanClass.getMethod(getMethodName2, new Class[0]); getMethod = 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;
} }
catch (NoSuchMethodException e) {
} }
} }
// Try the set method name
if (setMethodName != null) {
if (getMethod != null) {
// If there is a get method, use its return type to help
// select the corresponding set method.
Class propertyType = getMethod.getReturnType();
if (propertyType == Void.TYPE) {
String msg = "The property's read method has return type 'void'";
throw new IntrospectionException(msg);
} }
if(getMethod != null) { Class[] setArgs = new Class[]{propertyType};
propertyType = getMethod.getReturnType();
if(setMethodName != null) {
Class[] setArgs = new Class[1];
setArgs[0] = propertyType;
try { try {
setMethod = beanClass.getMethod(setMethodName, setArgs); 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) { catch (NoSuchMethodException e) {
}
}
else if (getMethodName1 == null && getMethodName2 == null) {
// If this is a write-only property, choose the first set method
// with the required name, one parameter and return type 'void'
Method[] methods = beanClass.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(setMethodName) &&
methods[i].getParameterTypes().length == 1 &&
methods[i].getReturnType() == Void.TYPE) {
setMethod = methods[i];
break;
}
}
}
}
}
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);
}
}
private void checkMethods()
throws IntrospectionException
{
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");
} }
} }
} else if(setMethodName != null) { if (setMethod != null) {
Method[] m = beanClass.getMethods(); if (setMethod.getParameterTypes().length != 1) {
for(int i=0;i<m.length;i++) { String msg = "set method does not have exactly one parameter";
Method current = m[i]; throw new IntrospectionException(msg);
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 (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(setMethod == null) { if ((!getMethod.getDeclaringClass().
throw new IntrospectionException("Cannot find get or set methods."); isAssignableFrom(setMethod.getDeclaringClass())) &&
(!setMethod.getDeclaringClass().
isAssignableFrom(getMethod.getDeclaringClass()))) {
String msg = "set and get methods are not in the same class.";
throw new IntrospectionException(msg);
} }
} 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