-
Bug
-
Resolution: Duplicate
-
P3
-
None
-
1.4.0
-
x86
-
windows_2000
Name: nt126004 Date: 02/06/2003
FULL PRODUCT VERSION :
java version "1.4.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_03-b04)
Java HotSpot(TM) Client VM (build 1.4.0_03-b04, mixed mode)
FULL OPERATING SYSTEM VERSION : Microsoft Windows 2000
[Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
If a class has a non-default no-arg constructor that is
declared to throw a checked exception, then calling
Class.newInstance() on that class causes that checked
exception to be thrown, violating the method signature for
Class.newInstance(). This can be used to cause an arbitrary
method to throw a checked exception even though it is not
declared in its signature.
Note that, in contrast, Constructor.newInstance() throws an
InvocationTargetException when the constructor throws an
exception (checked or unchecked).
I suggest that the cleanest fix to this issue would be to
modify Class.newInstance() to be declared to throw
InvocationTargetException as well. However, I suspect this
would break too much existing code.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. javac Test.java // See source code below
2. java Test
3.
EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected: compile-time error
Actual: The method doit() throws a checked exception that it
is not declared to throw
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java Test
Test$TestException: abcde
at Test.throwException(Test.java:7)
at Test.<init>(Test.java:3)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance
(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:296)
at java.lang.Class.newInstance(Class.java:249)
at Test.doit(Test.java:15)
at Test.main(Test.java:23)
Exception in thread "main"
Note how doit() throws this checked exception, in violation of its
method signature.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class Test {
public Test() throws TestException {
this.throwException();
}
public void throwException() throws TestException {
throw new TestException("abcde");
}
// This is not declared to throw TestException, but it does throw
// it anyway.
static void doit() {
try {
Class clazz = Class.forName("Test");
Test o = (Test) clazz.newInstance();
}
// Ignore all these
catch (ClassNotFoundException cnfe) { }
catch (IllegalAccessException iae) { }
catch (InstantiationException ie) { }
}
public static void main(String args[]) { doit(); }
static class TestException extends Exception {
TestException(String message) { super(message); }
}
}
---------- END SOURCE ----------
(Review ID: 180896)
======================================================================
- duplicates
-
JDK-4233093 (reflect spec) Class.newInstance() illegally throws undeclared Exceptions
-
- Closed
-