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

newInstance should throw IllegalAccessException when constructor is inaccessible

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • None
    • x86, sparc
    • solaris_8

      java.lang.Class.newInstance() is supposed to throw an IllegalAccessException
      if the class that invokes it has an inaccessible constructor. For instance,
      the constructor Class() in java.lang.Class is private. The following code
      should set state to 3.

      Instead, the kestrel-solaris JDK is throwing an InstantiationException (an
      exception that should be thrown if the class one wishes to instantiate is
      abstract or an interface).

      public class X {
          public static void main(String[] args) {
              Class cl=null;
              Object o;
              int state=0;
              try {
                  cl = Class.forName("java.lang.Class");
              }catch (ClassNotFoundException e) {
                  state = 1;
              }
              // If we find the class try and create a new instance
              if (state == 0) {
                  try {
                      // we should not be able to instantiate Class
                      // as it has a private constructor
                      o = cl.newInstance();
                  }catch (InstantiationException e) {
                      System.out.println(e.getMessage());
                      state = 2;
                  }catch (IllegalAccessException e) {
                      System.out.println(e.getMessage());
                      state = 3;
                  }
              }
              System.out.println("state="+state);
          }
      }

      The problem was discovered with Plum Hall test
      plumhall-99a/jvs/lang/Class/newInstance_01_x


      allan.jacobs@Eng 2000-04-06

            foliversunw Fred Oliver (Inactive)
            ajacobssunw Allan Jacobs (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: