18 June 2016

I know Eclipse Neon brings features in the Java editor that I will use almost every day. I have already blogged about Bug 476304. It will be a pleasure to be able to navigate to inner classes correctly.

Today I want to write about the clean-Up to remove redundant type arguments feature. This is something I was looking for at the beginning of 2015.

Since Java 7 you can use the Diamond operator. The code line presented in Listing 1 (Java 6) can be written as in Listing 2 (Java 7).

Listing 1. Declaration and initialization of a List in Java 6
List<String> rows = new ArrayList<String>();
Listing 2. Usage of the diamond notation in Java 7
List<String> rows = new ArrayList<>();

With Eclipse Mars (and probably before) you could already let the compiler warn you about Redundant type arguments. The setting is under Preferences → Java → Compiler → Errors/Warnings in the tree. And in the list: Generic Types → Redundant type arguments (1.7 or higher) as presented in Figure 1.

2016 06 19 compiler settings
Figure 1. Compiler setting for redundant type arguments

With this setting enabled, it was possible to Quick-Fix all the code locations that were not using the diamond operator (see Figure 2).

2016 06 19 quick fix
Figure 2. Quick-fix to remove redundant type arguments

This is a lot of CTRL+1 for an operation that does not require any manual intervention. If the team as decided to use the diamond notation, it should be used everywhere. This belongs to coding guidelines, exactly as code format.

With Eclipse Neon a new clean-up action makes this possible! You can configure Eclipse to remove redundant type arguments when you save your Java files. Open the preference page Java → Editor → Save actions. Check the Checkbox "Additional actions" and click on the Configure…​ button. Switch to the "Unecessary Code" Tab and enable "Remove redundant type arguments (1.7 or higher)"

2016 06 19 preferences
Figure 3. Configure the new clean-up action with Eclipse Neon

Eclipse neon is coming next wednesday. Stay tuned!

Post source on GitHub