- Java 9 Programming Blueprints
- Jason Lee
- 595字
- 2021-07-02 18:56:27
Java Platform Module System/Project Jigsaw
Despite being a solid, feature-packed release, Java 8 was considered by a fair number to be a bit disappointing. It lacked the much anticipated Java Platform Module System (JPMS), also known more colloquially, though not quite accurately, as Project Jigsaw. The Java Platform Module System was originally slated to ship with Java 7 in 2011, but it was deferred to Java 8 due to some lingering technical concerns. Project Jigsaw was started not only to finish the module system, but also to modularize the JDK itself, which would help Java SE scale down to smaller devices, such as mobile phones and embedded systems. Jigsaw was scheduled to ship with Java 8, which was released in 2014, but it was deferred yet again, as the Java architects felt they still needed more time to implement the system correctly. At long last, though, Java 9 will finally deliver this long-promised project.
That said, what exactly is it? One problem that has long haunted API developers, including the JDK architects, is the inability to hide implementation details of public APIs. A good example from the JDK of private classes that developers should not be using directly is the com.sun.*/sun.* packages and classes. A perfect example of this--of private APIs finding widespread public use--is the sun.misc.Unsafe class. Other than a strongly worded warning in Javadoc about not using these internal classes, there's little that could be done to prevent their use. Until now.
With the JPMS, developers will be able to make implementation classes public so that they may be easily used inside their projects, but not expose them outside the module, meaning they are not exposed to consumers of the API or library. To do this, the Java architects have introduced a new file, module-info.java, similar to the existing package-info.java file, found at the root of the module, for example, at src/main/java/module-info.java. It is compiled to module-info.class, and is available at runtime via reflection and the new java.lang.Module class.
So what does this file do, and what does it look like? Java developers can use this file to name the module, list its dependencies, and express to the system, both compile and runtime, which packages are exported to the world. For example, suppose, in our preceding stream example, we have three packages: model, api, and impl. We want to expose the models and the API classes, but not any of the implementation classes. Our module-info.java file may look something like this:
module com.packt.j9blueprints.intro { requires com.foo; exports com.packt.j9blueprints.intro.model; exports com.packt.j9blueprints.intro.api; }
This definition exposes the two packages we want to export, and also declares a dependency on the com.foo module. If this module is not available at compile-time, the project will not build, and if it is not available at runtime, the system will throw an exception and exit. Note that the requires statement does not specify a version. This is intentional, as it was decided not to tackle the version-selection issue as part of the module system, leaving that to more appropriate systems, such as build tools and containers.
Much more could be said about the module system, of course, but an exhaustive discussion of all of its features and limitations is beyond the scope of this book. We will be implementing our applications as modules, though, so we'll see the system used--and perhaps explained in a bit more detail--throughout the book.
- JavaScript前端開發(fā)模塊化教程
- 數(shù)字媒體應用教程
- 測試驅(qū)動開發(fā):入門、實戰(zhàn)與進階
- Python爬蟲開發(fā):從入門到實戰(zhàn)(微課版)
- Linux網(wǎng)絡程序設計:基于龍芯平臺
- Full-Stack Vue.js 2 and Laravel 5
- 數(shù)據(jù)結構與算法分析(C++語言版)
- Learning Modular Java Programming
- SQL Server 入門很輕松(微課超值版)
- Python預測分析實戰(zhàn)
- 現(xiàn)代CPU性能分析與優(yōu)化
- 實驗編程:PsychoPy從入門到精通
- Beginning C# 7 Hands-On:The Core Language
- Qt 5.12實戰(zhàn)
- Getting Started with the Lazarus IDE