34
Project Jigsaw – Modularity at language level Kamil Korzekwa @kamkorz | blog.kamkor.me April 14, 2016

Project Jigsaw - modularity at language level

Embed Size (px)

Citation preview

ProjectJigsaw– Modularityatlanguagelevel

Kamil Korzekwa@kamkorz | blog.kamkor.me April14,2016

ProjectJigsaw

• AddsmodularitytotheJavaplatform• Hopefully comingtoJDK9inMarch2017• WillcausecompatibilityissuesforapplicationsthatuseJDKinternalAPIs(JEP260)

HowisitdifferentfromtoolslikeMaven?

Jigsawislanguagelevelmechanism!

What’sajigsawmodule?

• A groupingofcode• ForexampleJavapackages

• Cancontainotherdatasuchas• resources• configurationfiles• nativecode(forexamplewhenusingJNI)

• Definedinthemodule-info.java file• Fileisplacedintherootofthemodulefolder

• Canbepackagedasjar

module-info.java file

• Modulehasasymbolicname(thatonlylookslikeapackage)• Moduleexportspackages(publicAPI)toothermodules• Modulerequiresdependenciestoothermodules

module me.kamkor.foo {

exports me.kamkor.foo.api;

exports me.kamkor.foo;

requires java.sql;

}

Current naming convention

Sameasmodulename,confusing

enough?

public!=accessible(fundamentalchangetoJava)

• public

JDK9• publictoeveryonemodule me.kamkor.foo {

exports me.kamkor.foo.api;}• public,butonlytospecificmodulesmodule me.kamkor.foo {

exports me.kamkor.foo.api tome.kamkor.bar,me.kamkor.magic;

}• publiconlywithinamodule

PRE-JDK9

Impliedreadabilitymodule me.kamkor.foo {

requires java.sql;}

• Codeintheme.kamkor.foo module:

final Driver driver = DriverManager.getDriver(url);final Logger logger = driver.getParentLogger();logger.info(“Connection acquired”); Logger is exported in

the java.logging module

Driver is exported in the java.sql module

Impliedreadability// assumed configuration (fake)

module java.sql { requires java.logging;

..}

Impliedreadabilitymodule java.sql {

requires public java.logging;

}

Impliedreadability

• Ifyouusetypesfromrequiredmoduleinyourexportedtypes,thenconsiderexposingthismoduletoyourusersusingrequirespublic

• Ifyouusetypesfromrequiredmoduleonlyinyourinternalcode,thendonotexposethismoduletoyourusers

“Fun”withclasspath

bin/greeter/GreetingProvider.class

bin/starwarsgreeter/GreetingProvider.class

bin/app/GreeterApp.classUsing default packagefor the sake of simplicity

“Fun”withclasspath

$ java -classpath bin/greeter:bin/app GreeterApp

Hello World

$ java -classpath bin/starwarsgreeter:bin/app GreeterApp

May the force be with you!

$ java -classpath bin/greeter:bin/starwarsgreeter:bin/app GreeterApp

Hello World

Classpath hell– shadowing

• Defaultclassloader loadsthefirstmatchingclassitfinds

"Theorderinwhichyouspecifymultipleclasspathentriesisimportant.TheJavainterpreterwilllookforclassesinthedirectoriesintheordertheyappearintheclasspathvariable.”https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

Classpath hell– realworldexamplesofshadowing• Differentlibrariesontheclasspath containclasswiththe

samefullyqualifiedname• Twodifferentversionsofthesamelibraryareonthe

classpath• Library isrenamedandaccidentallyaddedtotheclasspath

twice

Modulepath totherescue!

Classpath vsmodulepath

• Flatstructure• Allowstolocateindividualtypes• Defaultclassloader loadsthefirstmatchingclassitfindsontheclasspath

Modulepath• Graphstructure• Allowstolocatemodulesratherthanindividualtypes• Javaisawareaboutrelationshipsbetweenthemodules• Candiscoverpotentialproblemssooner

Classpath

ShadowingfixedwithJigsawmodulepath

• CompilingJavaapplicationwithtwomodulesonthemodulepath thatexportthesamepackage

src/me.kamkor.foo/module-info.java:1: error:

module me.kamkor.foo reads package me.kamkor.beta from both me.kamkor.bar1 and me.kamkor.bar2

Compile error

ShadowingfixedwithJigsawmodulepath

• RunningJavaapplicationwithtwomodulesonthemodulepath thathavethesamename

Error occurred during initialization of VM java.lang.module.ResolutionException: Two versions of module me.kamkor.bar found in mods Error when trying to

run application

Moduleversionselection

• Versionselectionisleftforthedependency-resolutionmechanismssuchasMaven,Gradle,sbtetc.

http://openjdk.java.net/projects/jigsaw/goals-reqs/03#version-selection

Multiplemoduleversions• Jigsawallowsonlyoneversionofmoduleinsingleconfiguration• Dependencyresolutionmechanismmustpickone

• Modulepath versionhelljustlikeclasspath versionhell

http://openjdk.java.net/projects/jigsaw/goals-reqs/03#version-selection

Circulardependenciesmodule me.kamkor.foo {

requires me.kamkor.bar;}

module me.kamkor.bar {requires me.kamkor.foo;

}

./src/me.kamkor.foo/module-info.java:2: error: cyclic dependence involving me.kamkor.bar requires me.kamkor.bar;

Compile error

BetterJDKwiththeuseofProjectJigsaw

JDKisBIG!

• 4000+classesinrt.jar(classes.jar onMacs)• java.util.List etc.

• Allloadedonstartbythebootstrapclassloader• Doesyourapplicationreallyneedallofthem?

(source:Liguori,R.,Liguori, P.:Java8PocketGuide)

JDKwillbesplitintomodules

http://openjdk.java.net/jeps/200

JDKStructure(simplified)

binjava

javac

jrebin java

lib rt.jar

lib tools.jar

JDK9

binjava

javacconf

jmods

lib

Pre-JDK9

jre directoryrt.jartools.jar

jlink – TheJavaLinker

“Createatoolthatcanassembleandoptimizeasetofmodulesandtheirdependenciesintoacustomrun-timeimageasdefinedinJEP220.”http://openjdk.java.net/jeps/282

jlink – TheJavaLinker

$ jlink --modulepath $JAVA_HOME/jmods:mlib \--addmods me.kamkor.greeter --output executable \--strip-debug --compress=2

$ tree –L 1 executable

executable

bin

conf

lib

jlink – TheJavaLinker

$ du -h -d 0 executable20M executable

$ executable/bin/me.kamkor.greeterGreetings World!

jlink – TheJavaLinker

$ executable/bin/java -listmodsjava.base@9-ea

[email protected]

Version is just for information purposes

Compatibility&Migration

• Applicationscanstilluseclasspath• JDKwillbesplitintomodules.ApplicationsmaybeaffectediftheyusedinternalAPIsoftheJDK(JEP260)• ApplicationscanbesplitintoJigsawmodulesinsmallsteps.Migrationguide:• http://openjdk.java.net/projects/jigsaw/spec/sotms/#compatibility--migration

ProjectJigsawsummary• ImprovedmaintainabilityofJDKanduserapplications

• Strongerencapsulation(modulesexportpublicAPIs)• Reliableconfigurationwithmodulepath thatreplaceserrorproneclasspathmechanism

• Improvedsecurityofapplications• LessAPIsmaketheattacksurfacesmaller

• ImprovedJavaplatformscalabilityandflexibility• ConfigurablemodularJDK• Run-timeimages(jlink – theJavaLinkertool)

• Improvedperformanceofapplications• Startupperformanceimprovements(lessclassestoloadbythebootstrapclassloader)

ImpactofProjectJigsaw• IhopethatitwillspeedupthedevelopmentoftheJDK• Willitbewidelyadoptedbythedevelopers?• Spring5willsupportJigsawoutofthebox• SpringwilldoeverythingitcantoallowyoutouseJigsaw• Springjars(spring-mvc,spring-jdbc etc.)willbeconfiguredasJigsawmodules• http://www.infoq.com/presentations/spring-framework-5

Usefulresources• https://github.com/AdoptOpenJDK/jdk9-jigsaw• http://openjdk.java.net/projects/jigsaw/spec/sotms/• http://openjdk.java.net/projects/jigsaw/

Thankyou,questions?