Problem using structural replace
Hi,
I am trying to add a line to the end of the tearDown() method that appears in each of my 344 unit test classes. The method appears in each test class and in some cases it is empty, in others it contains code (different in each class). When I do a structural search using the following template, I find all the instances I expect.
Search template is:
class $Class$ {
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) {
$StatementList$;
}
}
Variables left as defaults except:
$MethodName$ text=tearDown min=1 max=1
$StatementList$ min=0 max=unlimited
When I use the same template to do a structural replace, rather than locating the method as it does with the search, it locates the comment at the start of the class definition. Then doing a Preview Replacement shows that doing a replace will replace the whole class such that it just contains the tearDown method and nothing else. (The new tearDown methods do include the line that I wished to insert, but all the other class methods and variables are removed.)
Replacement template:
class $Class$ {
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) {
$StatementList$;
TestHelper.newMethod();
}
}
Does anyone know what am I doing wrong? How can I achieve what I want i.e. insert a line at the end of each occurence of a method that appears in over 300 classes, and is different in each one.
Thanks,
Dan.
Please sign in to leave a comment.
Hi,
You need to add variable that will match remaining class content (e.g.
$RemainingContent$ 0 .. unlimited).
class $Class$ {
$RemainingContent$
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) {
$StatementList$;
}
}
and reference it in replacement pattern
class $Class$ {
$RemainingContent$
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) {
$StatementList$;
TestHelper.newMethod();
}
}
This is needed since replacement pattern is treated literally
Dan Murray wrote:
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
Hi - thank you very much for the very quick reply. That has cleared things up a lot, but I still have a problem.
-
Scenario 1
$RemainingContent$ 0 to unlimited
Search template:
class $Class$ {
$RemainingContent$
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) $Throws$ {
$StatementList$;
}
}
Replace template:
class $Class$ {
$RemainingContent$
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) $Throws$ {
$StatementList$;
TestHelper.newMethod();
}
}
Using these templates the teardown method gets moved to the very bottom of the class file.
-
Scenario 2
$RemainingContent$ 0 to unlimited
$EndContent$ 0 to unlimited
Search template:
class $Class$ {
$RemainingContent$
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) $Throws$ {
$StatementList$;
}
$EndContent$
}
Using these templates the teardown method gets moved to the very top of the class definition, above the class variables.
I need it to stay in the same position it was (halfway down the class definition).
Thanks again,
Dan.
Hi,
Right now there is no way to save method order so let's file JIRA item
and have it available.
Dan Murray wrote:
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
Thanks for the prompt correspondence. We would find it a very useful feature if we could preserve method order.
Meanwhile I will have to get my copy and paste finger ready for action...
Thanks,
Dan.