preparing your code for java 9
TRANSCRIPT
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Preparing your code for Java 9 Eclipse Summit, India
Deepu Xavier Product Manager Java Platform Group 26 August 2016
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Agenda
• Java 9 Schedule
• How a developer can leverage and adjust to Java 9 features – Jshell
– Javadoc Search
– Internal API encapsulation
– Process API
– Convenience Factory Methods for Collections
– Version String – V for Verona
– Multi Release Jar Files
– Gone, gone, gone…
• The Jigsaw way – How to make your own modular image
• What Next
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Java 9 Timeline
Schedule Milestone Target Date
Feature Complete 26 May 2016
All Tests Run 11 August 2016
Rampdown Start 01 September 2016
Zero bug Bounce 20 October 2016
Rampdown Phase 2 01 December 2016
Final Release Candidate 26 January 2017
General Availability 23 March 2017
https://jdk9.java.net/
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 6
JDK Enhancement Proposals (JEPs) for Java 9 Total
84 JEPs
Jigsaw Client Lib Core Lib Core SVC Hotspot JavaFX Security Lib Tools XML Others
Version String Linux/AArch64
Port Infra & Deploy- 3
JEPs
7 JEPs 19 JEPs 2 JEPs 17 JEPs 3 JEPs 9 JEPs 15 JEPs 1 JEPs 5 JEPs
Note: The content of this slide is based on information available as of 26th July 2016
6 JEPs
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 7
Java 9: Jigsaw and Many More…
Process API Improvements
Multi-Resolution Images
Interned String in CDS Unicode Compatibility
Default GC
JVM Command Validation
Compact String
Segmented Code Cache
Reserved Stack Areas for Critical Sections
Better Import Statement Handling
Convenient Collections Handling
Concurrency Updates
Multi Release JAR
Smart Compilation 2
HiDPI Graphics on Windows and Linux
Microbenchmark Suite More JVM Diagnostic
Commands Compile for Older
Platform Versions
Jigsaw
HTTP2 Compliance
New Version String
http://openjdk.java.net/projects/jdk9/
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 222: JShell • Lets you try code.
• Supports libraries, tab completion, up/down arrows, etc.
• Not for “create a class or method from this prose.”
8
•Without JShell, I often wrote random unit tests or main methods to try things. •With JShell, I can try things and close it when I’m done.
JEP 222
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 225: Javadoc Search
• Add a search box to generated API documentation that can be used to search for program elements and tagged words and phrases within the documentation.
tools / javadoc(tool)
Java 8 Javadoc: https://docs.oracle.com/javase/8/docs/api/
Java 9 Javadoc: http://download.java.net/java/jdk9/docs/api/index.html
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 260: Encapsulate Most Internal APIs
• Make most of the JDK's internal APIs inaccessible by default but leave a few critical, widely-used internal APIs accessible, until supported replacements exist for all or most of their functionality
– In order to keep critical APIs without a replacement accessible by default sun.misc and sun.reflect will not be hidden and a few APIs kept “public”
• sun.misc.Unsafe
• sun.misc.{Signal,SignalHandler}
• sun.reflect.Reflection::getCallerClass
• sun.reflect.ReflectionFactory
– All other APIs in these packages (e.g. sun.misc.Base64) will be removed
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 11
Categories of APIs in JDK
• Supported, intended for external use
• JCP standard, java.*, javax.* • JDK-specific API, some com.sun.*, some jdk.*
• Unsupported, JDK-internal, not intended for external use • sun.* mostly
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 12
Internal API Related Changes
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 13
General compatibility policies
•If an application uses only supported, non-deprecated, APIs and works on release N then it should work on N+1, even without recompilation •Supported APIs can be removed but only with advanced notice
How we manage incompatibilities • Judge risk and impact based on actual data (where possible) • Communicate early and vigorously through Early Access, OpenJDK Mails etc. • Make it easy to understand how existing code will be affected • When removing unsupported APIs, provide replacements where it make sense • Provide workarounds where possible
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 14
JEP 260 proposal Encapsulate all non-critical internal APIs by default
Encapsulate all critical internal APIs for which supported replacements exist in JDK 8 Do not encapsulate critical internal APIs • Deprecate them in JDK 9 • Plan to remove in JDK 10 Provide a workaround via command-line flag
Non-critical No evidence of use outside of JDK or used only for convenience Critical Functionality that would be difficult, if not impossible, to implement outside of the JDK
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 15
Finding uses of JDK-internal APIs
• jdeps tool in JDK 8, improved in JDK 9 • Maven JDeps Plugin
$ jdeps -jdkinternals glassfish/modules/security.jar
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Code shown in the following examples are purely author’s imagination or used in a fictitious manner. Any resemblance to your application code “could be” purely coincidental.
Internal API !!!
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
jdeps To The Rescue
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
The Right Way
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 102: Process API Updates
19
• There has been a huge limitation in managing and controlling OS processes with Java • Platform compatibility was always a challenge. At present, developers resort to native code or much
verbose workaround
• With Java 9, processes can be handled using new direct methods like: • ProcessHandle currProcess = ProcessHandle.current(); • ProcessHandle.allProcesses()
.filter(processHandle -> processHandle.info().command().isPresent()) .limit(3) .forEach((process) ->{// action here}
//In Windows Process myProc = Runtime.getRuntime().exec ("tasklist.exe"); InputStream procOutput = myProc.getInputStream (); if (0 == myProc.waitFor ()) { // actions here }
//For Linux Process myProc = Runtime.getRuntime().exec("ps -e"); InputStream procOutput = myProc.getInputStream (); if (0 == myProc.waitFor ()) { // actions here }
Note: This is just couple of new methods/ classes. Please refer the respective javadoc for full list
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
The Right Way
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 269: Convenience Factory Methods for Collections
• Define library APIs to make it convenient to create instances of collections and maps with small numbers of elements, so as to ease the pain of not having collection literals in the Java programming language
• Less boilerplate and reduced amount of code needed for creating small collections and maps
• Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
– populate a collection using a copy constructor from another collection:
• Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{ add("a"); add("b"); add("c"); }});
– use the so-called "double brace" technique:
• Set<String> set = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(toSet()));
– by combining stream factory methods and collectors
Set<String> alphabet = Set.of("a", "b", "c");
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Overload Difficulty: List.of(array)
Oracle Confidential – Internal 22
Suppose you have this: String[] array = { "a", "b", "c" };
List.of(array);
Should this create:
A. List<String> containing three strings?
B. List<String[]> containing a single array?
JEP 269
ANSWER: A. Compiler chooses varargs, so we get List<String> with three strings Probably right; list of arrays is rare. Workaround:
List<String[]> listOfArray = List.<String[]>of(array);
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 23
JEP 223: New Version-String Scheme
Revise the JDK's version-string scheme so that it is easier to distinguish major, minor, and security-update releases. Goal is to device a new version numbering scheme which is
easily understandable aligning with industry best practices (Semantic Versioning) is easy to distinguish between major, minor and critical patch update (CPU) releases
New scheme will follow MAJOR.MINOR.SECURITY (-PRE)?(\+BUILD)?(-OPT)? convention
MAJOR : The major version number will be incremented for every major release MINOR : The minor version number will be incremented for every minor update release MINOR is reset to zero when MAJOR is incremented SECURITY: The security version number will be incremented for every security update release
SECURITY is reset to zero only when MAJOR is incremented A higher value of SECURITY for a given MAJOR value, therefore, always indicates a more secure release, regardless of the value of MINOR
PRE: This represents a pre-release identifier. E.g. ea, for an early-access release BUILD: Build number may be promoted by 1 for every promoted build. BUILD is reset to one when any portion of MAJOR.MINOR.SECURITY is incremented OPT: Additional build information
Note: Existing code that assumes the initial element to have the value 1, however, and therefore always skips to the second element when comparing version numbers, will not work correctly e.g., such code will consider 9.0.1 to precede 1.8.0.
Version # Detail
9 Java 9 GA
9.0.1 CPU: 9 + critical changes
9.1.1 Minor release: 9.0.1 + other changes
9.1.2 CPU: 9.1.1 + critical changes
9.2.2 Minor release: 9.1.2 + other changes
Hypothetical Release Sequence (for illustration only)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 282: jlink: The Java Linker
• Create a tool that can assemble and optimize a set of modules and their dependencies into a custom run-time image as defined in JEP 220. Define a plugin mechanism for transformation and optimization during the assembly process, and for the generation of alternative image formats
• Create a custom runtime optimized for a single program
• JEP 261 defines link time as an optional phase between the phases of compile time and run time. Link time requires a linking tool that will assemble and optimize a set of modules and their transitive dependencies to create a run-time image or executable.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 25
JEP 238: Multi-Release JAR Files
Extend the JAR file format to allow multiple, Java-release-specific versions of class files to coexist in a single archive.
• It is practically difficult to express conditional platform dependencies or to distribute separate
library artifacts for each new java version
• A multi-release JAR ("MRJAR") will contain additional directories for classes and resources
specific to particular Java platform releases.
With this, it is possible for versions of a class designed for a later Java platform version to override the version of that same class designed for an earlier Java platform release.
META-INF
Content Root A.class B.class C.class D.class
Normal JAR
META-INF versions -8 A.class
B.class -9
A.class
Content Root A.class B.class C.class D.class
MR JAR
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 289: Deprecate the Applet API
• Deprecate the Applet API, which is rapidly becoming irrelevant as web-browser vendors remove support for Java browser plug-ins
JEP 241: Remove the jhat Tool
• Remove the antiquated jhat tool
• jhat is an experimental, unsupported, and out-of-date tool added in JDK 6
• Superior heap visualizers and analyzers have now been available for many years
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JEP 231: Remove Launch-Time JRE Version Selection • Remove the ability to request, at JRE launch time, a version of the JRE that is not the JRE being launched.
• Modern applications are typically deployed via Java Web Start (JNLP), native OS packaging systems, or active installers, and all of these technologies have their own ways of finding, and even sometimes installing and later updating, an appropriate JRE for the application.
JEP 240: Remove the JVM TI hprof Agent • Remove the hprof agent from the JDK
• The hprof agent was written as demonstration code for the JVM Tool Interface and not intended to be a production tool.
• The useful features of the hprof agent have been superseded by better alternatives. Alternatives Heap dumps: GC.heap_dump (jcmd <pid> GC.heap_dump)/ jmap –dump Allocation profiler : Java VisualVm & other 3rd party tools CPU profiler: Java VisualVm and Java Flight Recorder
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
What Next ?
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Download Early Access and Try Java 9
https://jdk9.java.net/
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
How can Open Source projects get involved to test JDK 9?
• Helps open source projects understand changes and know how to test. • Check your code for internal APIs using Jdeps. • Run your tests on JDK 9. https://wiki.openjdk.java.net/display/quality/Quality+Outreach
30
Join the OpenJDK Quality Outreach Campaign
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.