Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A reasonable overview of Java 9 [Devoxx PL`2017]

A reasonable overview of Java 9 [Devoxx PL`2017]

The upcoming Java 9 release brings with it modularity, a new REPL, a new default garbage collector, HTTP 2 client, and much more. However, project Jigsaw requires so much of developer's attention that it's hard to see the true value of the upcoming release.

In this session we'll look at the features of Java 9 that developers not architects will face the most: welcome additions to the API: Optional, Date and Time, Stack walker, process management, collection literals (just kidding, they are factories of course), performance improvements, where to find the Unsafe and how to treat it after all. Is JShell a real tool or a toy to run fun one-liners?

Should you rush to migrate to Java 9, as you did with the Java 8 and those sweet lambdas? Are multi-release jars nice or a point of pain?

In this session we'll try to answer whether it is all worth it and how could you proceed with it.

Oleg Šelajev

June 22, 2017
Tweet

More Decks by Oleg Šelajev

Other Decks in Programming

Transcript

  1. JAVA 9
    A REASONABLE OVERVIEW
    Oleg Šelajev
    ZeroTurnaround
    @shelajev

    View Slide

  2. @shelajev

    View Slide

  3. View Slide

  4. WHAT’S JAVA 9 CODE NAME?

    View Slide

  5. View Slide

  6. Safe harbour and
    stuff
    wARNING!!
    Sarcasm ahead!

    View Slide

  7. September 21st

    View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. IDE SUPPORT

    View Slide

  15. http://0t.ee/rebellabs-survey-2017

    View Slide

  16. http://openjdk.java.net/projects/jigsaw/
    JEP 200: The Modular JDK
    JEP 220: Modular Run-Time Images
    JEP 260: Encapsulate Most Internal APIs
    JEP 261: Module System
    JEP 282: jlink: The Java Linker
    JSR 376: JAVA PLATFORM MODULE SYSTEM

    View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. AREN’T MODULES EXCITING?

    View Slide

  21. module org.shelajev.N {
    requires com.google.guava;
    requires transitive com.shelajev.api;
    requires static compile.time.dependency;
    requires transitive static and.its.dependencies;
    exports public.classes.in.these.packages;
    exports some.packages to specific.modules;
    opens packages.for.reflection.and.public;
    opens these.packages to specific.modules, and.others;
    uses some.service.api;
    provides some.api with some.implementation;
    }

    View Slide

  22. MECHANISM COMPILE ACCESS REFLECTION ACCESS
    export all code > public all code > public
    qualified export specified modules > public specified modules > public
    open package none all code > private
    qualified open package none specified modules > private
    open module none all code > private
    https://blog.codefx.org/java/reflection-vs-encapsulation/

    View Slide

  23. --add-exports /=
    --add-exports java.management/sun.management=ALL-UNNAMED
    --add-modules=java.xml.bind
    COMMAND LINE FUN

    View Slide

  24. View Slide

  25. View Slide

  26. WHAT’S DOES
    method.isAccessible()
    RETURN?

    View Slide

  27. View Slide

  28. AUTOMATIC MODULES
    Any JAR on the module path without a
    module descriptor is an automatic module.
    Automatic modules implicitly export all their packages
    and read all other modules.

    View Slide

  29. --module-path --classpath
    Modular JAR application module unnamed module
    Non-Modular JAR automatic module unnamed module

    View Slide

  30. --ILLEGAL-ACCESS=PERMIT
    1 WARNING, OTHERWISE LIKE CLASSPATH

    View Slide

  31. MIGRATION STRATEGY

    View Slide

  32. LET’S TALK ABOUT MAVEN

    View Slide

  33. View Slide

  34. PETCLINIC APP
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.6:jar (default-jar) on project spring-
    petclinic: Execution default-jar of goal org.apache.maven.plugins:maven-jar-plugin:2.6:jar failed: An API
    incompatibility was encountered while executing org.apache.maven.plugins:maven-jar-plugin:2.6:jar:
    java.lang.ExceptionInInitializerError: null

    View Slide

  35. Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class
    java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws
    java.lang.ClassFormatError accessible:
    module java.base does not "opens java.lang" to unnamed module @4b9e00f3
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337) ~[na:na]
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281) ~[na:na]
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:198) ~[na:na]
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:192) ~[na:na]
    at org.springframework.cglib.core.ReflectUtils$1.run(ReflectUtils.java:54) ~[na:na]
    at java.base/java.security.AccessController.doPrivileged(Native Method) ~[na:na]
    at org.springframework.cglib.core.ReflectUtils.(ReflectUtils.java:44) ~[na:na]
    ... 38 common frames omitted
    PETCLINIC APP
    built on Java 8, running Java 9

    View Slide

  36. PETCLINIC APP
    built on Java 8, running Java 9
    with --permit-illegal-access
    Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:553) ~[na:na]
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94)
    ~[spring-petclinic-1.5.1.jar:1.5.1]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:486) ~[na:na]
    ... 34 common frames omitted

    View Slide

  37. PETCLINIC APP
    built on Java 8, running Java 9
    with --permit-illegal-access
    and --add-modules java.se.ee

    View Slide

  38. View Slide

  39. Multirelease JARs

    View Slide

  40. G1GC
    GC

    View Slide

  41. Generational GC

    View Slide

  42. G1GC

    View Slide

  43. @shipilev
    https://shipilev.net/jvm-anatomy-park/3-gc-design-and-pauses/

    View Slide

  44. https://2017.geekout.ee/videos/#day2-02

    View Slide

  45. mad skillz, amirite?

    View Slide

  46. JSON
    picture of a hipster

    View Slide

  47. import jdk.incubator.http.HttpClient;
    import jdk.incubator.http.HttpResponse;
    HttpClient.newHttpClient().send(
    newBuilder(URI.create("https://www.google.com"))
    .GET()
    .build(),
    HttpResponse.BodyHandler.asString())
    .body();
    http2

    View Slide

  48. STRING PERFORMANCE
    JEP 254: Compact Strings
    Adopt a more space-efficient internal representation for strings.
    JEP 280: Indify String Concatenation
    Change the static String-concatenation bytecode sequence
    generated by javac to use invokedynamic calls to JDK library
    functions.

    View Slide

  49. View Slide

  50. THINGS NOT TO TOUCH
    UNLESS YOU’RE 100% SURE
    multi-release jars
    jlink

    View Slide

  51. THINGS NOT TO TOUCH
    UNLESS YOU’RE 100% SURE
    multi-release jars
    jlink
    Unsafe

    View Slide

  52. Java 9, the cooking guide
    1. Upgrade your IDE, your other tools
    2. Upgrade Spring to 5.0
    3. Add --illegal-access=warn
    a. fix easily fixable warnings
    4. Wait 365 days (or more) on the classpath

    View Slide

  53. View Slide

  54. View Slide

  55. [email protected]
    @shelajev
    Find me and chat with me!

    View Slide