Comparing two PsiMethods

I want to check if two PsiMethods I have are the same (name and signature) or not.
I can do a psimethod1.getName().equals(psimethod2.getName()) for checking the name.
For getting the signature , psiMethod.getSignature(PsiSubstitutor) requires a PsiSubstitutor. I have no control over the signatures of the PsiMethods to be compared so what kind of PsiSubstitutor should I pass.
How to compare the two MethodSignatures returned?
Also is there any better way to achieve this comparison?

7 comments
Comment actions Permalink

There's com.intellij.psi.util.MethodSignatureUtil#areSignaturesEqual()

0
Comment actions Permalink

For that I need to pass the MethodSignature and to get MethodSignature from PsiMethod I need to pass the PsiSubstitutor.

Passing the Substitutor as EMPTY or UNKNOWN works for methods with normal parameters but if we have a Generic type argument they do not work.

0
Comment actions Permalink

Does com.intellij.psi.util.MethodSignatureUtil#areSignaturesEqual(com.intellij.psi.PsiMethod, com.intellij.psi.PsiMethod) not work?

0
Comment actions Permalink

That method internally takes the PsiSubstitutor as Empty which works otherwise but not when the methods have a generic type argument.

0
Comment actions Permalink

Please provide the context, why do you need to compare 2 methods. I don't get why do you want to consider different method foo(T) from 2 different parameterisations  of the containing class. 

The correct way is as Yann pointed - MethodSignatureUtil.

 

Anna

0
Comment actions Permalink

I want to check the presence of a method in a file before adding it, hence the comparison.

0
Comment actions Permalink

How do know what method to add? Where do you get the correct signature?

0

Please sign in to leave a comment.