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

Doc of PropertyDescriptor.getPropertyType() is not clear about the returning Type

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      I have a class(Bean) that has a private Object field called "value", and public getter/setter.
      Another class(StringBean) extends this class, and overrides the getter to return String.
      And the getPropertyType() for value in StringBean is Object. Should it be String?

      https://docs.oracle.com/javase/8/docs/api/java/beans/PropertyDescriptor.html

      Doc: "Returns the Java type info for the property. Note that the Class object may describe primitive Java types such as int. This type is returned by the read method or is used as the parameter type of the write method. Returns null if the type is an indexed property that does not support non-indexed access."

      ACTUAL VS EXPECTED BEHAVIOR
      ACTUAL -
      Exception in thread "main" java.lang.RuntimeException: Incorrect property type: expected String but got java.lang.Object
      at PropertyTypeTest.main(PropertyTypeTest.java:32)
      EXPECTED -
      Property type equals to String.class

      ---------- BEGIN SOURCE ----------

      import java.beans.Introspector;
      import java.beans.PropertyDescriptor;

      public class PropertyTypeTest {
          public static class BaseBean {
              private Object value;
              public Object getValue() {
                  return this.value;
              }
              public void setValue(Object value) {
                  this.value = value;
              }
          }

          public static class StringBean extends BaseBean {
              @Override
              public String getValue() {
                  return (String) super.getValue();
              }
          }

          public static void main(String[] args) throws Exception {
              PropertyDescriptor[] pds = Introspector.getBeanInfo(StringBean.class).getPropertyDescriptors();
              PropertyDescriptor pd = null;
              for (PropertyDescriptor desc : pds) {
                  if (desc.getName().equals("value")) {
                      pd = desc;
                  }
              }
              if (pd != null && !String.class.equals(pd.getPropertyType())) {
                  throw new RuntimeException("Incorrect property type: expected String but got " +
                                  (pd.getPropertyType() == null ? "null" : pd.getPropertyType().getName()));
              }
          }
      }

      ---------- END SOURCE ----------

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

              Created:
              Updated: