Automatically remove unused parameters from constructor (Eclipse, Java)

2.4k views Asked by At

I have a bunch of classes which contain constructors with unused parameters. For example this one:

class Book {

    String author;
    String title;

    public Book(String author, String title, int numberOfPages) {       
        this.author = author;
        this.title = title;
    }
}

Does Eclipse offer any way of automatically removing these unused parameters over multiple files? Removing them manually would take hours.

2

There are 2 answers

0
Konstantin Yovkov On BEST ANSWER

On Eclipse Juno, you can set the compiler to show a warning/error when there's an unused parameter.

  1. Open Window -> Preferences
  2. Navigate to Java -> Compiler -> Errors/Warnings
  3. Find the node called Unnecessary code
  4. Set the Value of parameter is not used to Warning or (better) Error.

enter image description here

This way the compiler will show a warning or will even fail to compile if there are unused method/constructor parameters.

In this case, when you navigate to the Problems view, you can apply a Quick Fix with a mouse right-click.

enter image description here

0
ashokramcse On

Go to Windows->Preference->Java->Compiler->Errors/Warnings

In that Unnecessary Code tab. Make Value of Parameter is not used as Error from Ignore or Warning.