Field and Method cannot check accessibility against referenced class

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 1.1, 1.2.2, 5.0, 25
    • Component/s: core-libs
    • Fix Understood
    • generic
    • generic

      java.lang.reflect.Field (get* and set*) and Method (invoke) base their access check on the declaring class. This is contrary to the JLS, which defines accessibility in terms of the reference type. Consider the following:

      foo/Y.java:

      package foo;

      class X {
          public int i;
      }

      public class Y extends X {
      }

      bar/T.java:

      package bar;

      import foo.Y;

      public class T {
          public static void main(String[] args) {
      Y y = new Y();
      int i = y.i;
      try {
      y.getClass().getField("i").getInt(y);
      } catch (Exception e) {
      e.printStackTrace();
      }
          }
      }

      The Field object from getField has no knowledge that it is accessed through a reference Y.i, which can refer to this field.

      At runtime, a workaround is to use MethodHandles.Lookup.findXxx methods, which correctly checks against the referenced class.

            Assignee:
            Chen Liang
            Reporter:
            Bob Scheifler (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: