Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7087876

java/beans/PropertyDescriptor.html#createPropertyEditor() throws RE if editor cannot be created

XMLWordPrintable

    • b17
    • generic
    • generic
    • Verified

      JDK : 7
      Platform: All

      The CCC request http://ccc.sfbay/4274639 introduced a new method in JavaSE 1.5:

      http://download.oracle.com/javase/7/docs/api/java/beans/PropertyDescriptor.html#createPropertyEditor%28java.lang.Object%29

      The method says:

      Returns:
          a property editor instance or null if a property editor has not been defined or cannot be created

      However the implmementation throws RuntimeExcetion if the editor has no public constructors for example.

      Please see the following code sample:

      import java.beans.IntrospectionException;
      import java.beans.PropertyDescriptor;
      import java.beans.PropertyEditorSupport;

      public class CreatePropEd {

          public static void main(String[] args) throws IntrospectionException {
              final PropertyDescriptor descriptor = new PropertyDescriptor("prop1", MyBean.class);
              descriptor.setPropertyEditorClass(PropEd_noPubConstructors.class);
              descriptor.createPropertyEditor(new Object());
          }

          public static class MyBean {
              String prop1;
              public String getProp1() { return prop1; }
              public void setProp1(String prop1) { this.prop1 = prop1; }
          }

          public static class PropEd_noPubConstructors extends PropertyEditorSupport {
              private PropEd_noPubConstructors() { }
          }
      }


      The output willl be

      Exception in thread "main" java.lang.RuntimeException: PropertyEditor not instantiated
      at java.beans.PropertyDescriptor.createPropertyEditor(PropertyDescriptor.java:459)
      at bugs.CreatePropEd.main(CreatePropEd.java:12)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
      Caused by: java.lang.IllegalAccessException: Class java.beans.PropertyDescriptor can not access a member of class bugs.CreatePropEd$PropEd_noPubConstructors with modifiers "private"
      at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
      at java.lang.Class.newInstance0(Class.java:366)
      at java.lang.Class.newInstance(Class.java:325)
      at java.beans.PropertyDescriptor.createPropertyEditor

            malenkov Sergey Malenkov (Inactive)
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: