Java语言规范基于JavaSE9 第七章 包和模块(四)

来源:互联网 发布:java 泛型 静态 编辑:程序博客网 时间:2024/06/09 20:56

7.3 Compilation Units

7.3 编译单元

CompilationUnit is the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following production:

CompilationUnit是用于Java程序的句法语法(第2.3节)的目标符号(第2.1节)。它是由下面的产生式定义的:

CompilationUnit: OrdinaryCompilationUnit ModularCompilationUnit

OrdinaryCompilationUnit:
[PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

ModularCompilationUnit:
{ImportDeclaration} ModuleDeclaration

An ordinary compilation unit consists of three parts, each of which is optional:

普通编译单元包含三个部分,每个部分都是可选的:

• A package declaration (§7.4), giving the fully qualified name (§6.7) of the package to which the compilation unit belongs.

• 包声明(第7.4节)中,给出了编译单元所属包的完全限定名(第6.7节)。

A compilation unit that has no package declaration is part of an unnamed package (§7.4.2).

没有包声明的编译单元是不具名包(第7.4.2节)的一部分。

• import declarations (§7.5) that allow types from other packages and static
members of types to be referred to using their simple names.

• 导入声明(第7.5节)中,允许对来自其他包的类型和类型的state成员使用其简单名进行引用。

• Top level type declarations (§7.6) of class and interface types.

• 类和接口类型的顶层类型声明(第7.6节)。

A modular compilation unit consists of a module declaration (§7.7), optionally preceded by import declarations. The import declarations allow types from packages in this module and other modules, as well as static members of types, to be referred to using their simple names within the module declaration.

模块化编译单元由一个模块声明(第7.7节)组成,它作为一个可选项在导入声明之前声明。导入声明允许来自此模块和其他模块的包中的类型以及类型的state成员在模块声明中使用其简单名进行引用。

Every compilation unit implicitly imports every public type name declared in the predefined package java.lang, as if the declaration import java.lang.*; appeared at the beginning of each compilation unit immediately after any package declaration. As a result, the names of all those types are available as simple names in every compilation unit.

每个编译单元隐式地导入了在预定义包java.lang中的所有public类型名,就好像在每个编译单元的开头紧跟package语句后编写了import java.lang.*;声明一样。因此,所有这些类型的名称在每个编译单元中都可以用简单名访问。

The host system determines which compilation units are observable, except for the compilation units in the predefined package java and its subpackages lang and io, which are all always observable.

除了预定义包java中的所有编译单元和它的子包lang与io总是可观察的外,主机系统将确定哪些编译单元是可观察的。

Each observable compilation unit may be associated with a module, as follows:

每个可观察的编译单元可以与一个模块相关联,如下所示:

• The host system may determine that an observable ordinary compilation unit is associated with a module chosen by the host system, except for the ordinary compilation units in the predefined package java and its subpackages lang and io, which are all associated with the java.base module.

• 除了与java.base模块相关联的预定义包java以及它的子包lang和io的普通编译单元外,主机系统能够确定一个可观察的普通编译单元与主机系统选择的一个模块相关联。

• The host system must determine that an observable modular compilation unit is associated with the module declared by the modular compilation unit.

• 主机系统必须确定一个可观察的模块化编译单元与其声明的模块相关联。

The observability of a compilation unit influences the observability of its package (§7.4.3), while the association of an observable compilation unit with a module influences the observability of that module (§7.7.6).

编译单元的可观察性会影响其包的可观察性(第7.4.3节),而可观察的编译单元与模块的相关性则会影响该模块的可观察性(第7.7.6节)。

When compiling the modular and ordinary compilation units associated with a module M, the host system must respect the dependences specified in M’s declaration. Specifically, the host system must limit the ordinary compilation units that would otherwise be observable, to only those that are visible to M. The ordinary compilation units that are visible to M are the observable ordinary compilation units associated with the modules that are read by M. The modules read by M are given by the result of resolution, as described in the java.lang.module package specification, with M as the only root module. The host system must perform resolution to determine the modules read by M; it is a compile-time error if resolution fails for any of the reasons described in the java.lang.module package specification.

主机系统在编译与模块M相关联的模块化和普通编译单元时, 必须遵守模块M声明中指定的依赖关系。具体来说,主机系统必须将可以观察到的普通编译单元限制为只有模块M可见的编译单元。对模块M可见的普通编译单元是与由模块M读取的模块相关的可观察的普通编译单元。由模块M读取的模块通过解析结果给出,如java.lang.module包规范中所述的那样,模块M为唯一的根模块。主机系统必须执行解析以确定由M读取的模块; 如果解析像java.lang.module包规范中描述的任何原因而失败,那么这是一个编译时错误。

The readability relation is reflexive, so M reads itself, and thus all of the modular and ordinary compilation units associated with M are visible to M.

因为可读性关系是自反性的,所以模块M读取自身,因此与模块M相关联的所有模块化和普通编译单元对于模块M来说都是可见的。

The modules read by M drive the packages that are uniquely visible to M (§7.4.3), which in turn drives both the top level packages in scope and the meaning of package names for code in the modular and ordinary compilation units associated with M (§6.3, §6.5.3, §6.5.5).

由模块M读取的模块迫使包只对模块M唯一可见(第7.4.3节),这反过来也决定了顶层包的范围以及与模块M相关联的模块化和普通编译单元中的代码包名的含义(第6.3节、第6.5.3节、第6.5.5节)。

The rules above ensure that package/type names used in annotations in a modular compilation unit (in particular, annotations applied to the module declaration) are interpreted as if they appeared in an ordinary compilation unit associated with the module.

上面的规则确保模块化编译单元中的注解中使用的包/类型名称(特别是应用于模块声明的注解)被解释为它们出现在与该模块相关的普通编译单元中。

Types declared in different ordinary compilation units can refer to each other, circularly. A Java compiler must arrange to compile all such types at the same time.

在不同的普通编辑单元中声明的类型彼此可以循环引用。Java编译器必须妥善安排,以便可以同时编译所有这样的类型 。

原创粉丝点击