days
0
-38
0
hours
-1
-9
minutes
-5
0
seconds
-1
-4
search

Embracing Java 9 and beyond with Eclipse JDT

Sarika Sinha
MicroProfile
© Shutterstock / Sadovski

The world around Java is changing at a fast pace. How is the Eclipse IDE keeping up? In this article, Sarika Sinha explains the support that Eclipse Phton now has for Java 9 and Java 10 features.

The world around Java is changing at a fast pace. Java now has bi-yearly releases, this started March 2018. Java 10 was released in March 2018 and Java 11 is expected in September 2018. Not so far back, in September 2017, we had the Java 9 release. Quarterly releases of the Eclipse IDE in March, June, September, and December will help provide a release with a new version of Java without much delay. The latest release, Eclipse Photon is now available and supports Java 9 and Java 10. It has added compiler, UI and launching support for Java 9 and Java 10.

Java 9 features

1. Private interface methods – Java 8 allowed default methods in Interface and Java 9 allows private scoped and static methods in interfaces. This will help programmers to keep reusable codes in private interface methods.

Eclipse JDT

2. Try-With Resources enhancements – Java 8 mandated the variable to be created for the try-with resources block. Java 9 allows final and effectively final variables to be placed in try-with-resource blocks.

Eclipse JDT

3. @SafeVarargs Annotation for private instance methods – Java 7 allowed @SafeVarargs annotation for final and static methods and the Constructors. Java 9 allows @SafeVarargs annotation for private instance methods also. It can not be used for public instance methods.

Eclipse JDT

4. Collection Factory Methods – Java 9 has added simple ways to create immutable Collections without null values by providing a set of <Collection>.of methods. The created collection should not have null values and should not be modified later to avoid runtime exceptions.

Eclipse JDT

5. Stream improvements – Java 9 added the new APIs for Stream.

  • Stream improvements–Java 9 added the new APIs for Stream. Stream iterate – A terminating condition can be added to the iterate method.
  • Stream takeWhile – takeWhile is another way to add a terminating condition to iterate method.
  • Stream dropWhile – dropWhile drops a subset of elements from the Stream till it matches the specified condition.
  • Stream ofNullable – ofNullable method returns a sequential Stream with single value only if the Stream is not null. This method helps the programmers to avoid adding the null checks to the Stream.

Eclipse JDT

6. Option –release  – Java 9 released –release option. In Eclipse IDE, if you have Java 9 in your build path you can still use the –release option along with compliance level 1.6 or more for the workspace or a project. Without this option, when Java 1.8 was on build path and the compliance level is set as 1.7, the code base still gets compiled against the APIs available with the Java 1.8 JRE found on the path. This is available only if build path has a JRE 9 or above and the compliance level used is 1.6 or more. This also means Eclipse does not need EE descriptor file and the default access rules for JRE libraries are not required anymore. Bot of these things can be achieved by the –release option.

Eclipse JDT

Eclipse JDT

7. Modules – Project Jigsaw introduced the Java 9 Platform Module System. Module is a uniquely named reusable group of related packages, as well as resources and a module descriptor (module-info.java)
All JDK Modules starts with “jdk.*”. All Java SE Specifications Modules starts with “java.*”. Java 9 Module System has a “java.base” Module which is an independent module.

There are many UI features supported in Eclipse to help the Java programmers use Java Modules.

Eclipse JDT

Eclipse JDT

Eclipse JDT

In the Eclipse IDE, a modular Java Project can be created using the New Java Project Menu which has by default Create module-info.java option set to true. Users can specify the module name for the Java Project. They can also create/update module-info.java file later using Configure Menu available.

Eclipse JDT

Java search now includes a new search scope – Module.Eclipse JDT

Java Build Path allows the addition of Projects and jars to Modulepath or Classpath.

Eclipse JDT

Is modular node is changed to Is not modular if the Build Path Project library is moved to Classpath.

Eclipse JDT

Eclipse JDT

Encapsulation properties of Modulepath elements can be modified using Is modular node. The modules can be limited by adding the modules explicitly, the –add-reads and –add-exports ca be added to the packages in a module. It can be used to patch a module.

Eclipse JDT

Eclipse IDE provides a quick fix in module-info.java to import class and add requires to module-info.java.

Eclipse JDT

Eclipse JDT

Eclipse JDT

If a buildpath entry specified in Classpath is used in module-info.java, the Eclipse IDE will suggest moving of the classpath entry to modulepath.

Eclipse JDT

Eclipse IDE provides a quick fix in a class to add requires to module-info.java.

Eclipse JDT

Eclipse workspace provides a quick fix in import to add requires to module-info.java.

8. Launching with Java 9 and beyond – As the Java Build Path page supports Module Path and Class Path, Launch Configuration Dialog for Java 9 and beyond has added this support via the Dependencies tab.

Eclipse JDT

Eclipse JDT

Eclipse JDT

9. Overriding dependencies while launching for Java 9 and beyond – Override Dependency dialog allows a user to override the encapsulation rules like patching, adding exports, adding reads, limiting modules for launching.

Eclipse JDT

Eclipse JDT

The dialog shows the dependencies derived from the build path and gives the option to override them.

Here’s an example of mixing non-modular code in the named module:

Eclipse JDT

Eclipse JDT

A Junit 5 test file in the test folder resides out of the module scope and needs to patch the module and –add-reads for the module to ALL-UNNAMED.

Java 10 features

1. Local variable type inference support for Java 10 – Java 9 extends type inference to declarations of local variables with initializers.

Eclipse JDT

Eclipse JDTEclipse JDT

Eclipse IDE supports the autocompletion of var, it shows the inferred type on the hover.

Eclipse JDT

Eclipse IDE suggests the usage of var or the inferred type to be used instead.

Eclipse JDT

Eclipse IDE also suggests changing the compliance to 10 if the var that is used in a Project has a compliance level lower than 10.

2. Time-based release versioning – As Java will have two releases a year with specified release versions, the Eclipse IDE allows to add future Java versions with a warning.

Eclipse JDT

Eclipse JDT

A Java program can also be launched with future versions of Java.

Eclipse JDT

Conclusion

This article covers some of the important features of Java 9 and Java 10 supported by Eclipse JDT. Some more details can be found at:

 

eclipseorb_color

This post was originally published in the June 2018 issue of the Eclipse Newsletter: Eclipse Photon – A Quantum Leap

For more information and articles check out the Eclipse Newsletter.

Author

Sarika Sinha

Sarika Sinha is an Advisory Staff Software Engineer at IBM. Follow her on Twitter @SarikaSinha1


Leave a Reply

avatar
400