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

jdk1.4.0beta-b54 Class.newInstance() incorrectly throws IllegalAccessException

    XMLWordPrintable

Details

    • generic
    • generic

    Description



      Name: poR10007 Date: 03/11/2001



      jdk1.4.0beta-b54 java.lang.Class: newInstance() incorrectly throws IllegalAccessException
      creating an instance of the class from the same package with protected nullary constructor.

      Such a behavior contradicts to the API spec for Class.newInstance
      that reads:

        "Creates a new instance of the class represented by this Class object.
         The class is instantiated as if by a new expression with an empty
         argument list."
         
      A new expression with an empty argument list should pass in this case
      (and it really passes) because of demands of JLS-2 section 6.6.1
      "Determining accessibility":

        "A member (class, interface, field, or method) of a reference (class,
         interface, or array) type or a constructor of a class type is accessible
         only if the type is accessible and the member or constructor is declared
         to permit access:
         ...
         - Otherwise, if the member or constructor is declared protected,
           then access is permitted only when one of the following is true:
           
            - Access to the member or constructor occurs from within the
              package containing the class in which the protected member
              or constructor is declared.
            - ... "

      jdk1.4.0beta-b53 java.lang.Class: newInstance() doesn't throw IllegalAccessException.

      The following test demonstrates the behavior:

      --Test.java---------------------------------------------------
      package test;

      public class Test {
          public static void main(String args[]) throws Exception {
      Subtest o = new Subtest();
      Subtest o1 = (Subtest)o.getClass().newInstance();
          }
      }
      --------------------------------------------------------------

      --Subtest.java------------------------------------------------
      package test;

      public class Subtest extends Test{
          protected Subtest() {}
      }
      --------------------------------------------------------------

      The execution log is following:

      $ jdk1.4.0beta-b54/solsparc/bin/java -version
      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b54)
      Java HotSpot(TM) Client VM (build 1.4beta-B54, mixed mode)
      $ jdk1.4.0beta-b54/solsparc/bin/javac -d . Test.java Subtest.java
      $ jdk1.4.0beta-b54/solsparc/bin/java -cp . test.Test
      Exception in thread "main" java.lang.IllegalAccessException: Class test.Test can not access a
      member of class test.Subtest with modifiers "protected"
              at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:50)
              at java.lang.Class.newInstance0(Class.java:278)
              at java.lang.Class.newInstance(Class.java:243)
              at test.Test.main(Test.java:6)
      $ jdk1.4.0beta-b53/solsparc/bin/java -cp . test.Test
      $ echo $?
      0
      $

      Justfication:
      The bug prevents Jmpp to work correctly.
      This causes JCK build failure.

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

      Name: elR10090 Date: 03/13/2001



      This bug also affects the test from testbase_nsk:

          nsk/logging/Formatter/Formatter/formatter001



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

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: