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 – Optional – the mother of all bikesheds

“Optional – the mother of all bikesheds”

Speaker: Stuart Marks
#OptionalBikeshed

The deck is available online

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


Showed early version of “optional” (stream returning null)

Review of basics

  • Two states – present (contains non null reference) or absent
  • Don’t say “null optional”. Say “absent” or “empty”
  • Optionals are immutable
  • equals() and hashCode() work as one would expect – useful for JUnit because can call assertEquals()

New in Java 9

  • stream() – stream with 0 or 1 elements
  • ifPresentOrElse(Consumer)
  • or(Supplier)

Good code and Best practices

  • Never set an Optional variable to null. It should be Optional.empty() as default.
  • If method returns Optional, never return a null either.
  • Intended for return types where might or might not have a value to return.
  • Don’t call get() as could be empty optional
  • Use isPresent() and get() as last resort
  • Using .map().orElse() chained after stream pipeline gives fluent API calls
  • Calling filter() returns original value only if non-empty optional that matches. else returns empty. Using filter() and orElseThrow() is good for validation
  • [and more see the deck]

My take: Amazed there is so much to say about Optional! Excellent set of best practices. And excellent point on not calling an optional “null.” I needed to leave early as my session is immediately after this one. And I really wanted to know “what’s with the bikeshed” which is covered last.  It’s a nice collection of quotes. So I found the deck and best practices online. There’s also a youtube video linked so you can watch at home.

JavaOne – Community Keynote

“Community Keynote”

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


IBM

  • This year, open sourced IBM JDK 9 (OpenJ9)and LIberty Server
  • Demo: microservices, error handling, live code update, Docker
  • Expecting coding in the cloud will become more common
  • More innovation needed: cloud dynamics, containers, hardware
  • Container Kindergarten – play will with others; don’t steal resources/toys from other containers
  • Competition around best JVM
  • Guarded storage – physical hardware that can run GC in parallel by adjusting to memory moves
  • “Server doesn’t even mean the same as it used to”
  • Java EE got reputation for being monolithic. Hasn’t been true in a long time. Now have lighweight runtimes Ex: TomEE, Liberty
  • Even with Kubernetes, need runtime so app can run in portable way
  • Open Liberty – supports Java EE 7, Microprofile, Docker. Uses Eclipse upgrade. Biggest open source contribution IBM ever made. Can get production support as paid upgrade. Designed to be run for microservices and cloud native apps.
  • Microprofie -optimized enterprise capabiities for micro services. Looking at contributing to Java EE or moving under EE4J umbrella.

Oracle

  • Cute skit about this being a rehersal for the Matrix. Callouts to Brazil and the Netherlands. Callout to Star Wars. Callout to Java’s new 6 month release schedule
  • [The lights are green which makes for an interesting reflection on my iPad keyboard]
  • Simulated [I think] tech failures
  • Used underscore variable and changed to double unerscore when didn’t compile
  • Counting: “8, 9 , 18.3, 18.9, whaaat”
  • Follow the white duke (to the JCP party
  • Scene 2: Juggie (a puppet), a bunch of JUG leaders in costumes and two artists
  • The blue J2EE bill taes you back and the red EE4J pill… YOur improvements will be faster now.
  • The agents are spreading fear saying Java EE is dead. The JUG Leaders are the first line of defense
  • Each JUG leader said something about upgrading or a new feature
  • RV carries more and has couch/bathroom. But motorcyce more agile an can go anywhere
  • Scene 3: green screen on motorcycle
  • Scene 4: Went to Japan to learn how to fight agents. Taught spirit of Jigsaw. Used the machine with the 60 Raspberry Pi devices in a circle to check punch. Creates video to see from all angles.
  • Scene 5: Virtual User Group – in 3 weeks, doing 24 hours of vjug. And a bunch of people from the vjug went up
  • Fun references to Java and EE from the matrix in the background as they did dialog
  • Matrix is killing processes as they start up. Deployed 6 instances of programming using Kubernetes to Google, IBM and Oracle clouds. Had visual as killed instances and they came back up. Scaled it 400 and watched the isul grow. Lots of references to the cost. . Google hangout session into VJUG
  • Scene 6: Meet architect of Matrix and our hero defeats the agents.. The agents make up and turn back into dev

My take: I was thinking of not going to the keynote to get some extra sleep and then getting ready for my session. Glad I woke up at 6:30 to get ready and attended to whole keynote. IBM’s was interestng and well put together. And Oracle’s “movie” was awesome! And the more people you recognized, the better it was. Lots of references to various news to such as Angular not having a migration path.