JVMS (4.4.6) says that the 'name' portion of a NameAndType may represent "either a valid unqualified name denoting a field or method, or the special method name <init>", while the 'descriptor' portion may be "a valid field descriptor or method descriptor".
Per 4.2.2, names that include '<' are valid unqualified field names. This includes the names '<init>' and '<clinit>', which should be fully usable as names of fields.
During format checking of a NameAndType constant (setting aside whether it is referenced by any other constant), HotSpot seems to consider the name and descriptor in tandem, and enforce different rules for the name based on the descriptor. Some examples:
<init>:()I --> CFE
<clinit>:()I --> CFE
<clinit>:()V --> no error
<finit>:()I --> CFE
This is inconsistent with the spec, and should be cleaned up. Given a standalone NameAndType, any valid unqualified name should be accepted for 'name', and any valid field or method descriptor should be accepted for 'descriptor'.
*References* to NameAndType constants enforce some additional constraints:
- From a Fieldref, the descriptor must be a field descriptor
- From a Methodref, the name must be a valid method name and must not be '<clinit>'. The descriptor must be a method descriptor, and if the name is '<init>', the descriptor must return 'V'.
- Same for InterfaceMethodref.
- From a Dynamic, the descriptor must be a field descriptor
- From an InvokeDynamic, the descriptor must be a method descriptor. (Should the name be restricted? I think so, but the spec doesn't say so for now, so that's a separate issue.)
Per 4.2.2, names that include '<' are valid unqualified field names. This includes the names '<init>' and '<clinit>', which should be fully usable as names of fields.
During format checking of a NameAndType constant (setting aside whether it is referenced by any other constant), HotSpot seems to consider the name and descriptor in tandem, and enforce different rules for the name based on the descriptor. Some examples:
<init>:()I --> CFE
<clinit>:()I --> CFE
<clinit>:()V --> no error
<finit>:()I --> CFE
This is inconsistent with the spec, and should be cleaned up. Given a standalone NameAndType, any valid unqualified name should be accepted for 'name', and any valid field or method descriptor should be accepted for 'descriptor'.
*References* to NameAndType constants enforce some additional constraints:
- From a Fieldref, the descriptor must be a field descriptor
- From a Methodref, the name must be a valid method name and must not be '<clinit>'. The descriptor must be a method descriptor, and if the name is '<init>', the descriptor must return 'V'.
- Same for InterfaceMethodref.
- From a Dynamic, the descriptor must be a field descriptor
- From an InvokeDynamic, the descriptor must be a method descriptor. (Should the name be restricted? I think so, but the spec doesn't say so for now, so that's a separate issue.)
- relates to
-
JDK-8323436 4.4.10: Restrict invokedynamic constant names
- Open
-
JDK-8268720 Unspecified checks on NameAndType constants should not be performed
- Resolved
-
JDK-8233854 4.8: Clarify which checks occur during format checking
- In Progress