Advanced Modular Development -...
Transcript of Advanced Modular Development -...
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Advanced Modular DevelopmentCON6821
Mark Reinhold, Alex Buckley, Alan BatemanJava Platform Group, OracleOctober 2015
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Sessions
Prepare for JDK 9
Introduction to Modular Development
Advanced Modular Development
Project Jigsaw: Under the Hood
Project Jigsaw Hack Session
3
2
2
1
4
5
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Application Migration
3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Typical application
4
JAR JAR
JAR
JARJAR
JAR JAR
JAR
JARJAR JAR
JAR
JDK
JAR JAR
JAR
JARJAR
JAR
JAR
JARJAR
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Typical application
5
JAR JAR
JAR
JAR
JAR
JARJAR JAR
JARJAR JAR
JAR
JARJAR
JAR
JARJAR
modulejava.base
modulejava.logging
modulejava.sql
modulejava.xml
JAR
JAR
JAR
JAR
JAR
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 6
jackson-databind-2.6.3.jar
myapp.jar
modulejava.base
modulejava.logging
mylib.jar
modulejava.xml
modulejava.sql
jackson-annotations-2.6.0.jarjackson-core-2.6.3.jar
SampleScenario
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 7
$ java -cp \ lib/myapp.jar:\ lib/mylib.jar:\ lib/jackson-core-2.6.3.jar:\ lib/jackson-databind-2.6.3.jar:\ lib/jackson-annotations-2.6.0.jar\ myapp.Main
Running my application
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 8
jackson-databind-2.6.3.jar
modulemyapp
modulemylib
jackson-annotations-2.6.0.jarjackson-core-2.6.3.jar
Migrating from the top down
modulejava.base
modulejava.logging
modulejava.xml
modulejava.sql
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Migrating from the top down
• module myapp requires?• module myapp exports?
• module mylib requires?• module mylib exports?
9
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 10
$ jdeps -s lib/myapp.jar lib/mylib.jar myapp.jar -> lib/jackson-core-2.6.3.jar myapp.jar -> lib/jackson-databind-2.6.3.jar myapp.jar -> mylib.jar myapp.jar -> java.base myapp.jar -> java.sql mylib.jar -> java.base
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 11
// src/mylib/module-info.java module mylib { requires java.base; exports com.myapp.lib.util to myapp; }
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 12
// src/myapp/module-info.java module myapp { requires mylib; requires java.base; requires java.sql; ??? requires jackson.core ??? ??? requires jackson.databind ??? }
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 13
If only …
jackson-databind-2.6.3.jar
jackson-core-2.6.3.jar
jackson-annotations-2.6.0.jar
modulejackson.core
modulejackson.databind
modulejackson.annotations
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 14
// src/myapp/module-info.java module myapp { requires mylib; requires java.base; requires java.sql; requires jackson.core; requires jackson.databind; }
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 15
modulejackson.databind
modulemyapp
modulejava.base
modulejava.logging
modulemylib
modulejava.xml
modulejava.sql
modulejackson.annotations
modulejackson.core
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Automatic modules
• “Real” modules• No changes to someone else’s JAR file :-)• Module name derived from JAR file name• Exports all its packages• Requires all other modules
16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 17
modulemyapp
modulemylib
modulejackson.databind
modulejackson.annotations
modulejackson.core
modulejava.xml
modulejava.sql
modulejava.logging
modulejava.base
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 18
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 19
lib/jackson-core-2.6.3.jar lib/jackson-databind-2.6.3.jar lib/jackson-annotations-2.6.0.jar
src/myapp/module-info.java src/myapp/...
src/mylib/module-info.java src/mylib/...
$ javac -modulesourcepath src -mp lib -d mods ...
$ jar --create --file mlib/mylib.jar -C mods/mylib .
$ jar --create --file mlib/myapp.jar -C mods/myapp . \ —-main-class myapp.Main
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 20
$ java -mp mlib:lib —m myapp
$ java -cp \ lib/myapp.jar:\ lib/mylib.jar:\ lib/jackson-core-2.6.3.jar:\ lib/jackson-databind-2.6.3.jar:\ lib/jackson-annotations-2.6.0.jar \ myapp.Main
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Library Migration
21
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 22
jackson-databind-2.6.3.jar
myapp.jar
modulejava.base
modulejava.logging
mylib.jar
modulejava.xml
modulejava.sql
jackson-annotations-2.6.0.jarjackson-core-2.6.3.jar
SampleScenario
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 23
modulejackson.databind
myapp.jar
modulejava.base
modulejava.logging
mylib.jar
modulejava.xml
modulejava.sql
modulejackson.annotations
modulejackson.core
Migrating from the bottom up
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Migrating from the bottom
• module jackson.core requires?• module jackson.core exports?• module jackson.databind requires?• module jackson.databind exports?• module jackson.annotations requires?• module jackson.annotations exports?
24
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Requires?
25
$ jdeps -s lib/jackson*.jar jackson-annotations-2.6.0.jar -> java.base jackson-core-2.6.3.jar -> java.base jackson-databind-2.6.3.jar -> lib/jackson-annotations-2.6.0.jar jackson-databind-2.6.3.jar -> lib/jackson-core-2.6.3.jar jackson-databind-2.6.3.jar -> java.base jackson-databind-2.6.3.jar -> java.sql jackson-databind-2.6.3.jar -> java.xml
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 26
jackson.databind
jackson.core java.sql
jackson.annotations java.xml
java.base
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Creating the module-info.java for each module
27
$ jdeps -genmoduleinfo src *.jar writing to src/jackson.annotations/module-info.java writing to src/jackson.databind/module-info.java writing to src/jackson.core/module-info.java
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 28
// src/jackson.databind/module-info.java module jackson.databind { requires public java.sql; requires public java.xml; requires public jackson.annotations; requires public jackson.core; exports com.fasterxml.jackson.databind; exports com.fasterxml.jackson.databind.annotation; exports com.fasterxml.jackson.databind.cfg; exports com.fasterxml.jackson.databind.deser; exports com.fasterxml.jackson.databind.deser.impl; exports com.fasterxml.jackson.databind.jsontype; exports com.fasterxml.jackson.databind.jsontype.impl; exports com.fasterxml.jackson.databind.module; exports com.fasterxml.jackson.databind.node; exports com.fasterxml.jackson.databind.ser; exports com.fasterxml.jackson.databind.ser.impl; exports com.fasterxml.jackson.databind.ser.std; exports com.fasterxml.jackson.databind.type; exports com.fasterxml.jackson.databind.util; }
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 29
// src/jackson.databind/module-info.java module jackson.databind { requires public java.sql; requires public java.xml; requires public jackson.annotations; requires public jackson.core; exports com.fasterxml.jackson.databind; exports com.fasterxml.jackson.databind.annotation; exports com.fasterxml.jackson.databind.cfg; exports com.fasterxml.jackson.databind.deser; exports com.fasterxml.jackson.databind.deser.impl; exports com.fasterxml.jackson.databind.jsontype; exports com.fasterxml.jackson.databind.jsontype.impl; exports com.fasterxml.jackson.databind.module; exports com.fasterxml.jackson.databind.node; exports com.fasterxml.jackson.databind.ser; exports com.fasterxml.jackson.databind.ser.impl; exports com.fasterxml.jackson.databind.ser.std; exports com.fasterxml.jackson.databind.type; exports com.fasterxml.jackson.databind.util; }
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 30
src/jackson.core/module-info.java src/jackson.core/...
src/jackson.databind/module-info.java src/jackson.databind/...
src/jackson.annotations/module-info.java src/jackson.annotations/...
$ javac -modulesourcepath src -d mods ...
$ jar --create --file mlib/jackson.core-2.6.3.jar \ —-module-version 2.6.3 -C mods/jackson.core .
$ jar --create --file mlib/jackson.databind-2.6.3.jar \ —-module-version 2.6.3 -C mods/jackson.databind .
$ jar --create --file mlib/jackson.annotations-2.6.0.jar \ —-module-version 2.6.0 -C mods/jackson.annotations .
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 31
modulejackson.databind
modulejava.logging
modulejava.sql
modulejackson.core
modulejackson.annotations
myapp.jar mylib.jar
modulejava.base
modulejava.xml
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 32
$ java -mp mlib -cp lib/myapp.jar:lib/mylib.jar -addmods jackson.databind \ myapp.Main
$ java -cp \ lib/myapp.jar:\ lib/mylib.jar:\ lib/jackson-core-2.6.3.jar:\ lib/jackson-databind-2.6.3.jar:\ lib/jackson-annotations-2.6.0.jar \ myapp.Main
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 33
$ java -mp mlib -cp lib/myapp.jar:lib/mylib.jar \ -addmods jackson.databind myapp.Main
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make member of class app.MyValue accessible: module jackson.databind does not read <unnamed module @6536e911> at sun.reflect.Reflection.throwInaccessibleObjectException([email protected]/Reflection.java:464) at java.lang.reflect.AccessibleObject.checkCanSetAccessible([email protected]/AccessibleObject.java:175) at java.lang.reflect.Constructor.checkCanSetAccessible([email protected]/Constructor.java:174) at java.lang.reflect.Constructor.setAccessible([email protected]/Constructor.java:167) at com.fasterxml.jackson.databind.util.ClassUtil.checkAndFixAccess(jackson.databind/ClassUtil.java:505) at com.fasterxml.jackson.databind.deser.impl.CreatorCollector._fixAccess(jackson.databind/ CreatorCollector.java:280) at com.fasterxml.jackson.databind.deser.impl.CreatorCollector.setDefaultCreator(jackson.databind/ CreatorCollector.java:155) :
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 34
jackson.databind
jackson.core
java.sqljackson.annotations
java.xml
<unnamed>
java.base
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 35
package java.lang.reflect;
public final class Module {
public String getName();
public boolean canRead(Module source); public Module addReads(Module source);
… }
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 36
Module source = clazz.getModule();this.getClass().getModule().addReads(source);
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 37
jackson.databind
jackson.core
jackson.annotations
<unnamed>
java.sql java.xmljava.base
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Putting it all together
38
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 39
modulejackson.databind
modulejava.logging
modulejava.sql
modulejackson.core
modulejackson.annotations
modulejava.base
modulejava.xml
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 40
modulejackson.databind
modulejava.logging
modulejava.sql
modulejackson.core
modulejackson.annotations
modulejava.base
modulejava.xml
modulemyapp
modulemylib
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 41
$ java -mp mlib -m myapp
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Linking
42
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 43
$ jlink —mp $JDKMODS:mlib —addmods myapp —output myimage
$ ls myimage bin conf lib release
$ ls myimage/bin myapp java keytool
$ myimage/bin/java -listmods myapp mylib [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 44
$ myimage/bin/java -m myapp Greetings from myapp, here’s some json!
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 45
$ myimage/bin/myapp Greetings from myapp, here’s some json!
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Summary
• Freedom to adopt modules at your own pace• Modularize your application before its libraries• Modularize libraries independently
• Different kinds of modules• Explicit vs. automatic
• Some libraries will need changes to work as modules
46
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 47
Go forth and modularize!
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Other sessions, this room
• Project Jigsaw: Under the Hood @ 5.30pm• Project Jigsaw Hack Session @ Tuesday 8.30am
48
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 49
More Information
OpenJDK Project Jigsaw
http://openjdk.java.net/projects/jigsaw/
mailto:[email protected]
Early Access Builds
https://jdk9.java.net/jigsaw/
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor StatementThe preceding 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.
50