System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə68/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   64   65   66   67   68   69   70   71   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

specific set of modules, not all other modules. For example, a library developer might
want to export a support package to certain other modules within the library, but not
make it available for general use. Adding a to clause to the exports statement provides
a means by which this can be accomplished.
In an exports statement, the to clause specifies a list of one or more modules that have
access to the exported package. Furthermore, only those modules named in the to
clause will have access. In the language of modules, the to clause creates what is known
as a qualified export.
The form of exports that includes to is shown here:
exports packageName to moduleNames;
Here, moduleNames is a comma­separated list of modules to which the exporting
module grants access.
You can try the to clause by changing the module­info.java file for the appfuncs
module, as shown here:
Now, simplefuncs is exported only to appstart and to no other modules. After
making this change, you can recompile the application by using this javac command:
After compiling, you can run the application as shown earlier.
This example also uses another module­related feature. Look closely at the preceding
javac command. First, notice that it specifies the ­­module­source­path option. The
module source path specifies the top of the module source tree. The ­­module­
source­path option automatically compiles the files in the tree under the specified


directory, which is appsrc in this example. The ­­module­source­path option must
be used with the ­d option to ensure that the compiled modules are stored in their
proper directories under appmodules. This form of javac is called multimodule mode
because it enables more than one module to be compiled at a time. The multimodule
compilation mode is especially helpful here because the to clause refers to a specific
module, and the requiring module must have access to the exported package. Thus, in
this case, both appstart and appfuncs are needed to avoid warnings and/or errors
during compilation. Multimodule mode avoids this problem because both modules are
being compiled at the same time.
The multimodule mode of javac has another advantage. It automatically finds and
compiles all source files for the application, creating the necessary output directories.
Because of the advantages that multimodule compilation mode offers, it will be used for
the subsequent examples.
NOTE
As a general rule, qualified export is a special case feature. Most often, your modules
will either provide unqualified export of a package or not export the package at all,
keeping it inaccessible. As such, qualified export is discussed here primarily for the sake
of completeness. Also, qualified export by itself does not prevent the exported package
from being misused by malicious code in a module that masquerades as the targeted
module. The security techniques required to prevent this from happening are beyond
the scope of this book. Consult the Oracle documentation for details on security in this
regard and Java security details in general.
USING REQUIRES TRANSITIVE
Consider a situation in which there are three modules, A, B, and C, that have the
following dependences:

A requires B.

B requires C.
Given this situation, it is clear that since A depends on B and B depends on C, A has an
indirect dependence on C. As long as A does not directly use any of the contents of C,
then you can simply have A require B in its module­info file, and have B export the


packages required by A in its module­info file, as shown here:
Here, somepack is a placeholder for the package exported by B and used by A. Although
this works as long as A does not need to use anything defined in C, a problem occurs if
does want to access a type in C. In this case, there are two solutions.
The first solution is to simply add a requires C statement to A’s file, as shown here:
This solution certainly works, but if B will be used by many modules, you must add
requires C to all module definitions that require B. This is not only tedious; it is also
error prone. Fortunately, there is a better solution. You can create an implied

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   64   65   66   67   68   69   70   71   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə