Using eclipse find and replace all to swap arguments

后端 未结 5 568
夕颜
夕颜 2021-02-07 20:46

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 f

相关标签:
5条回答
  • 2021-02-07 20:49

    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)/
    
    0 讨论(0)
  • 2021-02-07 20:54

    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

    0 讨论(0)
  • 2021-02-07 21:03

    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.

    0 讨论(0)
  • 2021-02-07 21:04

    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 );".

    0 讨论(0)
  • 2021-02-07 21:07

    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
    

    enter image description here

    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.

    0 讨论(0)
提交回复
热议问题