Java語言規範基於JavaSE9 第七章 包和模塊(三)

7.2 Host Support for Modules and Packages

7.2主機對模塊和包的支持

Each host system determines how modules, packages, and compilation units are created and stored.

每一個主機系統都確定了模塊、包和編譯單元是如何創建和存儲的。

Each host system determines which compilation units are observable in a particular compilation (§7.3). Each host system also determines which observable compilation units are associated with a module. The observability of compilation units associated with a module determines which modules are observable (§7.7.3) and which packages are visible within those modules (§7.4.3).

每一個主機系統都確定了哪些編譯單元在特定的編譯中是可觀察的(第7.3節)。每一個主機系統還確定了哪些可觀察的編譯單元與模塊相關聯。這些與模塊相關聯的編譯單元的可觀察性決定了哪些模塊是可觀察的(第7.7.3節),以及哪些包在這些模塊中是可見的(第7.4.3節)。

The host system is free to determine that a compilation unit which contains a module declaration is not, in fact, observable, and thus is not associated with the module declared therein. This enables a compiler to choose which directory on a modulesourcepath is “really” the embodiment of a given module. However, if the host system determines that a compilation unit which contains a module declaration is observable, then §7.4.3 mandates that the compilation unit must be associated with the module declared therein, and not with any other module.

主機系統可以自主確定包含模塊聲明的編譯單元實際上是不可觀察的,因此不與其中聲明的模塊相關聯。這使得編譯器能夠選擇模塊資源路徑上的哪個目錄是“真正”給定模塊的實例。但是,如果主機系統確定包含模塊聲明的編譯單元是可觀察的,則按照第7.4.3節中要求的,編譯單元必須與其中聲明的模塊關聯,而不是與其他任何模塊關聯。

The host system is free to determine that a compilation unit which contains a type declaration is (first) observable and (second) associated with an unnamed module or an automatic module - despite no declaration of an unnamed or automatic module existing in any compilation unit, observable or otherwise.

主機系統可以自由地確定包含類型聲明的編譯單元(第一)是可觀察的,(第二)與未命名模塊或自動模塊相關聯——儘管沒有聲明任何編譯單元中存在可觀察或其他的未命名或自動模塊。

In simple implementations of the Java SE Platform, packages and compilation units may be stored in a local file system. Other implementations may store them using a distributed file system or some form of database.

在Java SE平臺的簡單實現中,包和編譯單元可以存儲在本地文件系統中。其他實現可以用分佈式文件系統或某種形式的數據庫來存儲它們。

If a host system stores packages and compilation units in a database, then the database must not impose the optional restrictions (§7.6) on compilation units permissible in file-based implementations.

如果主機系統將包和編譯單元存儲在數據庫中,那麼數據庫不能對在基於文件的實現中所允許的編譯單元上強加任何可能的限制條件(第7.6節)。

For example, a system that uses a database to store packages may not enforce a maximum of one public class or interface per compilation unit.

例如,使用數據庫來存儲包的系統不能對每個編譯單元中公共類或接口的數量強加最大值限制。

Systems that use a database must, however, provide an option to convert a program to a form that obeys the restrictions, for purposes of export to file-based implementations.

但是,考慮到需要導出到基於文件的實現系統中,對於使用數據庫的系統,必須提供可選項,將程序轉換爲遵循某種限制的形式。

As an extremely simple example of storing packages in a file system, all the packages and source and binary code in a project might be stored in a single directory and its subdirectories. Each immediate subdirectory of this directory would represent a top level package, that is, one whose fully qualified name consists of a single simple name. Each further level of subdirectory would represent a subpackage of the package represented by the containing directory, and so on.

作爲將包存儲在文件系統中的一個極其簡單的例子,項目中所有的包、源代碼和二進制碼可以都存儲在單個目錄及其子目錄中。該目錄的直接子目錄可以表示頂層包,即完全限定名由單個簡單名構成的包。每個更深層的子目錄級別表示由包含它的目錄所表示的包的子包,以此類推。

The directory might contain the following immediate subdirectories:

該目錄可以包含下面這些直接目錄:

com
gls
jag
java
wnj

where directory java would contain the Java SE Platform packages; the directories jag, gls, and wnj might contain packages that three of the authors of this specification created for their personal use and to share with each other within this small group; and the directory com would contain packages procured from companies that used the conventions described in §6.1 to generate unique names for their packages.

其中java目錄包Java SE平臺包;jag、gls和wnj可以包含本規範的三個作者創建的用於個人使用的包,並且在這個小組中彼此共享;com目錄將包含從其他公司獲取的包,這些公司使用 了第6. l節描述的慣用法爲他們的包產生唯一的名字。

Continuing the example, the directory java would contain, among others, the following subdirectories:

繼續看這個例子,上述子目錄中的java目錄將包含下面的子目 錄:

applet
awt
io
lang
net
util

corresponding to the packages java.applet, java.awt, java.io, java.lang, java.net, and java.util that are defined as part of the Java SE Platform API.

對應的包爲:java.applet、java.awt、java.io、java.lang、java.net、 java.util,它們 是作爲Java SE平臺API的一部分而定義的。

Still continuing the example, if we were to look inside the directory util, we might see the following files:

再繼續看這個例子,在util目錄的內部可以看到下面的文件:

BitSet.java Observable.java
BitSet.class Observable.class
Date.java Observer.java
Date.class Observer.class

where each of the .java files contains the source for a compilation unit (§7.3) that contains the definition of a class or interface whose binary compiled form is contained in the corresponding .class file.

其中每個.java文件都包含用於某個編譯單元(第7.3節)的源 碼,該編譯單元包含某個類或接口的定義,而該定義的二進制編譯形式則包含在該文件對應的.class文件中。

Under this simple organization of packages, an implementation of the Java SE Platform would transform a package name into a pathname by concatenating the components of the package name, placing a file name separator (directory indicator) between adjacent components.

在這個包的簡單組織結構下,Java SE平臺的某種實現可以將包名轉換爲目錄名,方法是將包名的各個部分連接在一起,並在兩個相鄰部分之間放置一個文件名分隔符(路徑指示符)。

For example, if this simple organization were used on an operating system where the file name separator is /, the package name:

例如,如果這個簡單的組織結構用於文件名分隔符爲“/”的操作系統上,下面的包名:

jag.scrabble.board

would be transformed into the directory name:

就將被轉換爲下面的目錄名:

jag/scrabble/board

A package name component or class name might contain a character that cannot correctly appear in a host file system’s ordinary directory name, such as a Unicode character on a system that allows only ASCII characters in file names. As a convention, the character can be escaped by using, say, the @ character followed by four hexadecimal digits giving the numeric value of the character, as in the \uxxxx escape (§3.3).

包名的組成部分或類名可以包含不能在主機文件系統的普通目錄名中正確顯示的字符,例如在只允許在文件名中出現ASCII字符的系統上使用Unicode字符。作爲一種慣用法,該字符可以用@字符後面跟着四個表示該字符的十六進制數字值來轉義,就像在\uxxxx轉義中的做法一樣(第3.3節)。

Under this convention, the package name:

在這個慣用法之下,下面的包名:

children.activities.crafts.papierM\u00e2ch\u00e9

which can also be written using full Unicode as:

也可以全部由Unicode來編寫:

children.activities.crafts.papierMâché

might be mapped to the directory name:

它可以映射成下面的目錄名:

children/activities/crafts/papierM@00e2ch@00e9

If the @ character is not a valid character in a file name for some given host file system, then some other character that is not valid in a identifier could be used instead.

如果@字符在用於某些主機文件系統的文件名中不是有效字符時,那麼可以用其他在標識符中無效的字符來代替。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章