简体   繁体   English

Java语法定义的完整性

[英]Java grammar definition completeness

Whereas the Java grammar seems very precisely described in JLS specifications, there are some concrete cases which I fail to apply on given definitions. 尽管Java语法在JLS规范中似乎非常精确地描述了,但在某些具体情况下我无法应用到给定的定义中。

For example, taking the ClassInstanceCreationExpression rule in chapter 15.9 of JLS8, non-qualified new expressions should be of the form: 例如,采用JLS8第15.9章中的ClassInstanceCreationExpression规则,不合格的new表达式应采用以下形式:

new [TypeArguments] {Annotation} Identifier [TypeArgumentsOrDiamond] ( [ArgumentList] ) [ClassBody] 

Identifier being a standard Java identifier (basically Java letters / numbers, no dot). Identifier是标准的Java标识符(基本上是Java字母/数字, 没有点)。

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 ? 鉴于点不能成为Identifier一部分?

Note that there was a change on this definition from JLS7 to JLS8, where JLS7 was stating, for non-qualified new expressions: 请注意,对于不合格的new表达式,此定义从JLS7更改为JLS7所声明的JLS8:

new [TypeArguments] TypeDeclSpecifier [TypeArgumentsOrDiamond]( [ArgumentList] ) [ClassBody]

TypeDeclSpecifier being defined as: TypeDeclSpecifier定义为:

TypeDeclSpecifier:
    TypeName
    ClassOrInterfaceType . Identifier 

allowing non-qualified new expressions for static nested classes and package-qualified classes. 允许静态嵌套类和包限定类使用非限定new表达式。

It seems to be a bug in the specification. 这似乎是规范中的错误

Quoting from the bug report linked to above (third point in the description): 引用以上链接的错误报告进行引用(描述中的第三点):

  1. The grammar doesn't define any production for the following expression: new java.security.Permissions() 语法没有为以下表达式定义任何形式:new java.security.Permissions()

This is a side-effect of removing TypeDeclSpecifier in 4.3, as it interacted poorly with type annotations. 这是在4.3中删除TypeDeclSpecifier ,因为它与类型注释的交互作用很差。 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." JSR 308公共评论指出:“ TypeDeclSpecifier是Java语言规范中比较模糊的非终结符之一。仅在少数情况下使用:类声明的extends和Implements子句(8.1.4,8.1.5) ),接口声明的extends子句(9.1.3)和类实例创建表达式的语法(15.9),其使用原因是禁止通配符类型参数(尽管9.1.3实际上没有这样做) ,但无需专用的非终端设备也可以同样好地实现。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM