Whereas the Java grammar seems very precisely described in JLS specifications, there are some concrete cases which I fail to apply on given definitions.
For example, taking the ClassInstanceCreationExpression
rule in chapter 15.9 of JLS8, non-qualified new
expressions should be of the form:
new [TypeArguments] {Annotation} Identifier [TypeArgumentsOrDiamond] ( [ArgumentList] ) [ClassBody]
Identifier
being a standard Java identifier (basically Java letters / numbers, no dot).
How does this definition would apply to valid expressions like static nested classes instanciation:
new C1.C2();
or package-qualified classes instanciation:
new java.lang.String("foo");
given that dots cannot be part of an Identifier
?
Note that there was a change on this definition from JLS7 to JLS8, where JLS7 was stating, for non-qualified new
expressions:
new [TypeArguments] TypeDeclSpecifier [TypeArgumentsOrDiamond]( [ArgumentList] ) [ClassBody]
TypeDeclSpecifier
being defined as:
TypeDeclSpecifier:
TypeName
ClassOrInterfaceType . Identifier
allowing non-qualified new
expressions for static nested classes and package-qualified classes.
It seems to be a bug in the specification.
Quoting from the bug report linked to above (third point in the description):
- The grammar doesn't define any production for the following expression: new java.security.Permissions()
This is a side-effect of removing
TypeDeclSpecifier
in 4.3, as it interacted poorly with type annotations. The JSR 308 Public Review noted: "TypeDeclSpecifier is one of the more obscure nonterminals in the The Java Language Specification. It is used in only a handful of situations: the extends and implements clauses of a class declaration (8.1.4, 8.1.5), the extends clause of an interface declaration (9.1.3), and the syntax of a class instance creation expression (15.9). The reason for its use is to prohibit wildcard type arguments (though 9.1.3 actually fails to do so), but this can be achieved equally well without a dedicated non-terminal."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With