blob: b05a1ca0579be70d58543abfc10ffcc86142e190 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html lang="en">
<head>
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2012. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>Eclipse and Java 7</title>
<script language="JavaScript" type="text/javascript" src="PLUGINS_ROOT/org.eclipse.help/livehelp.js"> </script>
</head>
<body>
<h1>Eclipse and Java 7</h1>
<p>
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.
</p>
<p>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.
</p>
<h2>Prerequisites</h2>
<p>
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
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.debug.ui.preferences.VMPreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Opens the Installed JRE preference page">
<strong>Java &gt; Installed JREs</strong></a>
preference page to register it with Eclipse.
</p>
<p>You can also configure a Java 7 execution environment, via the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.debug.ui.jreProfiles)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Opens the Execution Environments preference page">
<strong>Java &gt; Installed JREs &gt; Execution Environments</strong></a> preference page:
<p><img src="images/java7/ee-page.png" alt="The execution environments preference page"></p>
</p>
<p>To convert an existing Java project to Java 7, open the <b>Properties</b> of the
<b>JRE System Library</b> and choose Execution environment: <b>JavaSE-1.7</b>.
<p><img src="images/java7/select-ee.png" alt="The properties dialog"></p>
<p>The execution environment API description for Java 7 is available from the corresponding <a href="http://wiki.eclipse.org/Eclipse_Project_Update_Sites">update site</a>.
After installing it, one can detect invalid references to Java 7 system libraries:
<p><img src="images/java7/pde-api-pref-page.png" alt="The API Errors/Warnings preference page showing the new Java 7 EE description"></p>
</p>
<h2>Java 7 language features</h2>
<p>
Java 7 comes with a set of <a href="http://download.oracle.com/javase/7/docs/technotes/guides/language/enhancements.html#javase7">small enhancements</a>
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 <a href="http://download.oracle.com/javase/7/docs/technotes/guides/language/enhancements.html#javase7" target="_blank">here</a> for more information.
</p>
<ul>
<li><a href="#diamond">Improved Type Inference for Generic Instance Creation (Diamond)</a></li>
<li><a href="#Multi-catch">Multi-catch</a></li>
<li><a href="#try-with-resources"><code>try</code>-with-resources statement</a></li>
<li><a href="#simplified-varargs-method-invocation">Simplified Varargs Method Invocation</a></li>
<li><a href="#strings-in-switch">Strings in switch</a></li>
<li><a href="#numeric-literals">Binary Literals and Underscores in Numeric Literals</a></li>
<li><a href="#polymorphic-methods">Polymorphic Methods</a></li>
</ul>
<h2 id="diamond">Improved Type Inference for Generic Instance Creation (Diamond)</h2>
<p>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.</p>
<p>
Where possible, <b>content assist</b> for constructor invocations inserts a diamond instead of explicit type arguments.
<p><img src="images/java7/content-assist-diamond-1.png" alt="Content assist after 'new HashMap'"></p>
Result:
<p><img src="images/java7/content-assist-diamond-2.png" alt="Content assist inserted 'new HashMap&lt;&gt;()'"></p>
</p>
<p>
The compiler can also detect <b>redundant specification of type arguments</b>, which you can remove via the <b>Remove type arguments</b> quick fix.
<p><img src="images/java7/redundant-specification-of-type-arguments.png" alt="Redundant specification of type arguments"></p>
<p>This option is disabled by default but can be enabled on the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.ProblemSeveritiesPreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Opens the Errors/Warnings preference page">
<b>Java &gt; Compiler &gt; Errors/Warnings</b></a> preference page:</p>
<p><img src="images/java7/redundant-type-arguments-warning.png" alt="Redundant type arguments warning"></p>
</p>
<p>
In addition, you can also <b>insert inferred type arguments</b> of a diamond via a quick assist.
<p><img src="images/java7/insert-inferred-type-arguments.png" alt="Insert inferred type arguments"></p>
<p>Hint: This also works as a quick fix in 1.5 and 1.6 code, where the diamond is a syntax error.</p>
</p>
<h2 id="Multi-catch">Multi-catch</h2>
<p>In Java 7 you can catch multiple exception types using a single catch block.</p>
<p>
<b>Mark Occurrences</b> in Java editor can be used to highlight statements which throw a particular exception.
<p><img src="images/java7/throwing-exception.png" alt="Mark Occurrences"></p>
</p>
<p>
The <b>Source &gt; Surround With &gt; Try/multi-catch Block</b> action allows you to surround selected statements with a try/multi-catch block.
This is also available as <b>Surround with try/multi-catch</b> quick fix in case there are multiple uncaught exceptions.
<p><img src="images/java7/surround-with-try-multicatch.png" alt="Surround with try/multi-catch"></p>
</p>
<p>
The <b>Add exceptions to existing catch clause</b> quick fix allows you to add uncaught exceptions to an existing catch clause.
<p><img src="images/java7/add-exceptions-to-existin-catch-clause.png" alt="Add exceptions to existing catch clause"></p>
</p>
<p>
The <b>Use separate catch blocks</b> quick assist allows you to replace a multi-catch clause with individual catch blocks,
one for each exception in the multi-catch clause.
<p><img src="images/java7/use-separate-catch-blocks.png" alt="Use separate catch blocks"></p>
</p>
<p>
The <b>Move exceptions to separate catch block</b> quick assist allows you to pick out one or more selected exceptions from a multi-catch clause.
<p><img src="images/java7/move-exceptions-to-separate-catch-block.png" alt="Move exceptions to separate catch block"></p>
</p>
<p>
The <b>Combine catch blocks</b> 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.
<p><img src="images/java7/combine-catch-blocks.png" alt="Combine catch blocks"></p>
</p>
<p>
The compiler reports an error if an exception in a multi-catch clause is already caught by an alternative exception.
The <b>Remove exception</b> quick fix allows you to remove this exception.
<p><img src="images/java7/remove-exception.png" alt="Remove exception"></p>
</p>
<p>
In addition, there are <b>Line Wrapping options</b> in the formatter for the multi-catch syntax.
These can be configured on the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.CodeFormatterPreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Opens the Formatter preference page">
<b>Java &gt; Code Style &gt; Formatter</b></a> preference page
under <b>Line Wrapping &gt; Statements &gt; 'multi-catch'</b>.
</p>
<h2 id="try-with-resources">try-with-resources statement</h2>
<p>The <code>try</code>-with-resources statement ensures that each resource - a <code>java.lang.AutoCloseable</code> - is closed at the end of the statement.</p>
<p>
<b>Mark Occurrences</b> in the Java editor understands the <code>try</code>-with-resources syntax.
The closing '}' of a <code>try</code>-with-resources statement is marked as a method exit point if the implicit <code>close()</code>
invocation throws an exception. The corresponding resource variable is also highlighted.
<p><img src="images/java7/mark-occurrences-exception-thrown-by-automatic-close.png" alt="Mark Occurrences"></p>
</p>
<p>
The compiler detects <b>unhandled exceptions thrown by automatic <code>close()</code></b> invocation on a resource.
<p><img src="images/java7/unhandled-exception-thrown-by-automatic-close.png" alt="Detection of unhandled exceptions thrown by automatic close()"></p>
</p>
<p>
The compiler can <b>suggest using <code>try</code>-with-resources statement</b>
when the resources have been explicitly closed but not declared in <code>try</code>-with-resources statements.
<p><img src="images/java7/twr-example.png" alt="Compiler warning for a resource that should be managed with try-with-resource"
title="Compiler warning for a resource that should be managed with try-with-resource"></p>
</p>
<p>This option is disabled by default but can be enabled on the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.ProblemSeveritiesPreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Opens the Errors/Warnings preference page">
<b>Java &gt; Compiler &gt; Errors/Warnings</b></a> preference page:</p>
<p><img src="images/java7/twr-warning.png" alt="Resource not managed via try-with-resource warning"></p>
<p>
In addition, there are <b>Line Wrapping and White Space options</b> in the formatter for the <code>try</code>-with-resources syntax.
These can be configured on the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.CodeFormatterPreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Opens the Formatter preference page">
<b>Java &gt; Code Style &gt; Formatter</b></a> preference page
under <b>Line Wrapping &gt; Statements &gt; 'try-with-resources'</b> and <b>White Space &gt; Control Statements &gt; 'try-with-resources'</b>.
</p>
<h2 id="simplified-varargs-method-invocation">Simplified Varargs Method Invocation</h2>
<p>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.</p>
<p>
The <b>Add @SafeVarargs</b> quick fix is offered for potential heap pollution warnings on method declarations.
<p><img src="images/java7/add-safevarargs.png" alt="Add @SafeVarargs"></p>
<p>The quick fix is also offered from call sites.</p>
<p><img src="images/java7/add-safevarargs-to-declaration.png" alt="Add @SafeVarargs to declaration"></p>
</p>
<p>
The <b>Remove @SafeVarargs</b> quick fix is offered for incorrect usage of @SafeVarargs annotation.
<p><img src="images/java7/remove-safevarargs-1.png" alt="Remove @SafeVarargs"></p>
<p><img src="images/java7/remove-safevarargs-2.png" alt="Remove @SafeVarargs"></p>
</p>
<h2 id="strings-in-switch">Strings in switch</h2>
<p>You can use a String object in the expression of a switch statement.</p>
<p>
The <b>Convert 'switch' to 'if-else'</b> quick assist works for strings in switch.
<p><img src="images/java7/convert-switch-to-if-else.png" alt="Convert 'switch' to 'if-else' quick assist"></p>
<p>Note that the quick assist avoids a NullPointerException in the resultant code.</p>
</p>
<h2 id="numeric-literals">Binary Literals and Underscores in Numeric Literals</h2>
<p>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.</p>
<p>
The feature for changing the value of a variable while debugging supports underscores in numeric literals
and binary literals:
<p><img src="images/java7/assign-vars.png" alt="An example of assigning a binary literal with underscores to an int"></p>
</p>
<h2 id="polymorphic-methods">Polymorphic Methods</h2>
<p>
Javadoc hovers for references to
<a href="http://download.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html#sigpoly">polymorphic methods</a>
show the actually used method signature.
<p><img src="images/java7/invokeExact-javadoc.png" alt="Polymorphic method signature in javadoc hover"></p>
</p>
Happy coding!
</body>
</html>