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

Method.invoke does not throw IllegalAccessException when it should

    XMLWordPrintable

Details

    • beta2
    • generic
    • generic

    Description



      Name: skR10005 Date: 05/16/2001


      The Method.invoke() method does not throw IllegalAccessException in case of
      access to protected member of superclass declared in other package via
      object of this superclass.

      The JLS 6.6.2.1. reads
        "Let C be the class in which a protected member m is declared. Access is permitted only within
        the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
           If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is
             permitted if and only if the type of the expression Q is S or a subclass of S.
           If the access is by a field access expression E.Id, where E is a Primary expression, or by a
             method invocation expression E.Id(. . .), where E is a Primary expression, then the access is
             permitted if and only if the type of E is S or a subclass of S."

      This means that inherited protected member is not accessible via object of superclass.

      The API spec for method Method.invoke reads
        "If this Method object enforces Java language access control and the underlying method is
         inaccessible, the invocation throws an IllegalAccessException."

      This means that invocation of protected member via object of superclass using Method.invoke
      should throw an IllegalAccessException. The JDK 1.4.0beta (since b51) does not throw
      any exception in described situation.

      The following example represents this failure:
      =========================B.java===============================
      package B;

      public class B {
          protected void method() {}
      }
      =========================A.java===============================
      package A;
      import B.B;
      import java.lang.reflect.Method;

      public class A extends B {
          public static void main(String[] argv) {
              int state = 0;
              B b = new B();
              A a = new A();
              try {
                  Method m = B.class.getDeclaredMethod("method", new Class[]{});
                  state = 1;
                  m.invoke(a, null);
                  state = 2;
                  m.invoke(b, null);
                  state = 3;
                  System.out.println("FAILED: " + state);
              } catch (IllegalAccessException e) {
                  System.out.println("OKAY: " + state);
              } catch (Exception e) {
                  System.out.println(e + ": " + state);
              }
          }
      }
      ==============================================================
      $java -version
      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b64)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b64, mixed mode)

      $java -cp . A.A
      FAILED: 3

      Note that removing of "m.invoke(a, null);" causes the test to pass!

      This bug affects new tests which are going to be added to merlin:
      api/java_lang/reflect/Method/descriptions.html#Invoke[Method0714]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2003]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2006]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2009]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2012]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2015]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2018]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2021]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2024]
      api/java_lang/reflect/Field/descriptions.html#GetProtectedFields[Field2027]

      ======================================================================

      Attachments

        Activity

          People

            kbr Kenneth Russell (Inactive)
            serjsunw Serj Serj (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: