Java development tools

Java Editor

CamelCase in Quick Outline The Quick Outline (Ctrl+O) and Quick Hierarchy (Ctrl+T) in the Java Editor now also support camelCase matching on the element name, quite similar to the Open Type dialog and Content Assist.

Quick Outline of StyledText with pattern 'gCa'

The * and ? wildcards still work, of course.
Quick Assist to convert enhanced for loop Ever encountered an enhanced for loop and needed access to the Iterator or to the loop index? Now, you can just use a Quick Assist (Ctrl+1) to convert the loop:

Screenshot of Quick Assist on the 'for' keyword of an enhanced 'for' loop

  • Convert to indexed 'for' loop is available for expressions of array- and List-based types.
  • Convert to Iterator-based 'for' loop is available for expressions of type Iterable.
Improved bracket matching support in Java editor The Java editor now supports highlighting of enclosing brackets.

Enclosing brackets

This can be configured on the Java > Editor preference page.

Preferences for bracket highlighting

Also, the Navigate > Go To > Matching Bracket action (Ctrl+Shift+P) now works everywhere in a file. If a bracket is not selected before invoking the action, the action navigates to the nearest enclosing end bracket. On invoking the action a second time, it navigates to the corresponding start bracket. On the third invocation, the original selection is restored.

So far, a matching bracket was found when the caret was placed immediately after a bracket. Now, a matching bracket is found both when the caret is placed either immediately before or immediately after a bracket.

Matching bracket examples

You can hover over a closing curly brace and see the source near the matching opening brace in a hover. This is helpful when you want to see the code near the start of a long code block. The hover also tells the number of lines in the code block.

Bracket hover example 1

The hover is also useful to temporarily 'highlight' a short code block.

Bracket hover example 2

Javadoc hover shows parameter annotations Javadoc hovers now render the annotations on method parameters:

org.eclipse.User.User(@NonNull String name, int uid, @Nullable String email)

'*.class without source' file type The new *.class without source file type allows to associate an internal or external editor to class files that have no source attached. This e.g. allows to open the class file in a decompiler:

Screenshot of the 'File Associations' preference page

Default im­ple­men­ta­tions for correction proposals JDT now provides the following default implementations for correction proposals that can be used to contribute quick fixes and quick assists.
  • org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal
  • org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal
  • org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal
This makes it easier to implement quick fixes/assists that operate on .java files and use an ASTRewrite.
Content assist in package-info.java Content assist (Ctrl+Space) is now available inside package-info.java, which is used to declare package-level annotations and Javadocs.

Content assist popup to add package-level annotations on package declaration

Java Compiler

Selectively ignore errors/warnings from source folders You can now configure JDT to ignore optional errors/warnings from certain source folders. Project properties > Java Build Path > Compiler > Source.

Java Build path page with 'Ignore optional compile problems' highlighted

With this option set to Yes, JDT will suppress errors/warnings configured in the Preferences > Java > Compiler > Errors/Warnings page. This can be desirable for source folders that contain auto-generated classes or tests.

Enhanced diagnostics for detection of incomplete switch statements The JDT's analysis regarding incomplete switch statements is now stronger and can be configured more in detail.

The compiler now separately checks two criteria for a switch statement to be complete:

  • A switch statement on an enum should define one case statement for each of the enum's constants
  • Each switch statement independent of type should define a default case.

By default the compiler warns for an enum switch where both criteria are violated:

Warning on switch over enum with missing default case

This warning is controlled by the option Preferences > Java > Compiler > Errors/Warnings > Incomplete 'switch' cases on enum. This option can be strengthened by selecting the new sub-option Signal even if 'default' case exists, in which case JDT will flag switch statements over an enum type which may have an enum value covered only through a default statement, but not directly through a case statement. When new enum constants are added, this option will help in detecting the switch statements that do not cover the new value.

Warning on switch with missing case statement for enum value even though default is present

Compiler Errors/warnings preference page with new switch diagnostic options highlighted

A second addition in the same section of the compiler preferences, Switch is missing 'default' case, controls whether the JDT will flag switch statements with a missing default case, because such a switch statement will be skipped entirely at runtime if an unexpected value occurs.

Warning on switch with missing default case

Note that in cases like the above, the warning also helps to understand another error against the last statement:
"The local variable printString may not have been initialized" is reported because clients could possibly call the method print(Color) with an enum value that is not known at the time of compiling this method.

New options to detect resource leaks The compiler can now detect leakage of resources i.e. local variables of type java.lang.AutoCloseable (compliance >= 1.7) and java.io.Closeable (compliance <= 1.6).

Errors/Warnings page with 'Resource leaks' highlighted

When the Resource leak option on the Java > Compiler > Errors/Warnings preference page is enabled, the compiler will issue an error or a warning if the close() method is not invoked locally on a resource.

Resource leak example

When you enable the Potential resource leak option on the Java > Compiler > Errors/Warnings preference page, the compiler will issue an error or a warning if the close() method is not invoked locally on a resource on all the execution paths.

Potential resource leak example

This analysis is less aggressive if a resource is shared between methods, possibly via a field, where any party accessing the resource could possibly issue the desired call to close(). In some cases (e.g.: passed as an argument to another method) the warning will be softened to only report a potential leak, in other cases (stored in a field) the analysis will be entirely silent.

Additionally the analysis recognizes certain cases as being practically unimportant. Some examples where the absence of a close() call does not trigger any error/warning are given below:

  • Resource-less implementors of Closeable, such as StringReader.

    no resource leak reported if resource-less implentor of Closeable is not closed

  • Wrapping implementors of Closeable, such as BufferedReader, that simply wrap a resource-less Closeable, directly or indirectly.

    no resource leak reported when a resource which wraps a resource-less implentor of Closeable is not closed

  • Wrapped resources where any resource in the chain is (potentially) closed.

    potential resource leak reported when any resource in the chain of wrapper resources is closed along some execution path.

New Batch compiler warning options Two new batch compiler options are now available to configure the compile time warnings:
  • -warn:all can be used to enable all compiler warnings
  • -warn:resource can be used to enable resource leak warnings
New build path option to warn when a source folder's output location overlaps another source folder JDT now shows a build path error when a source folder's output location overlaps another source folder. This error can be configured on the Java > Compiler > Building > Output location overlaps source location preference page.

Build path error when source folder's output location overlaps another source folder

Annotation-based null analysis JDT can now be configured to use annotations designated as null annotations to perform enhanced inter-procedural null analysis. This feature can be enabled in Preferences > Java > Compiler > Errors/Warnings > Null analysis:

Errors/Warnings preference page with 'Enable annotation-based null analysis' highlighted

You can use either the default annotations bundled with JDT or use your own annotations as null annotations. In addition to enabling the null analysis, the annotations need to be on the Java build path of your project. The default annotations can be found in the org.eclipse.jdt.annotation JAR in the plugins directory, which can be consumed as an OSGi bundle or plain JAR.

When annotation-based null analysis is enabled, JDT issues new diagnostics based on the nullness of a variable or a method return type as specified by a null annotation.

	public void foo(@NonNull Object o1) {¶		System.out.println(o1.toString()); // OK: o1 cannot be null¶	}¶	¶	@NonNull Object bar(@Nullable String s1) {¶		foo(null); // cannot pass null argument¶		@NonNull String s= null; // cannot assign null value ¶		@NonNull String t= s1; // cannot assign potentially null value ¶		return null; // cannot return null value¶	}

Similar diagnostics are also elicited when the value assigned/returned may potentially be null or has unknown nullness.

Overriding methods are also checked for correctness:

class Base {¶	@NonNull Object method(@Nullable Object o) {¶		return new Object();¶	}¶}¶¶class Sub extends Base {¶	@Override            // method parameter must allow null¶	@Nullable Object method(@NonNull Object o) {¶		return new Object();¶	}¶}

You can also use the @NonNullByDefault annotation to set a global default on an element (package, type, method) for all enclosed elements.

@NonNullByDefault¶class MyClass {¶	// redundant @NonNull annotations:¶	public @NonNull Object foo(@NonNull Object o) {¶		return o;¶	}¶	¶	@NonNullByDefault(false) // cancels the default¶	Object bar(@NonNull Object o) {¶		return o;¶	}¶}

As a result of annotating method signatures, the existing intra-procedural null analysis has even more information because of user-specified annotations and thus provides better results. In the example below, the compiler would not have been able to evaluate the null status of parameter 'obj' without the null annotation (option Redundant null check must be enabled to see the warning).

public void foo(@NonNull Object obj) {¶		if (obj == null) { }¶	}

The new diagnostics are classified in three categories:

  • Violation of null specification
    This option produces the¶			'Null type mismatch: required '@NonNull String' but the provided value is specified as @Nullable' warning

    Here, 'in' is not a legal return value because of the @Nullable annotation in its declaration.

  • Conflict between null annotations and null inference
    This option produces the¶			'Null type mismatch: required @NonNull String but the provided value is inferred as @Nullable' warning

    Here, 'local' is not a legal return value, because flow analysis for the un-annotated variable 'local' infers that it can be null on some path.

  • Unchecked conversion from non-annotated type to @NonNull type
    This option produces the¶			'Null type safety: The expression of type String needs unchecked conversion to conform to @NonNull String' warning

    Here, the return value from 'getLegacyValue()' may or may not be compatible to '@NonNull String'. Similar to converting a legacy raw type to a generic type, also here essential type information is missing since 'getLegacyValue()' has no @NonNull or @Nullable annotation.

Note: This is the first version of this feature. We are working on making this easier to consume in Java project setups. The implemented features are complete and tested, but e.g. support for annotating fields or APIs from third-party libraries is currently missing. An initial set of quick fixes is included which will be further improved in the future.

Batch compiler options for using null annotations You can now configure the batch compiler to use annotations for null analysis by passing the token nullAnnot(nullable|nonnull|nonnullbydefault) to the -warn option, where nullable, nonnull and nonnullbydefault are the fully qualified names of the annotation types to be used for null analysis. You can use nullAnnot alone to use JDT's default annotations.

In addition, you can use the -nonNullByDefault token to globally use nonnull as the default when annotation-based null analysis is enabled. This implies that all types in method signatures without a null annotation are considered as specified non-null.

Detection of missing default nullness annotation In order to make non-null the default for un-annotated types in method signatures you may use the '@NonNullByDefault' annotation on all packages. To ensure full coverage of all packages you can configure JDT to detect a missing default nullness annotation. When Preferences > Java > Compiler > Errors/Warnings > Null analysis > Missing '@NonNullByDefault' annotation on package is enabled, JDT will issue an error/warning in the following cases:
  • When a package does not contain an '@NonNullByDefault' annotation in the package-info.java:

    Missing @NonNullByDefault annotation on package warning

  • When a type inside a default package does not contain a default nullness annotation.

    Missing @NonNullByDefault annotation on type warning

Null analysis treats org.​eclipse.​core.​runtime.​Assert like Java assert Since org.eclipse.core.runtime.Assert is often used just like the Java assert keyword, JDT's null analysis considers the boolean expression passed into the former as always true after the Assert reference, since if the condition did turn out to be false, the control flow will never go further than the org.eclipse.core.runtime.Assert reference.

Code snippet showing the boolean expression passed into org.eclipse.core.runtime.Assert.isLegal(..) is considered true after the call

Faster search with pre-built indexes Note: This feature is mainly intended for plug-ins that implement their own classpath container, not for the end user.

You can now tell JDT search to use pre-built indexes instead of having the compiler generate the indexes the first time a JAR is added to the classpath. This prevents indexing from occurring on the user's machine and lets the user obtain first search results faster.

You can specify the index file as a classpath attribute IClasspathAttribute.INDEX_LOCATION_ATTRIBUTE_NAME of the library.

See Help > JDT Plug-in Developer Guide > Programmer's Guide > JDT Core > Indexes for the Java search for more details.

Java Views and Dialogs

Encoding for source attachments You can now specify the encoding for source attachments for libraries e.g. in Build Path > Libraries > expand the library > Source attachment > Edit...

Dialog to specify encoding for source attachments.

Highlighting on Errors/Warnings preference pages On Errors/Warnings preference pages, it is sometimes hard to see which item is selected, especially when you opened the dialog via the "Configure Problem Severity" button.

Configure Problem Severity button in Java Editor problem hover

Now, an arrow highlights the selected item, and another arrow highlights the item under the mouse.

Highlighting in preference pages

The highlighting also works on the PDE and API Tools preference pages.

Properties File Editor

Quick assists to reorganize property keys If you use Eclipse-style externalized strings, the following new quick assists make it easier to reorganize keys inside the Properties File Editor:
  • Rename in workspace - renames the key in the properties file and updates all references
  • Create field in '...' - creates the corresponding field in the resource bundle accessor class
  • Remove property - deletes the property from the properties file and the field from the resource bundle accessor class
  • Remove properties - deletes the selected properties from the properties file and the fields from the resource bundle accessor class

Debug

Export detail formatters Detail formatters (which are e.g. used to render objects in the Variables view) can now be exported as separate preferences. Previously, the only way to share detail formatters was to export all of your workspace settings.

Detail formatters in the export dialog

Show Monitors on by default The default setting for displaying monitor information in the Debug view is now set to on. This change only affects new workspaces and does not change existing settings.

Showing monitor information can be turned on or off using the Java > Show Monitors view action within the Debug view.

Toggle breakpoint modifier keys There is now support for modifier keys while toggling breakpoints in the Java editor:
  • Holding the Ctrl key while double-clicking (toggling) a breakpoint will cause the Properties dialog for that breakpoint to be displayed (on the Mac, use Command)

  • Holding the Alt key while double-clicking (toggling) a breakpoint will enable / disable that breakpoint
If there is no existing breakpoint, a normal breakpoint toggle takes place even if a modifier key is held down.

JUnit

JUnit 4.10 The JUnit 4 version shipped with Eclipse has been updated to 4.10.

Previous     Next