JDK-8250226 : Unspecified exception thrown by Class::getDeclaringClass and Class::getEnclosingClass if the outer class is not present
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 8
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2020-07-23
  • Updated: 2025-01-21
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.
Other
tbdUnresolved
Related Reports
Relates :  
Description
public class X {
    public class Y {} 
}

The outer class of Y is X.  `Y.class.getDeclaringClass()` returns `Class` instance representing X.

The behavior is not specified if X cannot be found.   The current implementation throws NCDFE.
 
Exception in thread "main" java.lang.NoClassDefFoundError: X
	at java.base/java.lang.Class.getDeclaringClass0(Native Method)
	at java.base/java.lang.Class.getEnclosingClass(Class.java:1536)
	at Test.main(Test.java:5)
Caused by: java.lang.ClassNotFoundException: X
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 3 more

The APIs whose implementation inspects `InnerAttributes` or `EnclosingMethod` method have simiar issue such as:
Class::getDeclaringClass, Class::getEnclosingClass, Class::getDeclaredClasses 
Comments
This behavior was introduced by JDK-8057919.
30-11-2020