PSI Help: looking for usages and overrides of a method

Hello folks, I need some advice about PSI:

I'm writing a LocalInspectionTool to cover some weird business
requirements for the project I'm currently working in. There's a
requirement that all interface methods that match a certain criteria
should use an specific class as the first parameter.

My inspection currently looks for interfaces that match the criteria, and
highlight it's methods as errors if they don't take the correct
parameters. Now I want to provide a LocalQuickFix that:

1) Add the parameter to the method
2) Add the parameter to all implementing methodns
3) Add the parameter to all calls to that method


(1) is actually easy to do, but I'm having trouble with (2) and (3). I've
taken a look at PsiSearchHelper, but the lack of documentation makes
things hard to do. I've tried to invoke findReferences() and
findReferencesIncludingOverriding(), but they don't seem to return what I
want. Any hints on how to do this?

--
Marcus Brito <pazu@animegaiden.com.br>


6 comments
Comment actions Permalink

Marcus Brito wrote:

Hello folks, I need some advice about PSI:

I'm writing a LocalInspectionTool to cover some weird business
requirements for the project I'm currently working in. There's a
requirement that all interface methods that match a certain criteria
should use an specific class as the first parameter.

My inspection currently looks for interfaces that match the criteria, and
highlight it's methods as errors if they don't take the correct
parameters. Now I want to provide a LocalQuickFix that:

1) Add the parameter to the method
2) Add the parameter to all implementing methodns
3) Add the parameter to all calls to that method


(1) is actually easy to do, but I'm having trouble with (2) and (3). I've
taken a look at PsiSearchHelper, but the lack of documentation makes
things hard to do. I've tried to invoke findReferences() and
findReferencesIncludingOverriding(), but they don't seem to return what I
want. Any hints on how to do this?


findReferencesIncludingOverriding() will return all method calls even if referencing directly overriding method instead
of base one (your (3) item). (2) may be acheived with PsiSearchHelper findOverridingMethods()

--
Maxim Shafirov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Em Thu, 20 May 2004 16:03:40 +0400, Maxim Shafirov (JetBrains) escreveu:

findReferencesIncludingOverriding() will return all method calls even if
referencing directly overriding method instead of base one (your (3)
item). (2) may be acheived with PsiSearchHelper findOverridingMethods()


Since I'm trying to find references to an interface method, do I need to
use *IncludingOverriding to find calls to implementing methods or
findReferences() should already return them? By the way, what's the third
parameter to findReferencesIncludingOverriding for?

--
Marcus Brito <pazu@animegaiden.com.br>


0
Comment actions Permalink

Em Thu, 20 May 2004 09:07:08 +0000, Marcus Brito escreveu:

Em Thu, 20 May 2004 16:03:40 +0400, Maxim Shafirov (JetBrains) escreveu:

>> findReferencesIncludingOverriding() will return all method calls even if
>> referencing directly overriding method instead of base one (your (3)
>> item). (2) may be acheived with PsiSearchHelper findOverridingMethods()

Ok, findReferencesIncludingOverriding() is finding PsiReferences to the
method calls, that's great. Now I'm stuck: how can I resolve this
reference to a PsiMethodCallExpression so I can modify it?

--
Marcus Brito <pazu@animegaiden.com.br>


0
Comment actions Permalink

Em Thu, 20 May 2004 08:56:15 +0000, Marcus Brito escreveu:

1) Add the parameter to the method
2) Add the parameter to all implementing methodns 3) Add the parameter to
all calls to that method


In other words, I want to do exactly what the "Change Method Signature"
refactoring does. Any code on how to do it? I've been dabbling with it
this afternoon, with moderate degree of success, but I'd love to get some
example code (and unfortunately there's nothing like this on
InspectionGadgets).

--
Marcus Brito <pazu@animegaiden.com.br>


0
Comment actions Permalink

Read you mail, I sent you some suggestion may be it helps may be not but in my case it helps partially anyway.

TIA,
Dmitry

0
Comment actions Permalink

Marcus Brito wrote:

Em Thu, 20 May 2004 09:07:08 +0000, Marcus Brito escreveu:

>> Em Thu, 20 May 2004 16:03:40 +0400, Maxim Shafirov (JetBrains) escreveu:
>>
>>> findReferencesIncludingOverriding() will return all method calls even if
>>> referencing directly overriding method instead of base one (your (3)
>>> item). (2) may be acheived with PsiSearchHelper findOverridingMethods()


Ok, findReferencesIncludingOverriding() is finding PsiReferences to the
method calls, that's great. Now I'm stuck: how can I resolve this
reference to a PsiMethodCallExpression so I can modify it?


Obtain the element PsiReference is associated with be calling
PsiReference.getElement(). You should then analyze its parent
and process those that has PsiMethodCallExpression as parent.

Friendly,
Dmitry
--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

Please sign in to leave a comment.