Skip to main content

Java development tools

JUnit

JUnit 5.7 JUnit 5.7 is here and Eclipse JDT has been updated to use this version.

Java Editor

Completion overwrites in Java editor The Java editor now uses Completion overwrites as the default. If Completion overwrites is on, the completion text replaces the characters following the caret position until the end of the word. If Completion inserts is on, the completion text is inserted at the caret position, so it never overwrites any existing text. Note that pressing Ctrl when applying a completion proposal toggles between the two insertion modes.

You can change the default in the Java > Editor > Content Assist preference page.

Insert best guessed parameters in Java editor Instead of simply inserting the method parameter names as placeholders, when a method is completed, the Java editor now inserts the best guessed parameters by default.

You can change the default in the Java > Editor > Content Assist preference page.

Quick assist to create new implementation Invoking the Quick Assist (Ctrl+1) to create new implementation on an interface or abstract class declaration launches the New Java Class wizard:

Quick fixes on permitted types You can add sealed, non-sealed, or final modifiers on permitted type declarations, as applicable, using the new Quick Fixes (Ctrl+1).

On a permitted class declaration:

On a permitted interface declaration:

Convert to switch expression A new quick assist and clean up has been added that converts switch statements to switch expressions (Java 14 or higher) where possible.

Switch statements that use control statements such as nested switch statements, if/else blocks, for/while loops are not considered as is the case for return/continue statements. All cases of the switch statement must either have a last assignment statement that sets the same variable/field as other cases, or else has a throw statement. Fall-through is allowed between cases but only if there are no other statements in between. The switch statement must have a default case unless the switch expression is an enum type and all possible enum values are represented in the cases.

To apply the quick assist, press Ctrl+1 on the target switch statement and select Convert to switch expression, if offered.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Convert to switch expression check box on the Code Style tab.

Preferences

For the given code:

Before

One gets:

After

Uses the else-if pseudo keyword A new clean up has been added that combines nested if statement in else block to else if.

Beware for any comments after the else keyword. It will be lost.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Combine nested 'if' statement in 'else' block to 'else if' check box on the Code Style tab.

For the given code:

Before

One gets:

After

Bitwise expressions in comparisons A new clean up has been added that replaces the > operator with != when the comparison expression has a bitwise expression operand and a 0 operand.

This resolves an anti-pattern for such kind of comparisons, which can also be a bug when the bitwise expression is involving a negative constant value. This code smell is further described by the FindBugs project as bug description "BIT: Check for sign of bitwise operation".

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog select Compare with != 0 for bitwise expression on the Code Style tab.

Preferences

For the given code:You get this after the clean up:
Before After
Pull up assignment A new clean up has been added that moves assignments inside an if condition above the if node.

It improves the readability of the code.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Pull up assignment check box on the Code Style tab.

For the given code:

Before

One gets:

After

Use switch A new clean up has been added that replaces if/else if/else blocks to use switch when possible.

It converts to switch when there are more than two cases.

It does not convert if the discriminant can be null, that is to say only primitive.

It does a variable conflict analyze.

The case value can be literals or constants.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Convert if/else if/else chain to switch check box on the Code Style tab.

For the given code:

Before

One gets:

After

Add elements in collections without loop A new clean up has been added that uses Collection.addAll() or Collections.addAll() instead of a for loop.

It refactors for loops with index, for loops with iterator and foreach loops.

If the source is an array, the list is raw, and the Java version is 1.5 or higher, we use Arrays.asList() to handle the erasure type. It doesn't decrease the performance.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Add elements in collections without loop check box on the Code Style tab.

Preferences

For the given code:

Before

One gets:

After

Use ternary operator A new clean up has been added that replaces (X && Y) || (!X && Z) by X ? Y : Z.

The operands must be passive and boolean.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Replace (X && Y) || (!X && Z) by X ? Y : Z check box on the Duplicate Code tab.

Preferences

For the given code:

Before

One gets:

After

Use '==' or '^' on booleans A new clean up has been added that replaces (X && !Y) || (!X && Y) by X ^ Y and replaces (X && Y) || (!X && !Y) by X == Y.

It only works on boolean.

It works with lazy or eager operators.

The operands must be passive.

It does not matter an operand is on the left or right.

It does a deep negation expression analyze.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Use '==' or '^' on booleans check box on the Duplicate Code tab.

For the given code:

Before

One gets:

After

Redundant falling through blocks A new clean up has been added that detects a list of statements that ends with a jump statement (return, break, continue or throw), and has the same list of statements below that.

It detects similar statements. It also checks that the declarations of the variables in the statements are the same. It looks for redundant statements in if, else, catch and finally but not in loops.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Remove redundant end of block with jump statement check box on the Duplicate Code tab.

For the given code:

Before

One gets:

After

Redundant if condition A new clean up has been added that removes a condition on an else that is negative to the condition of the previous if.

The condition must be passive. The removed code should not throw an expected exception. The cleanup uses a deep condition comparison algorithm.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Redundant if condition check box on the Duplicate Code tab.

For the given code:

Before

One gets:

After

Use Objects.hash() A new clean up has been added that rewrites Eclipse-autogenerated hashCode() method by Eclipse-autogenerated hashCode() method for Java 7 using Objects.hash().

Let's remind that you can autogenerate your hashCode() and equals() methods by right-clicking on your class, selecting Source and clicking on Generate hashCode() and equals() methods.... Since Eclipse 2018-09, a checkbox allows you to generate your methods using Java 7 API. This cleanup rewrites your method as if it has been generated using this option.

This clean up does not generate again your method from scratch, it rewrites it using a more modern syntax. That is to say, if your method is missing or voluntary does not process a field, this field still won't be processed.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Use Objects.hash() check box on the Unnecessary Code tab.

Preferences

For the given code:

Before

One gets:

After

Use String.join() A new clean up has been added that uses String.join() when possible.

It detects all types of for loops. The delimiter can be added before or after. The condition can be a boolean or an index comparison.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Use String.join() check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Use Arrays.fill() A new clean up has been added that replaces for loops to use Arrays.fill() where possible.

The value must be hard-coded.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Use Arrays.fill() check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Evaluate without null check A new clean up has been added that removes redundant null checks.

It removes null check on value before equals() or equalsIgnoreCase() method and before instanceof expression.

It only removes redundant passive expressions.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Evaluate without null check check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Avoid double negation A new clean up has been added that reduces double negation in boolean expression.

It removes negations on both operands in an equality/difference operation.

It prefers equality/difference operation rather than negated operand.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Double negation check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Redundant comparison statement Removes useless bad value checks before assignments or return statements. Such useless bad value checks are comparing an expression against bad value, then either assigning bad value or the expression depending on the result of the bad value check. It is simpler to directly assign the expression.

The expression should be passive.

The excluded value should be hard coded.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Remove redundant comparison statement check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Unnecessary super() call A new clean up has been added that removes call to super constructor with empty arguments.

Such a call is redundant. See JLS section 12.5 for more info.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Remove redundant super() call in constructor check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Initialize collection at creation A new clean up has been added that replaces the creation of a new Collection, then invoking Collection.addAll() on it, by the creation of the new Collection with the other Collection as parameter.

Only well known collection classes are refactored to avoid behavior changes. The cleanup is enabled only if there is no useful instantiation parameters.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Initialize collection at creation check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Initialize map at creation A new clean up has been added that replaces creating a new Map, then invoking Map.putAll() on it, by creating the new Map with the other Map as parameter.

Only well known map classes are refactored to avoid behavior changes. The cleanup is enabled only if there is no useful instantiation parameters.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Initialize map at creation check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Remove overridden assignment A new clean up has been added that removes passive assignment when the variable is reassigned before being read.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Remove overridden assignment check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Raise embedded if into parent if A new clean up has been added that merges inner if statement into the parent if statement.

The cleanup checks that there is no else statement.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Raise embedded if into parent if check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Redundant return A new clean up has been added that removes useless lone return at the end of a method or lambda.

The cleanup checks that there is no value on the return statement.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Remove useless return check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Redundant continue A new clean up has been added that removes useless lone continue at the end of a loop.

A continue statement at the end of a loop is removed. A continue statement at the end of a control statement is removed if the control statement is at the end of a loop. A continue statement is kept if it has a label.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Remove useless continue check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Use try-with-resource A new clean up has been added that changes code to make use of Java 7 try-with-resources feature. In particular, it removes now useless finally clauses.

It may move an inner closeable assignment as a resource. It handles finally with a simple close() invocation, a null-check and remaining statements below.

It is only enabled from Java 7 and it also handles the Java 9 syntax.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Use try-with-resource check box on the Unnecessary Code tab.

For the given code:

Before

One gets:

After

Exit loop earlier A new clean up has been added that adds a break to avoid passive for loop iterations.

The inner assignments must not do other different assignments after (assign other values or assign into other variables).

There must be no side effects after the first assignments.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Exit loop earlier check box on the Optimization tab.

For the given code:

Before

One gets:

After

Use StringBuilder A new clean up has been added that replaces String concatenation by StringBuilder when possible.

It uses StringBuffer for Java 1.4-.

It only replaces strings on several statements and the concatenation should have more than two pieces.

The variable should be only concatenated and it should retrieve the string once.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Replace String concatenation by StringBuilder check box on the Optimization tab.

For the given code:

Before

One gets:

After

Primitive serialization A new clean up has been added that replaces a primitive boxing to serialize by a call to the static toString() method.

It works for all the primitive types: boolean, char, byte, short, int, long, float and double.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Primitive serialization check box on the Optimization tab.

For the given code:

Before

One gets:

After

Prefer boolean literal A new clean up has been added that replaces Boolean.TRUE/Boolean.FALSE by true/false when used as primitive.

To apply the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog, select Prefer boolean literals check box on the Optimization tab.

Preferences

For the given code:

Before

One gets:

After

Java Views and Dialogs

Fine-grain search for permitted types You can perform a fine-grain search for permitted type declarations in the Search dialog (Ctrl+H) > Java Search > Limit To > Match Locations with the new option:

Sort library entries alphabetically in Package Explorer enabled by default The Preferences > Java > Appearance > [x] Sort library entries alphabetically in Package Explorer is now enabled by default. This makes it easier for you to see if a library is available or not. If you want to see the order in which the libraries are added to the classpath, e.g. to understand classpath loading issues, you can disable the preference.

Java Compiler

Java Formatter

Annotations wrapping The formatter now allows more control over how multiple annotations on a single element should be divided into lines. Previously, they could either all be placed in a single line along with the annotated element, or each in a separate line. The settings that controlled this behavior (in the New Lines > After annotations section) now only control a line break between the last annotation and the annotated element. Line breaks between annotations are controlled by a new group of settings in the Line Wrapping > Wrapping Settings > Annotations section.

Just like with standard wrapping settings, they can be set to keep everything in a single line (Do not wrap), each annotation in a separate line (Wrap all elements), or only break lines that exceed the width limit (Wrap where necessary). The last option along with the Never join already wrapped lines setting effectively means manual control over each case. The annotation wrapping settings differ from other wrapping settings in that the indentation control is not available.

The formatter configuration sections can be found in the Profile Editor (Preferences > Java > Code Style > Formatter > Edit...).

Debug

Support for @argfiles when launching A new check box was added to the Arguments tab for Java based launch configurations (Java Application, JUnit, and others) for writing arguments into an @argfile. This is disabled below Java 9 and can be enabled for Java programs launched with Java 9 and above.

screenshot showing new checkbox

Stabilized logical structures in Variables view with active GC The Debug view no longer breaks when logical structures are shown while the application's garbage collector is active (com.sun.jdi.ObjectCollectedException occurred while retrieving value).

Previous Up Next

Back to the top