Using eclipse find and replace all to swap arguments
I have about 100 lines that look like the below:
assertEquals(results.get(0).getID(),1);
They all start with assertEquals and contain two arguments. Im looking for a way to use find and repl开发者_StackOverflow社区ace all to swap the arguments of all these lines.
Thanks
use the following regexp to find:
assertEquals\((.*),(.*)\);
and this replacement value:
assertEquals(\2,\1);
The regexp means "assertEquals( followed by a first group of chars followed by a comma followed by a second group of chars followed by );".
The replacement value means "assertEquals( followed by the second group of chars found followed by a comma followed by the first group of chars found followed by );".
I don't know how to do it in Eclipse, but if you happen to also have a vim
installed you might load your file in it and do
:%s/\(assertEquals(\)\(.*\),\(.*\))/\1\3,\2)/
If you find yourself swapping parameter order in method declarations farily often, I found a plugin that does it for you with a single click.
This plug-in adds two toolbar buttons to the Eclipse Java editor:
Swap backward Swap forward
With the caret at | in:
void process(int age, String |name, boolean member) {...}
clicking the Swap forward button yields:
void process(int age, boolean member, String |name) {...}
or clicking Swap backward button with the original source yields:
void process(String |name, int age, boolean member) {...}
Here is the article discussing it.
Here is the jar to drop into your eclipse plugin directory.
You can also use Eclipse's built-in method signature refactoring to re-order the arguments.
In the case of converting from JUnit to TestNG (which is what it looks like you're doing), you can copy org.testng.Assert into your project and refactor the assertXYZ methods to transpose the expected/actual arguments.
When you're done, delete your copy of org.testng.Assert
search for
assertEquals\((.*),\s*(.*)\);
and replace with
assertEquals(\2, \1);
Sorry for the more or less superflous answer. But it does work better for me and I think for others as well. My edit of the first post was rejected.
精彩评论