Project Jigsaw in JDK 9: Modularity Comes to Java

33
© Copyright Azul Systems 2016 © Copyright Azul Systems 2015 @speakjava azul.co m Project Jigsaw in JDK 9: Modularity Comes To Java Simon Ritter Deputy CTO, Azul Systems 1

Transcript of Project Jigsaw in JDK 9: Modularity Comes to Java

Page 1: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

© Copyright Azul Systems 2015

@speakjava azul.com

Project Jigsaw in JDK 9:Modularity Comes To Java

Simon RitterDeputy CTO, Azul Systems

1

Page 2: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

API Structure Changes

Page 3: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

API Classification Supported, intended for public use

– JCP specified: java.*, javax.*– JDK specific: some com.sun.*, some jdk.*

Unsupported, not intended for public use– Mostly sun.*– Most infamous is sun.misc.Unsafe

3

Page 4: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JDK 9: Incompatible Changes Encapsulate most JDK internal APIs Remove a small number of supported APIs

– 6 in total, all add/remove PropertyChangeListener– Already flagged in JSR 337 (Java SE 8), JEP 162

Change the binary structure of the JRE and JDK New version string format A single underscore will no longer be allowed as an

identifier in source code

4

Page 5: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JDK Internal API Classification Non-critical

– Little or no use outside the JDK– Used only for convenience (alternatives exist)

Critical– Functionality that would be difficult, if not impossible to

implement outside the JDK

5

Page 6: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JEP 260 Proposal Encapsulate all non-critical JDK-internal APIs Encapsulate all critical JDK-internal APIs, for which

supported replacements exist in JDK 8 Do not encapsulate other critical JDK-internal APIs

– Deprecate these in JDK 9– Plan to encapsulate or remove them in JDK 10– Provide command-line option to access encapsulated

critical APIs

6

Page 7: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Reviewing Your Own Code jdeps tool

– Introduced in JDK 8, improved in JDK 9– Maven jdeps plugin

jdeps –jdkinternals path/myapp.jar

7

path/myapp.jar -> /opt/jdk1.8.0/jre/lib/rt.jar <unnamed> (myapp.jar) -> java.awt -> java.awt.event -> java.beans -> java.io ...

Page 8: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Introduction To Jigsaw And Modules

Page 9: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Module Fundamentals Module is a grouping of code

– For Java this is a collection of packages The module can contain other things

– Native code– Resources– Configuration data

9

com.azul.zoop

com.azul.zoop.alpha.Namecom.azul.zoop.alpha.Positioncom.azul.zoop.beta.Animalcom.azul.zoop.beta.Zoo

Page 10: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Module Declaration

10

module com.azul.zoop {}

module-info.javacom/azul/zoop/alpha/Name.javacom/azul/zoop/alpha/Position.javacom/azul/zoop/beta/Animal.javacom/azul/zoop/beta/Zoo.java

Page 11: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Module Dependencies

module com.azul.zoop { requires com.azul.zeta;}

com.azul.zoop

com.azul.zeta

Page 12: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Module Dependencies

module com.azul.app { requires com.azul.zoop requires java.sql}

com.azul.app

com.azul.zoop java.sql

Page 13: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Module Dependency Graphcom.azul.app

java.base

java.sqlcom.azul.zoop

com.azul.zetajava.xml java.logging

explicitimplicit

Page 14: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Readability v. Dependency

com.azul.app

java.sql

java.logging

module java.sql { requires public java.logging;}

Driver d = …Logger l = d.getParentLogger();l.log(“azul’);

Implied readability

Page 15: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Module Implied Readability Graphcom.azul.app

java.base

java.sqlcom.azul.zoop

com.azul.zetajava.xml java.logging

explicitimplicitimplied

Page 16: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Package Visibility

module com.azul.zoop { requires com.azul.zeta; exports com.azul.zoop.alpha; exports com.azul.zoop.beta;} com.azul.zoop

com.azul.zoop.alphacom.azul.zoop.beta com.azul.zoop.theta

Page 17: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Accessibility For a package to be visible

– The package must be exported by the containing module– The containing module must be read by the using module

Public types from those packages can then be used

com.azul.zoopcom.azul.appreads

Page 18: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JDK 8 Dependencies

Page 19: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JDK 9 Platform Modules

19

java.se

java.compact3

java.compact2

java.compact1

java.scripting

java.instrument

java.base

java.logging

java.sql

java.sql.rowset

java.xml

java.desktop

java.rmi

java.prefs java.datatransfer

java.compiler

java.managementjava.security.jgss

java.naming

java.security.sasl

All modules depend on java.base

Page 20: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JDK 9 Platform Modules

20

java.se.ee java.se

java.xml.bind

java.corba

java.compiler

java.desktop

java.annotations.common

java.rmi

java.datatransfer

java.management

java.xml.ws

java.naming

java.transaction

java.activation

All modules depend on java.base

Page 21: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Developing Code With Modules

Page 22: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Packaging With Modular JAR Filesmods/zoop/module-info.classmods/zoop/com/azul/app/Main.class

$ jar --create --file mylib/app.jar \ --main-class com.azul.app.Main \ -C mods .

module-info.classcom/azul/app/Main.class

app.jar

Page 23: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

JAR Files & Module Information

$ jar --file mylib/app.jar –pName: com.azul.zoopRequires: com.azul.zeta java.base [MANDATED] java.sqlMain class: com.azul.zoop.Main

Page 24: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

LinkingModular run-time

image

…confbin

jlink

$ jlink --modulepath $JDKMODS \ --addmods java.base –output myimage

$ myimage/bin/java –[email protected]

Page 25: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Linking An Application

$ jlink --modulepath $JDKMODS:$MYMODS \ --addmods com.azul.app –output myimage

$ myimage/bin/java –[email protected]@[email protected]@[email protected]@[email protected]

Version numbering forinformation purposes only“It is not a goal of the module system to solve the version-

selection problem”

Page 26: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Application Migration

Page 27: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Typical Application (JDK 8)

jarjar

jar

JDK

jar

jarjarjar jar

jarjar

jar

jar

Cla

sspa

th

Page 28: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Typical Application (JDK 9)

jarjar

jar

modulejava.base

modulejava.desktop

modulejava.datatransfer

modulejava.xml

jar

jarjarjar jar

jarjar

jar

jar

Unnamed module

Page 29: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Automatic Modules Real modules Simply place unmodified jar file on module path

– Rather than classpath No changes to JAR file Module name derived from JAR file name Exports all its packages

– No selectivity Automatically requires all modules on the module path

29

Page 30: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Summary & Further Information

Page 31: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Summary Modularisation is a big change for Java

– JVM/JRE rather than language/APIs Potentially disruptive changes to exposure of non-public APIs

– Is it safe? Developing modular code will require some learning

– Not a huge change, though

31

Page 32: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

Further Information openjdk.java.net openjdk.java.net/jeps openjdk.java.net/projects/jigsaw jcp.org

www.zulu.org

32

Page 33: Project Jigsaw in JDK 9: Modularity Comes to Java

© Copyright Azul Systems 2016

© Copyright Azul Systems 2015

@speakjava azul.com

Questions

Simon RitterDeputy CTO, Azul Systems

33