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.

9 comments
Comment actions Permalink

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:

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.

>

---
Original message URL: http://www.jetbrains.net/devnet/message/5264019#5264019



--
Best regards,
   Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

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.

0
Comment actions Permalink

Find first pattern, in dialog options for second pattern set search scope

On 26.05.2010 21:31, Dmitry Kandalov wrote:

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.

>

---
Original message URL: http://www.jetbrains.net/devnet/message/5264040#5264040



--
Best regards,
   Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

If you mean Scope dropdown box in "Structural Search" dialog, I can't see any templates in there.

0
Comment actions Permalink

In Scope dropdown box in the dialog there is "Previous Search Results" scope

On 26.05.2010 22:23, Dmitry Kandalov wrote:

If you mean Scope dropdown box in "Structural Search" dialog, I can't see any templates in there.

>

---
Original message URL: http://www.jetbrains.net/devnet/message/5264045#5264045



--
Best regards,
   Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

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

0
Comment actions Permalink

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:

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?

>

---
Original message URL: http://www.jetbrains.net/devnet/message/5264112#5264112



--
Best regards,
   Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

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

0
Comment actions Permalink

Great, thanks for fixing the error!

On 28.05.2010 15:07, Dmitry Kandalov wrote:

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

>

---
Original message URL: http://www.jetbrains.net/devnet/message/5264247#5264247



--
Best regards,
   Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Please sign in to leave a comment.