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

PropertyDescriptor - method not found (in case when class implements interface)

XMLWordPrintable

    • b01
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      openjdk 17.0.7 2023-04-18 LTS
      OpenJDK Runtime Environment (build 17.0.7+7-LTS)
      OpenJDK 64-Bit Server VM (build 17.0.7+7-LTS, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      In OpenJDK version 17.0.7 as opposed to 17.0.6 instantiation of PropertyDescriptor fails if the beanClass passed as a parameter implements an interface, so that the getter of related property is overriden.

      The issue does not appear in Oracle JDK 17.0.7, it affects only Open JDK versions.
      The bug could have been introduced by this PR: https://github.com/openjdk/jdk17u-dev/pull/1014


      REGRESSION : Last worked in version 17

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a generic interface accepting classes which extend Serializable as generic type and declare the getter which returns the generic type.
      Create a class holding the private variable, a corresponding getter and a setter. The class implements the above mentioned interface and overrides the getter.
      Create an application class with a main method and create a PropertyDescriptor, passing the name of private variable as the first argument and the reference to class created in the previous step.
      Run the compiled application.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Result: 42
      ACTUAL -
      An exception is thrown:
      Exception in thread "main" java.beans.IntrospectionException: Method not found: setValue
      at java.desktop/java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:114)
      at java.desktop/java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:74)
      at Demo.main(Demo.java:24)

      ---------- BEGIN SOURCE ----------
      import java.beans.PropertyDescriptor;
      import java.io.Serializable;
      import java.lang.reflect.Method;

      interface Interface<T extends Serializable> {
          T getValue();
      }

      class Entity implements Interface<Long> {
          private Long value;

          @Override
          public Long getValue() {
              return value;
          }

          public void setValue(Long value) {
              this.value = value;
          }
      }

      public class Demo {
          public static void main(String[] args) throws Exception {
              PropertyDescriptor pd = new PropertyDescriptor("value", Entity.class);
              Method method = pd.getReadMethod();
              Entity ph = new Entity();
              ph.setValue(42L);
              Object result = method.invoke(ph);
              System.out.println("Result: " + result);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      It requires changes in existing application code.
      E.g. code can be refactored so that beanClass getter does not override a getter from the parent classes.

      FREQUENCY : always


        1. Demo.java
          0.8 kB
          Praveen Narayanaswamy

            azvegint Alexander Zvegintsev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: