Search code examples
javaeclipsegenericsdiamond-operator

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


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:


Solution

  • 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