Chapter 17 Modular Applications Flashcards
1
Q
Reviewing Module Directives
A
Common module directives
-
exports <package>
Allows all modules to access the package -
exports <package> to <module>
Allows a specific module to access the package -
requires <module>
Indicates module is dependent on another module -
requires transitive <module>
Indicates the module and that all modules that use this module are dependent on another module -
uses <interface>
Indicates that a module uses a service -
provides <interface> with <class>
Indicates that a module provides an implementation of a service
2
Q
Comparing Types of Modules
A
Three types of modules:
named modules
automatic modules
unnamed modules
3
Q
CLASSPATH VS. MODULE PATH
A
- the Java runtime is capable of using class and interface types from both the classpath and the module path, although the rules for each are a bit different.
- An application can access any type in the classpath that is exposed via standard Java access modifiers, such as a public class.
- public types in the module path are not automatically available. While Java access modifiers must still be used, the type must also be in a package that is exported by the module in which it is defined. In addition, the module making use of the type must contain a dependency on the module.
4
Q
NAMED MODULES
A
- A named module is one containing a module‐info file.
- this file appears in the root of the JAR alongside one or more packages.
- Unless otherwise specified, a module is a named module.
- Named modules appear on the module path rather than the classpath.
- You’ll learn what happens if a JAR containing a module‐info file is on the classpath.
- For now, just know it is not considered a named module because it is not on the module path.
5
Q
AUTOMATIC MODULES
A
- An automatic module appears on the module path but does not contain a module‐info file.
- It is simply a regular JAR file that is placed on the module path and gets treated as a module.
- The code referencing an automatic module treats it as if there is a module‐info file present. It automatically exports all packages.
- It also determines the module name.
- When Java 9 was released, authors of Java libraries were encouraged to declare the name they intended to use for the module in the future. All they had to do was set a property called Automatic‐Module‐Name in the MANIFEST.MF file.
6
Q
ABOUT THE MANIFEST
A
- A JAR file is a zip file with a special directory named META‐INF.
- The MANIFEST.MF file is always present.
- The manifest contains extra information about the JAR file.
- For example, it often contains the version of Java used to build the JAR file. For command‐line programs, the class with the main() method is commonly specified.
- Each line in the manifest is a key/value pair separated by a colon.
- The default manifest in Java 11 looks like this:
Manifest-Version: 1.0 Created-By: 11.0.2 (Oracle Corporation)
7
Q
Determining the name of an automatic module.
A
- If the MANIFEST.MF specifies an Automatic‐Module‐Name, use that. Otherwise, proceed with the remaining rules.
- Remove the file extension from the JAR name.
- Remove any version information from the end of the name. A version is digits and dots with possible extra information at the end, for example, ‐1.0.0 or ‐1.0‐RC.
- Replace any remaining characters other than letters and numbers with dots.
- Replace any sequences of dots with a single dot.
- Remove the dot if it is the first or last character of the result.
8
Q
Practicing with automatic module names
commons2‐x‐1.0.0‐SNAPSHOT.jar
A
-
Beginning JAR name
commons2‐x‐1.0.0‐SNAPSHOT.jar
-
Remove file extenstion
commons2‐x‐1.0.0‐SNAPSHOT
-
Remove version information
commons2‐x
-
Replace special characters
commons2.x
-
Replace sequence of dots
commons2.x
-
Remove leading/trailing dots (results in the automatic module name)
commons2.x
9
Q
Practicing with automatic module names
mod_$‐1.0.jar
A
-
Beginning JAR name
mod_$‐1.0.jar
-
Remove file extenstion
mod_$‐1.0
-
Remove version information
mod_$
-
Replace special characters
mod..
-
Replace sequence of dots
mod.
-
Remove leading/trailing dots (results in the automatic module name)
mod
10
Q
UNNAMED MODULES
A
- An unnamed module appears on the classpath.
- Like an automatic module, it is a regular JAR.
- Unlike an automatic module, it is on the classpath rather than the module path.
- This means an unnamed module is treated like old code and a second‐class citizen to modules.
- An unnamed module does not usually contain a module‐info file.
- If it happens to contain one, that file will be ignored since it is on the classpath.
- Unnamed modules do not export any packages to named or automatic modules.
- The unnamed module can read from any JARs on the classpath or module path.
- You can think of an unnamed module as code that works the way Java worked before modules. Yes, we know it is confusing to have something that isn’t really a module having the word module in its name.
11
Q
COMPARING MODULE TYPES
A
test
12
Q
Analyzing JDK Dependencies
A
test
13
Q
IDENTIFYING BUILT-IN MODULES
A
test
14
Q
USING JDEPS
A
test
15
Q
ABOUT SUN.MISC.UNSAFE
A
test