Eclipse and Java 7

Since release 3.7.1, Eclipse includes full support for the new Java language features of Java SE 7. One of the most important consequences of this support is that you may not notice it at all - everything you expect to work for Java 6, including editing, compiling, debugging, quick fixes, refactorings, source actions, searching, etc., will work seamlessly with Java 7's new syntax. In this document, we will introduce some of the more interesting capabilities Eclipse users will find when working with Java 7.

Note that both version numbers '1.7' and '7' are used to identify the release of the Java Platform Standard Edition. Version '7' is the product version, while '1.7' is the developer version and is also used for the compiler compliance level.

Prerequisites

In order to develop code compliant with Java 7, you will need a Java 7 Java Runtime Environment (JRE). If you start Eclipse for the first time using a Java 7 JRE, then it will use it by default. Otherwise, you will need to use the Opens the Installed JRE preference page Java > Installed JREs preference page to register it with Eclipse.

You can also configure a Java 7 execution environment, via the Opens the Execution Environments preference page Java > Installed JREs > Execution Environments preference page:

The execution environments preference page

To convert an existing Java project to Java 7, open the Properties of the JRE System Library and choose Execution environment: JavaSE-1.7.

The properties dialog

The execution environment API description for Java 7 is available from the corresponding update site. After installing it, one can detect invalid references to Java 7 system libraries:

The API Errors/Warnings preference page showing the new Java 7 EE description

Java 7 Language Features

Java 7 comes with a set of small enhancements to the Java language (aka Project Coin), a new byte code to dynamically invoke methods, and many additions to the libraries. This document introduces some of the new language features in Java 7 very briefly, but it is not a proper tutorial for these features. See here for more information.

Improved Type Inference for Generic Instance Creation (Diamond)

This language change allows explicit type arguments to constructors of parameterized classes to be omitted in many situations. The compiler infers the omitted type arguments based on the expected type.

Where possible, content assist for constructor invocations inserts a diamond instead of explicit type arguments.

Content assist after 'new HashMap'

Result:

Content assist inserted 'new HashMap<>()'

The compiler can also detect redundant specification of type arguments, which you can remove via the Remove type arguments quick fix.

Redundant specification of type arguments

This option is disabled by default but can be enabled on the Opens the Errors/Warnings preference page Java > Compiler > Errors/Warnings preference page:

Redundant type arguments warning

In addition, you can also insert inferred type arguments of a diamond via a quick assist.

Insert inferred type arguments

Hint: This also works as a quick fix in 1.5 and 1.6 code, where the diamond is a syntax error.

Multi-catch

In Java 7 you can catch multiple exception types using a single catch block.

Mark Occurrences in Java editor can be used to highlight statements which throw a particular exception.

Mark Occurrences

The Source > Surround With > Try/multi-catch Block action allows you to surround selected statements with a try/multi-catch block. This is also available as Surround with try/multi-catch quick fix in case there are multiple uncaught exceptions.

Surround with try/multi-catch

The Add exceptions to existing catch clause quick fix allows you to add uncaught exceptions to an existing catch clause.

Add exceptions to existing catch clause

The Use separate catch blocks quick assist allows you to replace a multi-catch clause with individual catch blocks, one for each exception in the multi-catch clause.

Use separate catch blocks

The Move exceptions to separate catch block quick assist allows you to pick out one or more selected exceptions from a multi-catch clause.

Move exceptions to separate catch block

The Combine catch blocks quick assist allows you to combine separate catch blocks into a single multi-catch block. The quick assist is offered only when bodies of all the catch blocks are same.

Combine catch blocks

The compiler reports an error if an exception in a multi-catch clause is already caught by an alternative exception. The Remove exception quick fix allows you to remove this exception.

Remove exception

In addition, there are Line Wrapping options in the formatter for the multi-catch syntax. These can be configured on the Opens the Formatter preference page Java > Code Style > Formatter preference page under Line Wrapping > Statements > 'multi-catch'.

Try-with-resources Statement

The try-with-resources statement ensures that each resource - a java.lang.AutoCloseable - is closed at the end of the statement.

Mark Occurrences in the Java editor understands the try-with-resources syntax. The closing '}' of a try-with-resources statement is marked as a method exit point if the implicit close() invocation throws an exception. The corresponding resource variable is also highlighted.

Mark Occurrences

The compiler detects unhandled exceptions thrown by automatic close() invocation on a resource.

Detection of unhandled exceptions thrown by automatic close()

The compiler can suggest using try-with-resources statement when the resources have been explicitly closed but not declared in try-with-resources statements.

Compiler warning for a resource that should be managed with try-with-resource

This option is disabled by default but can be enabled on the Opens the Errors/Warnings preference page Java > Compiler > Errors/Warnings preference page:

Resource not managed via try-with-resource warning

In addition, there are Line Wrapping and White Space options in the formatter for the try-with-resources syntax. These can be configured on the Opens the Formatter preference page Java > Code Style > Formatter preference page under Line Wrapping > Statements > 'try-with-resources' and White Space > Control Statements > 'try-with-resources'.

Simplified Varargs Method Invocation

The Java 7 complier generates a warning at the declaration site of a varargs method or constructor with a non-reifiable varargs formal parameter. This warning can be suppressed using the @SafeVarargs annotation.

The Add @SafeVarargs quick fix is offered for potential heap pollution warnings on method declarations.

Add @SafeVarargs

The quick fix is also offered from call sites.

Add @SafeVarargs to declaration

The Remove @SafeVarargs quick fix is offered for incorrect usage of @SafeVarargs annotation.

Remove @SafeVarargs

Remove @SafeVarargs

Strings in Switch

You can use a String object in the expression of a switch statement.

The Convert 'switch' to 'if-else' quick assist works for strings in switch.

Convert 'switch' to 'if-else' quick assist

Note that the quick assist avoids a NullPointerException in the resultant code.

Binary Literals and Underscores in Numeric Literals

In Java 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. In addition, any number of underscore characters (_) can appear anywhere between digits in a numerical literal.

The feature for changing the value of a variable while debugging supports underscores in numeric literals and binary literals:

An example of assigning a binary literal with underscores to an int

Polymorphic Methods

Javadoc hovers for references to polymorphic methods show the actually used method signature.

Polymorphic method signature in javadoc hover

Happy coding!