Java 9 模块化开发实践

Java 9 模块化开发实践

6. Java Project Jigsaw , Java Platform, Oracle Sept, 2017 Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
7. Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3
8. – Principal Member of Technical Staff, OpenJDK Committer, Oracle Java – Java Java – – IBM CDL “Java Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
9. 1 2 Jigsaw Java 3 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 5
10. 1 2 Jigsaw Java 3 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 6
11. Java • classpath (Jar Hell) – Jar – – – classloading • Massive Monolithic JDK Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7
12. Jigsaw Java Platform Module System(JPMS) • : • – – classpath (encapsulation) : • – – – – Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 8
13. Jigsaw • JSR 376: Java Platform Module System – JEP 200: The Modular JDK – JEP 201: Modular Source Code – JEP 220: Modular Run-Time Images – JEP 260: Encapsulate Most Internal APIs – JEP 261: Module System – JEP 282: jlink: The Java Linker “ JEP (Java Enhancement Proposal) Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 9
14. Java • • javac/java: – – API module path • jlink – Java – linking Phase Java Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10
15. java.se.ee Java SE java.se java.xml.ws java.corba java.jnlp java.xml.crypto java.xml.bind java.transaction java.sql.rowset java.destkop java.activation java.sql java.security.sasl java.security.jgss java.naming java.compiler java.scripting java.instrument java.logging java.prefs java.transfer java.rmi java.xml java.management.rmi java.xml.ws.annotation java.smartcardio java.management java.base Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
16. 1 2 Jigsaw Java 3 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12
17. Java • Module – Unnamed module – Automatic module • Accessibility – Open module/package • Readability •… Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13
18. Java (1) • (Explicit) Named modules • Automatic named modules – – – Manifest Maven group identifier JAR • Unnamed modules Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14
19. Java (2) • – Unnamed modules: • Reads all modules • Exports all its packages named modules unnamed modules – Automatic named modules: • Reads all modules • Exports all its packages Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 15
20. Java • • – – Module Descriptor(module-info.java) • – Module jar – JMOD – module-info.class native Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 16
21. Module Descriptor module java.naming { requires java.security.sasl; package public exports javax.naming; exports javax.naming.directory; exports javax.naming.event; package public exports javax.naming.ldap; exports javax.naming.spi; exports com.sun.jndi.toolkit.ctx to jdk.naming.dns; exports com.sun.jndi.toolkit.url to java.corba, jdk.naming.dns, jdk.naming.rmi; uses javax.naming.ldap.StartTlsResponse; Service Provider uses javax.naming.spi.InitialContextFactory; provides java.security.Provider with sun.security.provider.certpath.ldap.JdkLDAP; } Service Provider Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 17
22. Accessibility (JDK 9) • public to everyone • public but only to specific modules • public only within a module • protected • <package> • private : --add-exports <source-module>/<package>=<target-module>(,<target-module>)* Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 18
23. Readability --add-reads <source-module>=<target-module> Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 19
24. (Reflection) • Deep Reflection (1) access non-public elements – – Java 9 ‘setAccessible()’ – Java 9 Java 8 – Kill switch: --permit-illegal-access Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 20
25. (Reflection) • (2) : – Open modules – Open packages – “--add-opens” Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 21
26. 1 2 Jigsaw Java 3 Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 22
27. • java --list-modules java --list-modules java.se • javac --module-path mod_dir \ --add-modules java.desktop \ --module-source-path src_dir java_files • java --module-path mod_dir -m my_mod/MainClass Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 23
28. JPMS JPMS Module Name Module Descriptor Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 24
29. classpath • unnamed modules JDK 9 , – – : • Jar – module path, Java automatic modules • – named modules Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 25
30. • : Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 26
31. • module descriptor • Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
32. 1 • JPMS – • circling references • “split packages” – JDK • tools.jar rt.jar – – • JDK Copyright © 2017, Oracle and/or its affiliates. All rights reserved. JDK 9 JRT 28
33. JDK • 2 API – • • $: jdeps --jdk-internals --class-path class_dir – • • --add-exports java.base/jdk.internal.misc=your_module https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
34. Troubleshooting • : – -Xdiag:resolver expose detail of constructing the initial module graph. – -Dsun.reflect.debugModuleAccessChecks thread dump to help investigate accessibility issues in reflection – -Xlog:modules=[debug trace] verbose logging for modules Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 30
35. 1 Service Provider • – – – API • – – scheme URI MyCloud://myworkspace/filestore00n/document Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 31
36. 2 ServiceLoader • provider() • module mycloud.fs { // Other module dependencies …. // provides java.nio.file.spi.FileSystemProvider with com.company.fs.MyCloudFileSystemProvider } Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 32
37. Module • API java.lang.module.* Java.lang.Module/ModuleLayer … • Path dir = Paths.get(“/path/to/modules”); ModuleFinder.of(dir); ModuleReference m = finder.find(“my.module").get(); ModuldeDescriptor desc = m.descriptor(); Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 33
38. Classloader • Bootstrap class loader • Platform class loader – was extension classloader – Drop extension mechanism – Modules de-privileged for security improvement • Side effect: NOT all Java SE types are visible to boot loader • Application class loader • URLClassLoader Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 34
39. ModuleLayers, classloaders and modules Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 35
40. Java (1) $ jlink --module-path jmods/ \ --add-modules java.sql.rowset,java.activation \ --output myimage $ myimage/bin/java --list-modules [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] $ myimage/bin/java –m company.application Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
41. Java (2) 40Mbytes • company.application • – – – Alphine + docker Java java.sql.rowset java.activation java.sql java.transfer java.naming java.xml java.logging java.base Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 37
42. Java JRE JDK JPMS JVM Jlink JDK Copyright © 2017, Oracle and/or its affiliates. All rights reserved. JDK
43. • • ” IDE NetBeans, Idea, Eclipse… Apache ” – Ant, Maven – Log4j – Lucene, Solr • Rxjava, junit, Hibernate • https://wiki.openjdk.java.net/display/quality/Quality+Outreach Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 39
44. Q&A Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 40