Save action in Eclipse IDE to convert the java code to the diamond notation

2.4k Views Asked by At

If you have a java version >= 1.7 you can use the diamond notation.

Instead of:

List<String> rows = new ArrayList<String>();

You can write:

List<String> rows = new ArrayList<>();

With Eclipse Mars-M4, I have errors when I use the first construct. (Maybe this was already the case with Eclipse Luna version. I did not check):

Java Code

This is due to this configuration:

Preferences > Java > Compiler > Errors/Warnings in the tree. In the list: Generic Types > Redundant type arguments (1.7 or higher).

Errors/Warnings preference page

This is set to ERROR. I can of course configure it to IGNORE to remove the error.

I am searching for another solution. Eclipse has the notion of “Additional actions”. I am looking for an action that would rewrite the code to the diamond notation.

Does this action already exist? How can I configure it?

Related question:

3

There are 3 best solutions below

0
On BEST ANSWER

With Eclipse Neon, just select "Remove redundant type arguments (1.7 or higher)" under the "Unnecessary Code" tab in the "Additional Save Actions" windows (or in the "Edit Clean-up Profile" windows).

Additional Save Actions windows

Read more in this blog post: Eclipse Neon: diamond notation as clean-up action

1
On

It looks like there's a ticket for this targeting eclipse 4.6 https://bugs.eclipse.org/bugs/show_bug.cgi?id=434788

0
On

Unfortunately this is not possible out of the box. You would need to write your own plugin and implement the ICleanUp interface.

If you wish to create your own plugin, details on how to create a cleanup and save action can be found here: Contributing a clean up and a save action using the clean up extension point