JDK-6380161 : (reflect) Exception from newInstance() not chained to cause.
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-02-02
  • Updated: 2012-09-28
  • Resolved: 2012-05-17
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 8
8 b01Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
XP and Linux

A DESCRIPTION OF THE PROBLEM :
In Class.java, an InstantiationException is thrown when the constructor could not be found on a Class where newInstance() was called. When throwing this exception, the exception message is only the class name.

1) The message should be more descriptive, as it is NO useful information is transmitted.

2) The exception should be chained to the MethodNotFoundException so that the stacktrace contains the full details.

            try {
		Class[] empty = {};
                final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
                // Disable accessibility checks on the constructor
                // since we have to do the security check here anyway
                // (the stack depth is wrong for the Constructor's
                // security check to work)
                java.security.AccessController.doPrivileged
                    (new java.security.PrivilegedAction() {
                            public Object run() {
                                c.setAccessible(true);
                                return null;
                            }
                        });
                cachedConstructor = c;
            } catch (NoSuchMethodException e) {
                throw new InstantiationException(getName());
            }


REPRODUCIBILITY :
This bug can be reproduced always.

Comments
SUGGESTED FIX # HG changeset patch # User darcy # Date 1312819663 25200 # Node ID d4ab25d65adb2c36ff6b41b11f48747ed63d1ee9 # Parent 94934ebbb654ef9af85b00da3f1cf38dd6d4ff04 6380161: (reflect) Exception from newInstance() not chained to cause. Reviewed-by: dholmes, lancea, forax --- a/src/share/classes/java/lang/Class.java Mon Aug 08 13:20:16 2011 +0100 +++ b/src/share/classes/java/lang/Class.java Mon Aug 08 09:07:43 2011 -0700 @@ -349,7 +349,8 @@ public final }); cachedConstructor = c; } catch (NoSuchMethodException e) { - throw new InstantiationException(getName()); + throw (InstantiationException) + new InstantiationException(getName()).initCause(e); } } Constructor<T> tmpConstructor = cachedConstructor; @@ -973,7 +974,8 @@ public final descriptor = (String) enclosingInfo[2]; assert((name != null && descriptor != null) || name == descriptor); } catch (ClassCastException cce) { - throw new InternalError("Invalid type in enclosing method information"); + throw (InternalError) + new InternalError("Invalid type in enclosing method information").initCause(cce); } } @@ -1239,7 +1241,8 @@ public final try { return getName().substring(enclosingClass.getName().length()); } catch (IndexOutOfBoundsException ex) { - throw new InternalError("Malformed class name"); + throw (InternalError) + new InternalError("Malformed class name").initCause(ex); } } @@ -2954,9 +2957,8 @@ public final } // These can happen when users concoct enum-like classes // that don't comply with the enum spec. - catch (InvocationTargetException ex) { return null; } - catch (NoSuchMethodException ex) { return null; } - catch (IllegalAccessException ex) { return null; } + catch (InvocationTargetException | NoSuchMethodException | + IllegalAccessException ex) { return null; } } return enumConstants; }
08-08-2011

PUBLIC COMMENTS See http://hg.openjdk.java.net/jdk8/tl/jdk/rev/d4ab25d65adb
08-08-2011

EVALUATION Puzzler 80
02-02-2006