Eclipse Zone is brought to you in partnership with:

Having worked with a number of languages and tools over the years, I've learned one thing: work with the tools you have, learn them, master them, then make life easier by sharing the knowledge. Whether Java, C++, VB, or COBOL, I've always applied the same philosophy with success. I love studying manuals and being a keyboard addict, the list of keyboard shortcuts is often my favorite page. Byron is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. View Full User Profile

Get Warnings About Unused Method Arguments From Eclipse to Keep Code Clean

04.27.2010
| 8753 views |
  • submit to reddit

Unused variables and methods should alway be unwelcome. Removing them keeps the code cleaner and easier to read. Now, by default Eclipse warns you about unused private variables and methods, but it doesn’t warn you (by default) about unused method arguments.

But there is a compiler setting in Eclipse that can warn you when you don’t use an argument in a method. You can even handle arguments on inherited methods, especially useful when using 3rd party libraries.

Setup the compiler preferences

To get warnings of unused arguments:

  1. Go to Window > Preferences > Java > Compiler > Errors/Warnings.
  2. Open up the section Unnecessary Code.
  3. Change the setting for Parameter is never read from Ignore to Warning.
  4. (Recommended, but optional) Deselect Ignore overriding and implementing methods. I recommend deselecting this option. You can leave it selected if you want to, but the feature loses a bit of its usefulness. I discuss this option a bit more in the next section.

Here’s what the preference should look like:

And here’s an example of such a warning. In the example, the argument capacity isn’t used so is annotated as a warning.

What’s the easiest way to get rid of the warning? Well, just remove the argument using the Eclipse Change Method Signature refactoring. This will remove the argument from the method declaration and any method callers in one go. However, if you can’t remove the argument from the method then read the next section.

On an old codebase you may get lots of warnings initially. I’d normally handle these on a class-by-class basis only for the classes I’m currently working on, unless the team has a specific cleanup project/task. Just something to bear in mind, especially if you share workspace settings across the team via SVN or similar.

What about method signatures that can’t be changed?

When you deselected Ignore overriding and implementing methods, you told Eclipse to warn you about unused arguments in implemented/overridden methods. It’s a good indicator that your superclass/interface method is passing in more than it needs to (ie. remove the argument from the method signature) or that you’re ignoring something important about the interface contract (ie. use it somewhere in your method).

But sometimes you can’t remove the argument because you don’t have control over inherited methods, especially if you implement/override a 3rd party library’s method or if the method is part of a bigger framework that’s difficult to change. Other times you won’t have any need for the argument in very isolated cases. So you don’t want a warning to appear for these.

That’s why you can use the @SuppressWarning compiler annotation to stop Eclipse from reporting the warning. Here’s an example:

public String process(@SuppressWarnings("unused") int capacity, int max) {...}
You can apply SuppressWarning to an individual argument or the whole method. I’d recommend annotating only the argument that you want to ignore.

Eclipse makes it easy to add the compiler annotation. Just navigate to the warning, press Ctrl+1 and choose Add @SuppressWarning ‘unused’ to ‘variable’ (Quick Fix does its job again).

From http://eclipseone.wordpress.com/2010/04/27/get-warnings-about-unused-method-arguments-from-eclipse/

Published at DZone with permission of Byron M, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Clure Rogre replied on Wed, 2010/04/28 - 4:54am

"but it doesn’t warn you (by default) about unused method arguments."

The reason for this, as in your example of "process" - "process" could be overridden in a sub-class that does need and use the "capacity" argument, and therefore, removing it would have serious consequences. Especially by an automatic process.

If in your example the method "process" was private or final - or the class was final - then it would be fine to remove. Otherwise, with a non-final public method, the unused argument could be perfectly legitimate.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.