Java Platform Module System: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile web edit
 
(14 intermediate revisions by 3 users not shown)
Line 67:
== Properties of modules ==
 
Modules are aused newto waygroup ofpackages groupingand codetightly control what packages belong to the public API. Contrary to [[JAR (file format)|Jar files]], modules explicitly declare which modules they depend on, and what packages they export.<ref>{{cite web
| url=http://openjdk.java.net/projects/jigsaw/spec/sotms/
| title=The State of the Module System
Line 75:
| accessdate=2017-02-18}}</ref> Explicit dependency declarations improve the integrity of the code, by making it easier to reason about large applications and the dependencies between software components.
 
The module declaration is placed in a file named ''{{mono|module-info.java}}'' at the root of the module’s source-file hierarchy. The JDK will verify dependencies and interactions between modules both at compile-time and runtime.
 
For example, the following module declaration declares that the module ''{{mono|com.foo.bar}}'' depends on another ''{{mono|com.foo.baz}}'' module, and exports the following packages: ''{{mono|com.foo.bar.alpha}}'' and ''{{mono|com.foo.bar.beta}}'':
<syntaxhighlight lang="cpp">
module com.foo.bar {
Line 86:
}
</syntaxhighlight>
The public members of ''{{mono|com.foo.bar.alpha}}'' and ''{{mono|com.foo.bar.beta}}'' packages will be accessible by dependent modules. Private members are inaccessible even through a means such as [[reflective programming|reflection]]. Note that in [[Java version history|Java versions]] 9 through 16, whether such 'illegal access' is ''de facto'' permitted depends on a command line setting.<ref name="JEP 396: Strongly Encapsulate JDK Internals by Default">{{cite web | url=https://openjdk.java.net/jeps/396 | title=JEP 396: Strongly Encapsulate JDK Internals by Default | access-date=2021-02-06}}</ref>
 
The JDK itself has been modularized in [[Java version history#Java SE 9|Java 9]].<ref>{{cite web
Line 99:
}}</ref> For example, the majority of the Java standard library is exported by the module <code>java.base</code>.
 
ModulesAs of Java 25, modules can themselves be imported, automatically importing all exported packages.<ref>{{Cite web|url=https://openjdk.org/jeps/494|title=JEP 494: Module Import Declarations (Second Preview)|website=openjdk.org}}</ref> This is done using <code>import module</code>. For example, <syntaxhighlight lang="Java" inline>import module java.sql;</syntaxhighlight> is equivalent to
<syntaxhighlight lang="Java">
import java.sql.*;
Line 106:
</syntaxhighlight>
Similarly, <syntaxhighlight lang="Java" inline>import module java.base;</syntaxhighlight>, similarly, imports all 54 packages belonging to <code>java.base</code>.
 
<syntaxhighlight lang="Java">
import module java.base;
 
/**
* importing module java.base allows us to avoid manually importing most classes
* the following classes (outside of java.lang) are used:
* java.text.MessageFormat
* java.util.Date
* java.util.List
* java.util.concurrent.ThreadLocalRandom
*/
public class Example {
public static void main(String[] args) {
List<String> colours = List.of("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");
IO.println(MessageFormat.format("My favourite colour is {0} and today is {1,date,long}",
colours.get(ThreadLocalRandom.current().nextInt(colours.size())),
new Date()
));
}
}
</syntaxhighlight>
 
Modules use the following [[List of Java keywords|keywords]]:
* <code>exports</code>: used in a module declaration to specify which packages are available to other modules
* <code>module</code>: declares a module
* <code>open</code>: indicates that all classes in a package are accessible via reflection by other modules
* <code>opens</code>: used to open a specific package for reflection to other modules
* <code>provides</code>: used to declare that a module provides an implementation of a service interface
* <code>requires</code>: used in a module declaration to specify that the module depends on another module
* <code>to</code>: used with the <code>opens</code> directive to specify which module is allowed to reflectively access the package
* <code>transitive</code>: used with the <code>requires</code> directive to indicate that a module not only requires another module but also makes that module's dependencies available to modules that depend on it
* <code>uses</code>: used in a module to declare that the module is using a service (i.e. it will consume a service provided by other modules)
* <code>with</code>: used with the <code>provides</code> directive to specify which implementation of a service is provided by the module
 
== Core modules ==
Line 112 ⟶ 146:
! scope="row" | {{code|java.base}}
| Defines the core APIs that form the foundation of the Java SE Platform.
Implicitly required by all modules and does not need to be declared with <code>requires</code> inside a module declaration.
|-
! scope="row" | {{code|java.compiler}}
| Defines APIs related to the language model, [[Java annotation]] processing, and the [[Java compiler]].
|-
! scope="row" | {{code|java.datatransfer}}
Line 279 ⟶ 314:
|-
! scope="row" | {{code|jdk.xml.dom}}
| Defines the JDK's subset of the [[World Wide Web Consortium]] (W3C) [[Document Object Model]] (DOM) API not covered by Java SE. Exports packages outside of the <code>java</code> namespace (from <code>org.w3c.dom</code>).
|-
! scope="row" | {{code|jdk.zipfs}}
Line 306 ⟶ 341:
* [[Classpath]]
* [[Java class loader]]
* [[Precompiled header#Modules]], (C++ modules)]]
 
== References ==
Line 322 ⟶ 357:
 
[[Category:Java specification requests]]
[[Category:Modularity]]