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.
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 Java > Installed JREs preference page to register it with Eclipse.
You can also configure a Java 7 execution environment, via the Java > Installed JREs > 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 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:
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.
try
-with-resources statementThis 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.
Result:The compiler can also detect redundant specification of type arguments, which you can remove via the Remove type arguments quick fix.
This option is disabled by default but can be enabled on the Java > Compiler > Errors/Warnings preference page:
In addition, you can also insert inferred type arguments of a diamond via a quick assist.
Hint: This also works as a quick fix in 1.5 and 1.6 code, where the diamond is a syntax error.
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.
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.
The Add exceptions to existing catch clause quick fix allows you to add uncaught exceptions to an 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.
The Move exceptions to separate catch block quick assist allows you to pick out one or more selected exceptions from a multi-catch clause.
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.
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.
In addition, there are Line Wrapping options in the formatter for the multi-catch syntax. These can be configured on the Java > Code Style > Formatter preference page under Line Wrapping > Statements > 'multi-catch'.
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.
The compiler detects unhandled exceptions thrown by automatic close()
invocation on a resource.
The compiler can suggest using try
-with-resources statement
when the resources have been explicitly closed but not declared in try
-with-resources statements.
This option is disabled by default but can be enabled on the Java > Compiler > Errors/Warnings preference page:
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
Java > Code Style > Formatter preference page
under Line Wrapping > Statements > 'try-with-resources' and White Space > Control Statements > 'try-with-resources'.
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.
The quick fix is also offered from call sites.
The Remove @SafeVarargs quick fix is offered for incorrect usage of @SafeVarargs annotation.
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.
Note that the quick assist avoids a NullPointerException in the resultant code.
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:
Javadoc hovers for references to polymorphic methods show the actually used method signature.
Happy coding!