I'm checking this in.
This adds a new 1.5 method to java.beans.
Tom
2006-03-04 Tom Tromey <[EMAIL PROTECTED]>
* java/beans/PropertyDescriptor.java (createPropertyEditor): New
method.
(findConstructor): Likewise.
(instantiateClass): Likewise.
Index: java/beans/PropertyDescriptor.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/beans/PropertyDescriptor.java,v
retrieving revision 1.16
diff -u -r1.16 PropertyDescriptor.java
--- java/beans/PropertyDescriptor.java 29 Sep 2005 22:35:39 -0000 1.16
+++ java/beans/PropertyDescriptor.java 4 Mar 2006 20:24:30 -0000
@@ -37,6 +37,8 @@
package java.beans;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
@@ -344,6 +346,71 @@
this.propertyEditorClass = propertyEditorClass;
}
+ /**
+ * Instantiate a property editor using the property editor class.
+ * If no property editor class has been set, this will return null.
+ * If the editor class has a public constructor which takes a single
+ * argument, that will be used and the bean parameter will be passed
+ * to it. Otherwise, a public no-argument constructor will be used,
+ * if available. This method will return null if no constructor is
+ * found or if construction fails for any reason.
+ * @param bean the argument to the constructor
+ * @return a new PropertyEditor, or null on error
+ * @since 1.5
+ */
+ public PropertyEditor createPropertyEditor(Object bean)
+ {
+ if (propertyEditorClass == null)
+ return null;
+ Constructor c = findConstructor(propertyEditorClass,
+ new Class[] { Object.class });
+ if (c != null)
+ return instantiateClass(c, new Object[] { bean });
+ c = findConstructor(propertyEditorClass, null);
+ if (c != null)
+ return instantiateClass(c, null);
+ return null;
+ }
+
+ // Helper method to look up a constructor and return null if it is not
+ // found.
+ private Constructor findConstructor(Class k, Class[] argTypes)
+ {
+ try
+ {
+ return k.getConstructor(argTypes);
+ }
+ catch (NoSuchMethodException _)
+ {
+ return null;
+ }
+ }
+
+ // Helper method to instantiate an object but return null on error.
+ private PropertyEditor instantiateClass(Constructor c, Object[] args)
+ {
+ try
+ {
+ return (PropertyEditor) c.newInstance(args);
+ }
+ catch (InstantiationException _)
+ {
+ return null;
+ }
+ catch (InvocationTargetException _)
+ {
+ return null;
+ }
+ catch (IllegalAccessException _)
+ {
+ return null;
+ }
+ catch (ClassCastException _)
+ {
+ return null;
+ }
+ }
+
private void findMethods(
Class beanClass,
String getMethodName1,