Search code examples
intellij-ideaintellij-plugin

How do i get the PsiMethodCallExpression object type?


How do I get the object type that a PsiMethodCallExpression refers to?

eg

Object x = new Object; x.hashCode();

I can use the visitor and get the PsiMethodCallExpression, and I can get "hashCode", but how do I get "Object"?


Solution

  • As you can learn from the javadocs to the APIs in question, you can obtain the method being called by calling PsiMethodCallExpression.resolveMethod(), and after that you can obtain the class where the method is declared by calling PsiMethod.getContainingClass().

    Edit - just added some code to make it obvious for all and sundry, use the "accept" method with the following:

    public void visitMethodCallExpression(PsiMethodCallExpression expression) {
           super.visitCallExpression(expression);
           PsiUtil.getMemberQualifiedName(expression.resolveMethod());
           expression.resolveMethod().getContainingClass().getName();
           expression.resolveMethod().getContainingClass().getQualifiedName();
    }