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

access to another package superclass protected constructor is permitted

XMLWordPrintable

    • sparc
    • solaris_2.5.1



      Name: ngC57085 Date: 08/06/98



      (From bug report 4060948 closed as duplicate of 4066506)

      Java Virtual Machine permits to use protected constructors of the superclass
      from another package by an invocation of the method newInstance of class Class.


      JLS (6.6.2) says:

      "
      A protected member or constructor of an object may be accessed from outside
      the package in which it is declared only by code that is responsible for the
      implementation of that object. Let C be the class in which a protected member
      or constructor is declared and let S be the subclass of C in whose
      declaration the use of the protected member or constructor occurs. Then:
                  . . .
         Otherwise, if an access is of a protected constructor:
                  . . .
            If the access is by an invocation of the method newInstance of
            class Class, then the access is not permitted.
      "

      Next test shows that the access to protected constructor by an invocation of the
      method newInstance of class Class is permitted when the class where this
      constructor is used and the class where it is declared are in the different
      packages.


      > javac -d . name06801c.java
      > javac -d . name06801.java
      > java -verify name06801.name06801
      25
      failed
      > java -version
      java version "1.2beta4"
      Classic VM (build JDK-1.2beta4-K, green threads, sunwjit)

      The same result for JDK-1.2fcs-D.

      ------------------------name06801c--------------
      package name06801.name06801a;

      public class name06801c {
         protected name06801c (int a) { n = a; }
         protected name06801c () { n = n*n; }
         public int n = 5;
      }
      --------------------------------------
      ------------------------name06801--------------
      package name06801;

      import java.io.PrintStream;
      import name06801.name06801a.name06801c;

      public class name06801 {
        public static void main(String argv[]) {
           System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
        public static int run(String argv[],PrintStream out) {
           name06801d d = new name06801d ();
           if ( ! d.check(out) ) {
              out.println("failed");
              return 2/*STATUS_FAILED*/;
           }
           return 0/*STATUS_PASSED*/;
        }
      }

      class name06801d extends name06801c {
         name06801d () { super(4); }
         name06801c c1;
         Class cl;
         boolean check (PrintStream out) {
            try {
               cl = java.lang.Class.forName ("name06801.name06801a.name06801c");
            }
            catch (ClassNotFoundException e) {
               out.println(e);
               return false;
            }
            try {
               c1 = (name06801c)cl.newInstance ();
            }
            catch (Exception e) {
               out.println(e);
               return true;
            }
            out.println(c1.n);
            return false;
         }
      }
      --------------------------------------

      ----------------------------------------------------

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

            gbrachasunw Gilad Bracha (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: