Java™ 16 Support |
|
Java 16 |
Java 16 is out and Eclipse JDT supports Java 16 in 4.19 via
Marketplace.
The release notably includes the following Java 16 features:
Please note that preview option should be on for preview language features. For an informal introduction of the support, please refer to Java 16 Examples wiki. |
JUnit |
|
JUnit 5.7.1 | JUnit 5.7.1 is here and Eclipse JDT has been updated to use this version. |
Java Editor |
|
Quick assist to create try-with-resources |
For expressions returning a type that is AutoCloseable there's a new quick assist (Ctrl+1) available: Assign to new local variable in try-with-resources.
It creates a new try-with-resources block with the expression assigned to a resource variable. The variable type and name can be selected from a few suggestions: The default hotkey sequence for this quick assist is Ctrl+2 followed by T. |
Add catch clause to try-with-resources assists |
There are multiple assists to surround auto-closeable statements in a try-with-resources statement including Surround with > Try-with-resources Block. Now, all forms will add
a catch clause for any exceptions (such as IOException ) thrown by the auto-close if not already handled via an existing catch clause
or throws directive. In the case where the existing code catches or throws an exception that sub-classes the exceptions of the new catch clause, an
additional catch clause will be added to rethrow the exception to ensure code logic remains consistent.
|
Quick fix to create permitted type declaration |
You can use the following quick fixes (Ctrl+1) to create a new permitted class or interface declaration:
The created type will declare the |
Java Feature clean ups |
A new tab named Java Feature has been added to the Clean Up preferences. It lists the clean up options that introduce the use of language features from different Java versions. Relevant clean up options from other tabs have also been moved to this new tab.
You can use these clean ups while upgrading the Java version in your code. |
Pattern matching for instanceof clean up |
A new clean up has been added that uses pattern matching for the instanceof operator when possible.
It is only applicable for Java 15 or higher when preview features are enabled. To apply the clean up, select Pattern matching for instanceof check box on the Java Feature tab in your clean up profile. For the given code: One gets: |
Reduce indentation clean up |
A new clean up has been added that removes useless indentation when the opposite workflow falls through.
When several blocks fall through, it reduces the block with the greatest indentation.
It can negate an To apply the clean up, select Reduce indentation when possible check box on the Code Style tab in your clean up profile. For the given code: One gets: |
Extract increment clean up |
A new clean up has been added that moves increment or decrement outside an expression.
A prefix increment/decrement ( But let's look at this code: int i = j++; Most of the developers hardly remember which from the increment or the assignment comes first. One way to make the code obvious is to write the increment/decrement in a dedicated statement: int i = j; j++; And so for the prefix expressions: int i = ++j; ...it goes like this: j++; int i = j; The cleanup moves a prefix expression above the statement and a postfix expression below. It does not move increments from loop condition and it does not cleanup several increments in the same statement. The increment/decrement is always rewritten as a postfix expression for standardization. To apply the clean up, select Extract increment/decrement from statement check box on the Code Style tab in your clean up profile. For the given code: One gets: |
Use Comparator.comparing() clean up |
A new clean up has been added that replaces a plain comparator instance by a lambda expression passed to a Comparator.comparing() method.
The feature is enabled only with Java 8 or higher.
The To apply the clean up, select Use Comparator.comparing() check box on the Java Feature tab in your clean up profile. For the given code: One gets: |
Multi-catch clean up |
A new clean up has been added that converts catch clauses with same body to Java 7's multi-catch.
The feature is enabled only with Java 7 or higher. To apply the clean up, select Use Multi-catch check box on the Java Feature tab in your clean up profile. For the given code: One gets: |
Convert fields into local variables |
A new clean up has been added that refactors a field into a local variable if its use is only local.
The previous value should not be read. The field should be To apply the clean up, select Convert fields into local variables if the use is only local check box on the Optimization tab (the Performance tab in Eclipse 2021-09) in your clean up profile. For the given code: One gets: |
Static inner class clean up |
A new clean up has been added that makes inner class static if it doesn't use top level class members.
To apply the clean up, select Make inner classes static where possible check box on the Optimization tab (the Performance tab in Eclipse 2021-09) in your clean up profile. For the given code: One gets: |
Use String.replace() clean up |
A new clean up has been added that replaces String.replaceAll() by String.replace() when the pattern is a plain text.
The pattern must be constant. To apply the clean up, select Use String.replace() instead of String.replaceAll() when no regex used check box on the Optimization tab (the Performance tab in Eclipse 2021-09) in your clean up profile. For the given code: One gets: |
Primitive comparison clean up |
A new clean up has been added that replaces the compareTo() method by a comparison on primitive.
It improves the space and time performance. The compared value must be a primitive. To apply the clean up, select Primitive comparison check box on the Optimization tab (the Performance tab in Eclipse 2021-09) in your clean up profile. For the given code: One gets: |
Primitive parsing clean up |
A new clean up has been added that avoids to create primitive wrapper when parsing a string.
The object should be used as a primitive and not as a wrapper. To apply the clean up, select Primitive parsing check box on the Optimization tab (the Performance tab in Eclipse 2021-09) in your clean up profile. For the given code: One gets: |
Pull down common code from if/else statement clean up |
A new clean up has been added that extracts common code from the end of an if / else if / else control flow.
Ultimately it removes the empty and passive
The control flow should have an
The statement matching performs a deep analysis. All the blocks should end with the same set of statements, or the blocks with different code should fall through with a jump statement ( To apply the clean up, select Pull down common code from if/else statement check box on the Duplicate code tab in your clean up profile. For the given code: One gets: And for the given code where all tails of blocks are identical except one block which falls through: The identical tails of blocks have been pulled down from the control flow and the falling through block has been left as it is: |
String.substring() clean up |
A new clean up has been added that removes the second substring() parameter if this parameter is the length of the string. It's the default value.
It must reference the same expression. The expression must be passive. To apply the clean up, select Redundant String.substring() parameter check box on the Unnecessary code tab in your clean up profile. For the given code: One gets: |
Unreachable block clean up |
A new clean up has been added that detects two if conditions that are identical and removes the second one.
The conditions should be passive. No exceptions should be awaited.
It doesn't create unreachable code below the To apply the clean up, select Unreachable block check box on the Unnecessary code tab in your clean up profile. For the given code: One gets: |
Unlooped while clean up |
A new clean up has been added that replaces a while loop that always terminates during the first iteration by an if .
The loop should not contain any
The loop should only contain To apply the clean up, select Convert loop into if when possible check box on the Unnecessary code tab in your clean up profile. For the given code: One gets: |
Source Fixing clean ups |
A new tab named Source Fixing has been added to the Clean Up preferences. It lists the clean up options that fixes the behavior of the code. The Compare with != 0 for bitwise expression clean up option from Code style tab have also been moved to this new tab.
⚠️ Use it carefully. You may get an unexpected behavior. It may trigger zombie code. A zombie code is a dead code that is dead because an error occurs before. The day someone fixes the error, the zombie code comes back to life and alters the behavior. Although most of the cleanups need review, those ones need testing. |
Object.equals() on non null clean up |
A new clean up has been added that inverts calls to Object.equals(Object) and String.equalsIgnoreCase(String) to avoid useless null pointer exception.
The caller must be nullable. The parameter must not be nullable. Beware! By avoiding null pointer exceptions, the behavior may change! To apply the clean up, select Avoid Object.equals() or String.equalsIgnoreCase() on null objects check box on the Source Fixing tab in your clean up profile. For the given code: One gets: |
Comparison to zero clean up |
A new clean up has been added that fixes Comparable.compareTo() usage.
The code is not supposed to predict the Beware! The behavior may change if you implement a custom comparator! To apply the clean up, select Compare to zero check box on the Source Fixing tab in your clean up profile. For the given code: One gets: |
Java Views and Dialogs |
|
Parallel index search |
A new preference option has been added and enabled by default:
Preferences > Java > Enable parallel index search.
Depending on the available hardware, this option should improve performance
for all index based Java search operations, but could also lead to possible regressions.
To switch back to the old sequential index search, turn this option off:
|
Coloring restricted identifiers |
A new option named Restricted identifiers has been added under Java category in Java > Editor > Syntax Coloring preferences.
Some identifiers (e.g. var, yield, record etc.) are restricted identifiers because they are not allowed in some contexts. Semantic highlighting options for such identifiers can be controlled by the element Restricted identifiers under Java category in Java > Editor > Syntax Coloring preference page. |
Externally annotate sources |
The concept of external null annotations has been extended to apply to source folders, too.
External annotations were introduced in Eclipse 4.5 in order to overlay not-editable library classes with null annotations to specify the null contract against which library calls should be analysed. You can now apply the same concept for another kind of classes that should not be edited: generated source code. In the Java Build Path dialog, also source folders now have a node External annotations where a path to Eclipse External Annotation files (.eea) can be configured. Given a project that is configured for annotation based null analysis, and given a Java class inside a source folder configured for external annotations, the editor now offers a quick assist (Ctrl+1) for annotating individual type references in the signatures of methods and fields.
The selected option will record that the return type should be interpreted as |
static import org.mockito.Mockito.* available as favorite |
Imports for static org.mockito.Mockito.* are added to the Java favorites in the preferences under Java > Editor > Content Assists > Favorites.
This way the organize imports action in the IDE will automatically add static imports to this class when you use the Mockito library in your tests.
|
Debug |
|
Toggle tracepoints in editor ruler |
A new Toggle Tracepoint context-menu entry has been added to the Java Editor line ruler.
Both the Toggle Tracepoint options i.e. the new context-menu entry and the existing option under Run menu have a new icon
and are now available for Java class files also along with Java source files.
|
Toggle breakpoint on a list of methods including abstract method |
You can now Toggle Method Breakpoint on a list of methods which includes an abstract method.
|