Frage

I have a lot of reformatting to do in a old Java project.

I have over than 300 different classes created by factories and I would like to automatize changes with a plugin for Eclipse or with a tool.

I need to remove the same parameter for the 300 classes (as Foo class above) and to change mother class name every time.

example:

public Foo extends **OldClass** {

    public Foo(A a, **B b**){
        //..
    }

}

to

public Foo extends newClass {

    public Foo(A a){
        //..
    }

}

I would also like to be able to "batch find and replace" a lot of declarations. I mean, I would like to have a tool with a list of replacements (with regex patterns) and just to replace everything in one click.

Do you know how I could do this? I am on a Windows 7 system.

War es hilfreich?

Lösung

You can use Eclipse's Search dialog (Ctrl+H) to do this. Next to the 'Search' button there is a 'Replace' button. This can be used to find every occurrence across multiple files (specifying for example, every filename matching *.java), and replace them in a single user action.

eclipse search dialog with replace option

Once you have configured your search criteria (e.g. " extends OldClass") and search for that, you will be presented with a dialog to enter a replacement value (e.g. " extends NewClass"). Upon pressing 'OK' for that dialog, it will do the search and replace.

replace dialog

This also allows regular expressions, both in the search and replace (I'm assuming this would allow using regex group captures, but I haven't tested it). It should be possible to apply the same principle to the parameter you also want to remove.

Note that it gets trickier if you only want to modify classes where both extends OldClass AND , B b) appear, and they can occur independently.

Andere Tipps

If the current code is not too complex, you can get away with sed, e.g.,

find . -type f -iname '*.txt' -exec sed -i 's,this,that,g' {} +
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top