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

Prepended classes don't follow the accessibility policies

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.2_08
    • hotspot
    • None
    • generic
    • generic

      If a class is explicitly prepended (or if a class is in rt.jar which is prepended by default), we can't access package declared methods anymore. In other words, it doesn't follow the accessibility policies anymore.

      How to reproduce:
      -----------------
      In the following examples, the package sun.util.calendar has been choosen, because it helps to understand the real world example better.

      $ java -version
      java version "1.4.2_08"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)

      $ cat sun/util/calendar/A.java
      package sun.util.calendar;
      public class A {

        public A() {
          System.out.println(B.getZoneAliases());
        }

        public static void main(String[] args) {
           new A();
        }
      }

      $ cat sun/util/calendar/B.java
      package sun.util.calendar;
      import java.util.*;

      public class B {

        static HashMap getZoneAliases() {
           return new HashMap();
        }
      }


      $ javac sun/util/calendar/*.java
      $ java sun/util/calendar/A
      {}

      => accessing a static package declared method works,
      we can access the method called B.getZoneAliases() from A.

      $ jar cfv b.jar sun/util/calendar/B.class
      added manifest
      adding: sun/util/calendar/B.class(in = 309) (out= 233)(deflated 24%)

      => If we prepend the B class (stored in b.jar now), we can not access B.getZoneAliases() from A anymore:

      $ java -Xbootclasspath/p:b.jar sun/util/calendar/A
      Exception in thread "main" java.lang.IllegalAccessError: tried to access method sun.util.calendar.B.getZoneAliases()Ljava/util/HashMap; from class sun.util.calendar.A
              at sun.util.calendar.A.<init>(A.java:5)
              at sun.util.calendar.A.main(A.java:9)

      => Prepended classes don't follow the accessibility policies, they behave differently.

      A real world example:
      ---------------------
      $ cat sun/util/calendar/C.java
      package sun.util.calendar;
      public class C {

        public C() {
          System.out.println(ZoneInfoFile.getZoneAliases());
        }

        public static void main(String[] args) {
           new C();
        }
      }


      $ javac sun/util/calendar/C.java
      $ java sun/util/calendar/C
      Exception in thread "main" java.lang.IllegalAccessError: tried to access method sun.util.calendar.ZoneInfoFile.getZoneAliases()Ljava/util/HashMap; from class sun.util.calendar.C
              at sun.util.calendar.C.<init>(C.java:5)
              at sun.util.calendar.C.main(C.java:9)


      => We can not access the package static declared method called ZoneInfoFile.getZoneAliases() from C.

      ###@###.### 2005-07-05 12:38:57 GMT
      ###@###.### 2005-07-05 13:30:12 GMT

            acorn Karen Kinnear (Inactive)
            jloefflm Johann Löfflmann (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: