JavaOne – How to Make a Project Java 9 Compatible

“How to Make a Project Java 9 Compatible”

Speaker: Nikhil Nanivadekar

For more blog posts from JavaOne, see the table of contents


Examples use Eclipse Collections

Examples from:
https://github.com/nikhilnanivadekar/Java9Compatibility
https://github.com/nikhilnanivadekar/Java9Modules

Reflection

  • Used ArrayListAdapter which uses reflection
  • Got illegal-access warning (when default was warn; now it is permit
  • If run with illegal-access=deny, it fails
  • Lesson: have lots of regression tests so know about such errors
  • Lesson: Allow a lot of time to migrate. Don’t start Friday evening. Long sequence of “just one more compilation error to fix”

Infering types

  • Compler error because can’t assume generictype to return
  • Comes up when class doesn’t have a generic type and try to store it in one that does.
  • Compiler not supposed to infer type
  • Fix is to cast or store in a local variable
  • Similarly need to declare types on class instatiation if not diamond operator usage

NullPointerException

  • collect() now calls Objects.requireNotNull(combiner).
  • In Java 8, a null combiner worked in serial mode because not used.

“I love compiler errors vs runtime exceptions”

Migrating a Java 8 Maven project to Java 9

  • Migrated a maze solving project. Was written recently so not legacy code
  • Switch source/target of maven-compiler-plugin to “9”.
  • Changed JDK in IntelliJ.
  • Maven itself works fine with Java 9. Compiling works. JavaDoc and Enforcer plugins work fine with milestone version; not yet for released versions. Seewiki on support for Java 9 in Maven plugins
  • Looking at modularizing library in future
  • Runs without adding module info file or doing anything else
  • Once add module file, more enforcement happens. ex: error on unnamed packages
  • Eclipse and IntelliJ have quick tip to add requires
  • Important: start with base module
  • If not using a build tool, need to ensure no cyclic dependencies. (build tool takes care of this for you)
  • Add exports clause for module. The compiler error just says the class isn’t found. It doesn’t say it is because the export is missing.
  • Move code in split package to another package updating all callers
  • Delete empty package; can’t have empty package
  • Hide internal packages by not exporting sub-packages

My take: Nice to see issues by example. And nice to see that it wasn’t “all about Jigsaw.” It was a good mix of problem types!

JavaOne – 55 new features in Java 9

“55 new features in Java 9”

Speaker: Simon Ritter

For more blog posts from JavaOne, see the table of contents


  • Modularity/Jigsaw – JDK is 94 moudles. Java SE is 36 of them. Probably using sun.misc.Unsafe in open source library
  • Big kill switch now has options to permit/warn/debug (warn with stack trace)/deny(will be default at some point) illegal access
  • jlink – jaa linker so can build runtime specifically for your application. Creates directory structure for you that matches jdk but only wit the modules you need. java -list-modules shows what modules are in there. Version numbering information is for informational purposes only. Can’t use different versions for different parts of the code.
  • Static factory methods for List/Set/Map interfaces. Old way was to instantiate, add a bunch of elements and then call Collections.unmodifiableSet(). Now can just call Set.of(…) with 0-10 elements or varargs if have more and get an umodifiable collection
  • Stream enhancements – dropWHile/takeWhile (like skip/limit but with predicate rather than number), improved iterate to be more like a for loop, parallel Files.lines() as memory mapped and a stream from Optional with zero/one elements
  • multi-release jar files – use JDK 8 code in main jar file and then JDK 9 in the “9” diretory
  • jshell – REPL (read eval print loop) – good for simple prototyping
  • concurrency updates – reactive streams publish/subscribe framework, async nonblocking, Flow interfaces, SubmissionPublisher utility class.
  • CompleteableFuture additions (originally added in Java 8). Now has delays/timeouts, better support fr subclassing, static utility methods for minimalCompletionStage (read JavaDoc; confusing), failedStage, failedFuture [and one more]
  • Enhanced deprecation – forRemoval and since (not back ported consistently), jdeprscan command can produce report from jar or class file
  • Project Coin – single underscore not valid identifier (so in future can use _ in lambda instead of variable), priate method in interfaces, effectively final variables in tr-with-resources, allow @SafeVarars on private instance variables, diamond operate with anonyous classes
  • New standards – Unicode 7 (2834 new chars) and 8 (7716 new chars) support – added emojis, PKCS12 key stores by default, HTML 5 JavaDoc, etc
  • UTF-8 property files
  • DRBG based SecureRadom bit generators
  • XML catalog API upgraded standard
  • G1 is default garbage collector for servers (used to be parallel collectors), low pause, deals with bigger heaps
  • Better String performance – compact strings (use less space internally; don’t use 16 bits for chars that could be 8 bits), store interned strings in CDS archive to share char[] and String objects between VMs, String concatentaton to use invokedynamic (allows for future performance improvements at runtim)
  • Marlin graphics renderer – faster
  • A couple really low level VM performance things
  • JavaFX improvement
  • Segment Code cache – separates non-method, proiled and non-profiled code (enables future performance improvments)
  • Unified JVM and GC logging – using old options prevent JVM startup
  • Spin-wait hints. Small change to improve performance on x86, but Azul [where Simon works] proposed it showing openness of JEP.
  • Variable handles – API to replace parts of sun.misc.Unsafe. Used for fence operations (memory control/atomic operations preventing JVM from changing order of operations in code), VarHandle (low level objec)
  • AOT compilation – ahead of time compilation – experimental feature; need to turn on with UnlockExperimenalVMOptions. jaotc to comiple in this mode
  • Process API can get pid, arguments, start time, CPU usage and name if security manager allows
  • New version string format for java JDK major minor security patch. Will cange again post Java 10. No more 1.X
  • JDK/JRE file structure – only one bin directory no special jre direcory. tools.jar and rt.jar also went away.
  • Sarchable JavaDocs
  • Annotation pipeline – repeating annotations
  • Parser API for Nashorn – and spports ECMAScript 5.1
  • Disable SHA-1 certs mostly
  • Applets deprecated for removal in Java 10
  • A handful of APIs removed [not ommon ones]
  • Removed a few other things like hprof agent, jhat tool, old docs
  • Removed deprecated GC options

My take: He said up front that this talk is supposed to be an hour. Implying we won’t get to all 55 features. He didn’t sound rushed though so there wasn’t the problem from yesterday’s speaker with two much information. It was a good overview (or review) of the features. I learned some things like the eixstance of jdeprscan

JavaOne – Project Jigsaw – Integration with Tools

“Project Jigsaw – Integration with Tools”

Speaker: Alexandru Jecan (author of Java 9 Modularity Revealed)

For more blog posts from JavaOne, see the table of contents


What expect build tools and IDEs to do

  • Code completion (IDEs only)
  • Make things easier
  • Work with class path and module path

Maven

  • Only Maven 3+ supports Java 9.
  • No changes to Maven core to run on Java 9
  • presence of module-info.java decides if Maven uses module path
  • Need Maven compiler plugin 3.6.1 to compile Java 9 code
  • Discourages using automatic module name
  • GAV not related to module name. Continue to use GAV.
  • Use module name in module-info.java – double work
  • Maven does not generate “requires” clauses for module-info.java
  • Can add <compilerArgs> options for -add-modules or -add-exports
  • New tag <release> which takes precedence over source/target tags. Recomended compiling twice or backward compatbility. once for java 9 modules and once for java 8. [why? isn’t that what multi release ars are for?]
  • maven-exec-plugin – can configure module path
  • taked abut updating maven toolchain file [not clear how this differs than prior versions of java]
  • maven-jdeps-plugin – can automatically generate module descriptor [but said earlier couldn’t]
  • maven-jmod-plugin – new plugin; not yet released. Creates and lists content of jmod files. Merge native code into jar
  • maven-jlink-plugin – new plugin; not yet released. Creates runtime images
  • maven-depdendency-plugin – said lists GAV [not clear how changed

Gradle
No first class support for Java 9 yet. Coming soon. [but said using; puzled?]

Ant
Ant 1.9.8+ support JDK 9 modules for java/javac/junit. [yet Ant doesn’t support JUnit 5]

Loom

  • New build tool for Java 9
  • Uses YAML config. Uses Maaven repo.
  • Supports JUnit 5,

loom.builders

Moditect
Maven plugin that generates module-info.java based on dependencies. Says helps a little but doesn’t do all the work. [how?]

SonarJava
SonarJava 4.11+ recognizes module keywords

Graphviz
Open source visualization software
Reads jdeps input to generate graphical representation of Jigsaw. As can IDES

IDEs

  • NetBeans, Eclipse and IntelliJ all understand modules clauses [if you count pre-release versions]
  • Also offer autocompletion, syntax higlighting, etc
  • NetBeans 9 – not yet released; must build yourself. Showed editing module path, visual of module dependencies, jshell, jlink
  • Eclipse Oxygen with Java 9 support – came out 9/27/17. Ten second demo
  • IntelliJ 2017.1+ – Mark directory as source root so directory name matches module name. Errors on common module errors, Showed module editing, module dependencies and other features

Open source readiness

How to modularize your app

  • Introduce modue-info.java by creating manually or running jdeps to generate
  • Problems: using JDK internal APIs (ignore warning or fix), using odule not availalbe such as xml bindings (add-modules at compile and runtime), cyclic dependencies (can have at runtime but not compile time), split packages (Oracle plans to make change in later version of Java to deal with this)
  • [Note: He recommended renaming packags as a solution to spit packages. That sounds like a horrible idea unless you can guarantee only you call that code]

    My take before session: You know how they say that first impressions matter? The speaker is wearing a suit. 90% of the people in the room are wearing jeans. Two people in the room are wearing a suit The speaker and someone he knows. Then he showed the table of contents. There are 29 points in 45 minutes in the outline. Preparing to have my head spin!

    My take after session: He is in fact technical. The suit was misleading! The pace was way too fast though; drinking from a fire hose. Not enough time to understand/process many of the points. He talks fast (as do I), but key is to *pause* if you talk fast so people can catch up. Also the side transitions were distracting. A cube transition is cute. But if you are reading when it moves, it is disorienting. And due to the speed, there was a good chance of being reading when transitions started. This was good information, but should have been two sessions so split up and a decent pace. And omiting how to migrate your libraries; that’s a talk on its own There is a Maven BOF tonight; maybe folks can discuss more then!

    One minute after the official end time, he asked if there were questions. My head was spinning with questions. [I didn’t even have time to process which was most important]. Another attendee asked if having JUnit tests in a parallel directory with the same package name is a split package. The speaker said yes and went on to say to wait for Java 10 or rename. I interjected at that point. Unless you are distributing a test jar, I don’t think this is a problem. In fact, most IDEs compile both the /src/main/java and /test/main/java directories to the same folder. I stated this and asked the speaker if he agreed. He said yes.