简体   繁体   中英

Automatically remove unused parameters from constructor (Eclipse, Java)

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.

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 .

在此处输入图片说明

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.

在此处输入图片说明

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM