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

Deprecate Class.newInstance since it violates the checked exception language contract

    Details

      Description

      A DESCRIPTION OF THE REQUEST :
      Please, deprecate Class.newInstance in JDK7 and add alternative method newObjectInstance which will declare exception correctly.
      (or by wrapping exception or by adding generic throws clause)


      JUSTIFICATION :
      Class.newInstance violate checked exception definition.
      It's' allows undeclared checked excetion to be actually throwed, which is in contradiction with rules of language and cause errors.

        See also comments to
        See also http://bugs.sun.com/view_bug.do?bug_id=5066799

      related bugs:
      http://bugs.sun.com/view_bug.do?bug_id=6195208
      http://bugs.sun.com/view_bug.do?bug_id=4233093

      they 'fixed' by specifing fundamentally incorrect behaviour in documentation, which is not fix of problem.


      ---------- BEGIN SOURCE ----------
      next code frtagment

      >>public class X
      >> {
      >>
      >> public X() throws Exception
      >> {
      >> throw new Exception("hi!");
      >> }
      >>
      >> public static void f() {
      >> try {
      >> X x = X.class.newInstance();
      >> } catch (InstantiationException ex) {
      >> System.err.println("instantiation exception");
      >> ex.printStackTrace();
      >> } catch (IllegalAccessException ex) {
      >> System.err.println("illegal access exception");
      >> ex.printStackTrace();
      >> }
      >> }
      >>
      >> }
      >>

      will throw checked exception X from function X.f(), which is not declare one.
      ---------- END SOURCE ----------

        Issue Links

          Activity

          Hide
          alanb Alan Bateman added a comment -
          BT2:WORK AROUND

          Use Constructor.newInstance, as recommended in the javadoc.
          Show
          alanb Alan Bateman added a comment - BT2:WORK AROUND Use Constructor.newInstance, as recommended in the javadoc.
          Hide
          darcy Joe Darcy added a comment -
          Deprecating Class.newInstance would be a fine issues to resolve once Enhanced Deprecation (JDK-8065614) is in the platform.
          Show
          darcy Joe Darcy added a comment - Deprecating Class.newInstance would be a fine issues to resolve once Enhanced Deprecation ( JDK-8065614 ) is in the platform.
          Show
          darcy Joe Darcy added a comment - - edited Discussion thread on core-libs: http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040283.html Review threads: http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040426.html http://mail.openjdk.java.net/pipermail/security-dev/2016-April/013738.html
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/03453120a011
          User: darcy
          Date: 2016-05-03 17:41:10 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/03453120a011 User: darcy Date: 2016-05-03 17:41:10 +0000
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/03453120a011
          User: lana
          Date: 2016-05-11 16:05:08 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/03453120a011 User: lana Date: 2016-05-11 16:05:08 +0000

            People

            • Assignee:
              darcy Joe Darcy
              Reporter:
              ndcosta Nelson Dcosta
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: