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

Class.getFields() does not return fields of previously visited super interfaces/classes.

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      It is possible that Class.getFields() does not return the fields of a previously visited super class.

      As far as I can see, that is because privateGetPublicFields(...) does perform caching of already seen fields and also checks that a super interface/class is not visited multiple times. That can lead to the situation that an empty list of fields is cached because the super interfaces/classes are not visited again.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Created an object structure like this:

          interface A
              CONSTANT
          interface B extends A
          interface C extends A
          class X implements B, C

      Calling C.class.getFields() on its own will list the CONSTANT correctly. However, calling X.class.getFields() first and C.class.getFields() will not list the CONSTANT under C.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should list the constants of the class and the interface like this:

          X
           CONSTANT = 5
          
          IC
           CONSTANT = 5

      ACTUAL -
      However, it does only list the constant on the X class:

          X
           CONSTANT = 5
          
          IC


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.Field;

      public class FieldsReflectionTestMain
      {
      public static void main(String[] args) throws IllegalAccessException
      {
      listFields(X.class);
      listFields(IC.class);
      }

      private static void listFields(Class<?> pClass) throws IllegalAccessException
      {
      System.out.println(pClass.getSimpleName());

      for (Field field : pClass.getFields())
      {
      System.out.println(" " + field.getName() + " = " + field.get(null));
      }

      System.out.println();
      }

      public interface IA
      {
      public static final int CONSTANT = 5;
      }

      public interface IB extends IA
      {

      }

      public interface IC extends IA
      {

      }

      public class X implements IB, IC
      {

      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Traverse the super interfaces yourself to get a complete list.


            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: