Foojay Today

Keeping Pace with Java Using Eclipse IDE

March 30, 2021

The Java language has been evolving at a fast pace with a six month release cadence and preview features.

With faster Java releases, it’s an exciting time to be a Java developer. Every new release of Java promises interesting features and updates.

To give them a spin, you have the tooling support in Eclipse Java IDE ready at your disposal.

Under the Hood

The Java tooling in Eclipse IDE has its own compiler implementation which not only generates class files, but also produces a Java document model which forms the basis for implementing a large number of tooling features.

To support the latest Java versions, the Eclipse Compiler for Java implements all the new language enhancements, existing tooling support is updated to blend with the new language features, and new functionality is added to help you while working with the new language constructs.

Here is a sneak peek into the major interesting features of recent Java versions with their support in the Eclipse IDE.

Highlights

To get started, you can add a JRE in Preferences > Java > Installed JREs:

Then, set the JDK compliance in Preferences > Java > Compiler:

To quickly enable the preview features on an existing Java project, you can right-click on it in the Package or Project Explorer and select Configure > Enable preview features:

When a preview feature is used in the code, a default warning is provided by the compiler that the preview feature may not be supported in a future release. You can choose to ignore this problem or set it to Info by changing its severity level on the Java Compiler preference page:

Sealed Classes

Sealed classes have received a second round of preview in Java 16 and they can be used to restrict the type hierarchy of a class. You can create a permitted class or interface declaring the sealed type as its super type with the provided Quick Fix (Ctrl/Cmd + 1):

The type hierarchy needs to be controlled at the permitted sub type by declaring it as final, sealed, or non-sealed, which can be done using the provided Quick Fixes:

Records

You can create a record in Eclipse IDE by using the New > Record wizard which provides more options like selecting the visibility modifier and adding the interfaces to be implemented by the record:

You can run a Java program which is using the record to see that the record instance is provided with auto-generated constructor, component accessor, toString, equals, and hashcode methods:

Eclipse IDE allows you to perform the rename refactoring on record components and it updates the accessor method names along with the component references:

A lot of new settings have been added to the formatter profile to control the formatting of records. You can use the filter to quickly view these configurable settings:

Pattern Matching for instanceof

Pattern matching for instanceof provides a pattern variable with the instanceof operator. Eclipse IDE provides you a new clean up option on the Java Feature tab of your clean up profile to use the pattern matching for instanceof and simplify your code by reducing explicit casts created after an instanceof check:

Eclipse IDE understands the pattern variable’s type and scope, and allows you to perform various actions like invoking the content assist (Ctrl + Space) and renaming the pattern variable:

Text Blocks

You can create a text block by enclosing any text in triple quotes. Eclipse IDE makes it easier for you to add this new delimiter with the new keyboard shortcut Ctrl/Cmd + Shift + ' (apostrophe). You can also select any existing text and use this key binding to quickly enclose it in text block delimiters:

The indentation of a text block can be configured in the formatter profile:

Switch Expressions

Eclipse IDE provides you a Quick Assist (Ctrl/Cmd + 1) and clean up option to convert eligible switch statements into switch expressions:

Many quick fixes, quick assists, and templates are also provided to help you in writing code with switch improvements. For example, there are quick fixes to add the default case or the missing case statements in a switch expression:

The proposals are inserted in linked mode so you can quickly replace them with the desired values:

A quick assist is provided to split multiple labels in a single case statement so that you can provide separate code for these cases if needed:

You can press Ctrl/Cmd and click on the default and case keywords to quickly navigate to the beginning of the switch expression.

The formatter profile has new settings that allow you to control spaces at various locations in switch expressions:

Local-Variable Type Inference

While extracting an expression to a local variable, you can choose to declare the type of the local variable as var:

Quick Assists are provided to change the type of a variable from var to the inferred type and vice-versa:

You can view the inferred type for var by just hovering over it and you can also use Ctrl/Cmd + click on var to navigate to that type.

Java Platform Module System

To convert an existing Java project to a modular project by creating a module-info.java file in it, you can right-click on the project in the Package or Project Explorer and select Configure > Create module-info.java:

The module-info.java file is created with directives like exports, requires, etc. based on the existing contents of the project.

You can go to the project’s properties and add libraries to its modulepath on the Java Build Path > Libraries tab:

You can also configure the properties of its module graph on the Java Build Path > Module Dependencies tab:

Quick Fixes are provided to identify the used types and add the required modules to module-info.java file:

You can create and export a new non-empty package directly from the module-info.java file using the provided Quick Fixes:

You can also provide a service implementation from module-info.java with the help of the provided Quick Fixes:

Functional Interface Instances

You can hover over the -> of a lambda expression or the :: of a method reference to view the functional interface method being implemented and you can use Ctrl/Cmd + click on these operators to navigate to that method:

Eclipse IDE provides you many quick assist, clean up, formatting, and refactoring options to work with anonymous classes, lambda expressions, and method references:

Summary

This post provides you a glimpse of the tooling features provided by Eclipse IDE as part of its support for the recent Java versions.

The 2021-03 (4.19) release of the Eclipse IDE provides integrated support up to Java 15 and it supports Java 16 with the Eclipse Marketplace entry.

So, download it now and start taking advantage of these enhancements!

Topics:

Author(s)

  • Noopur Gupta

    Noopur Gupta is an open source enthusiast working as an advisory software engineer at IBM India Software Labs. She co-leads the Eclipse Java Development Tools project and is a member ... Learn more

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard