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

(reflect spec) Clarify Class.newInstance() behavior for exceptions

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6
    • core-libs

      A licensee requests to clarify the behavior of newInstance() in API Document.

      Please execute the following test case, you will see the IOException.
      You will see IOException.

      --- Test Case --->
      import java.io.IOException;

      public class Test {

          public Test() throws IOException {
              if (true) throw new IOException();
          }

          public static void main(String[] args) {
              try {
                  Object obj = Class.forName("Test").newInstance();
              } catch(Throwable t) {
                  t.printStackTrace();
              }
          }
      }
      <-----------------


      According to the API document of Class#newInstance(),
      .....
      Throws:
          IllegalAccessException - if the class or its nullary constructor is not accessible.
          InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type,
          or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
          ExceptionInInitializerError - if the initialization provoked by this method fails.
          SecurityException - If a security manager, s, is present and any of the following conditions is met:

              * invocation of s.checkMemberAccess(this, Member.PUBLIC) denies creation of new instances of this class
              * the caller's class loader is not the same as or an ancestor of the class loader for the current class and
                 invocation of s.checkPackageAccess() denies access to the package of this class
      .....


       The newInstance() seems to throw only IllegalAccessException, InstantiationException,
       ExceptionInInitializerError and SecurityException.
       However, as the above test case shows, other exceptions can be thrown.
      (In the above test case, IOException occurs.)
       This behavior can not be found in API documents.

      PROBLEM:
        The above test case simulates, if IOexception (or others) is implemented in constructor,
        there might occur the exception when the constructor is invoked by newInstance().

        This fact can not be found in API document. The licensee considers it problem.

      REQUEST :
         To clarify the above-mentioned behavior is from newInstance() specifications ?
         (if so, to mention explicitly in APU document.)

      NOTE:
         What the licensee is afraid are,
           - the behavior is from newInstance() specifications and will be supported in future release.
         Otherwise,
           - that just depends on the current JRE implementation and may not be supported in future release
             without announce.

      CONFIGURATION:
         OS : WindowsXP(SP1, Japanese)
         JRE : 1.4.2/5.0/6.0b11
      ###@###.### 2004-11-15 06:24:55 GMT

            iris Iris Clark
            tbaba Tadayuki Baba (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: