How to use structural search to find log.error() within method hierarchy?
I want to find invocations of log.error() called from calculate() method within class hierarchy. I use this template:
class $Class$ extends $Parent$ {
$content$
public boolean calculate() {
$calc_content$;
$log$.error($params$);
$calc_content$;
}
$content$
}
where $content$, $calc_content$ have min occurs: 0, max occurs: unlimited;
$params$ min occurs: 0, max occurs: unlimited;
$log$ min occurs: 0, max occurs: 1, is target of search.
It works fine, except that it doesn't find "log.error()" within "if" and "while" blocks.
How can I search method statements for "log.error()" recursively? (Checking "Recursive matching" option didn't work for me.)
Thanks.
请先登录再写评论。
You need to search for methods first and then search for
$log$.error($params$); in search results
On 26.05.2010 17:56, Dmitry Kandalov wrote:
>
>
>
>
>
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
How can I do it?
I tried the following:
- saved template which finds all calculate() methods. Created second template to find log.error(). For the second template in "Edit Variables - Comlete Match - Contained in constraints" I chose the first template. Didn't work for me.
- the same thing but with first template which finds all statements in all calculate() methods. Didn't work for me.
Find first pattern, in dialog options for second pattern set search scope
On 26.05.2010 21:31, Dmitry Kandalov wrote:
>
>
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
If you mean Scope dropdown box in "Structural Search" dialog, I can't see any templates in there.
In Scope dropdown box in the dialog there is "Previous Search Results" scope
On 26.05.2010 22:23, Dmitry Kandalov wrote:
>
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
Thanks a lot.
This is a bit offtopic but is there a way to do it with single search? May be with groovy script constraints?
(Initially I wanted to put search template into Structural Search Inspection.)
Yes, it is possible (but not user friendly in version 9) like follows:
script constraints should be for template $log$.error($params$); script
constraint for complete match:
x = com.intellij.psi.util.PsiTreeUtil.getParentOfType(__context__,
com.intellij.psi.PsiMethod.class);
if(x == null || !"calculate".equals(x.name)) return false;
if (!com.intellij.psi.util.InheritanceUtil.isCorrectDescendant(x.parent,
x.manager.findClass("my.class.qName"))) return false;
// some check for calculate parameters
return true;
On 27.05.2010 14:47, Dmitry Kandalov wrote:
>
>
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
In case someone tries the above script, x.manager.findClass("my.class.qName") didn't work in IDEA 9.0.2.
Replaced it with
jpf = com.intellij.psi.JavaPsiFacade.getInstance(x.project);
aClass = jpf.findClass("my.class.qName");
Great, thanks for fixing the error!
On 28.05.2010 15:07, Dmitry Kandalov wrote:
>
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"