Summary
javac will reject class
files with bad EnclosingMethod
attributes.
Problem
A class
file may have an EnclosingMethod
attribute that is syntactically well-formed (according to the JVM's class
file format checking) but semantically ill-formed (according to the rules of the Java language). This may cause a Java compiler to crash. A notable instance of this problem is when EnclosingMethod
specifies an enclosing class that could not have enclosed the current class in Java source code. That is, in a class
file for a local/anonymous class A, EnclosingMethod
specifies that the enclosing class is C, but the binary name of C is not a prefix of the binary name of A.
Solution
The EnclosingMethod
attribute is defined by JVMS 4.7.7 to record the class (and method) that enclosed the source declaration of the current class, which was local/anonymous. The rules in JLS 13.1 indicate that a local/anonymous class has a binary name which is an extension of the binary name of its enclosing class. Accordingly, if the EnclosingMethod
attribute is present in the current class
file, then its class_index
item should specify a binary name for the enclosing class which is a prefix of the binary name of the current class. If class_index
does not follow this pattern, then the attribute is not semantically well-formed, and Java compilers should reject the class
file.
Specification
javac will check if the binary name specified by class_index
in EnclosingMethod
is a prefix of the binary name of the class
file that contains EnclosingMethod
. If not, then javac will refuse to load the class
file.
- csr of
-
JDK-8215407 javac should reject class files with bad EnclosingMethod attributes
-
- Resolved
-