JDK-6850612 : Deprecate Class.newInstance since it violates the checked exception language contract
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2009-06-12
  • Updated: 2022-03-15
  • Resolved: 2016-05-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b118Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Sub Tasks
JDK-8155516 :  
JDK-8155872 :  
JDK-8159330 :  
JDK-8168681 :  
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 ----------

Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/03453120a011 User: lana Date: 2016-05-11 16:05:08 +0000
11-05-2016

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/03453120a011 User: darcy Date: 2016-05-03 17:41:10 +0000
03-05-2016

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
21-04-2016

Deprecating Class.newInstance would be a fine issues to resolve once Enhanced Deprecation (JDK-8065614) is in the platform.
02-12-2015

WORK AROUND Use Constructor.newInstance, as recommended in the javadoc.
14-06-2009