本文所有示例代码均假设我们的.java文件是如下内容

public class MyClass{

    /**
     * 想睡HR不过分吧?
     **/
    public String m1(String p1,String p2) {
        return "方法M1";
    }
}

我们在写代码之前还要做一些其他工作,由于IntelliJ平台的变化,导致不在默认加载PsiMethodImplPsiFieldImpl两个类所在的jar包,而我们恰好要用到这两个类,所以需要手动依赖这两个类,我们在plugin.xml增加如下依赖,官网说明在这个地址

// 下面这1行是项目创建的时候就自带的
<depends>com.intellij.modules.platform</depends>
// 下面这2行是我们刚刚添加的,我们需要的两个类就在下面的jar包下
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.java</depends>

下面的代码会演示:鼠标光标在方法上,打印方法m1的相关信息

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.source.PsiFieldImpl;
import com.intellij.psi.impl.source.PsiMethodImpl;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.keyFMap.KeyFMap;
import org.jetbrains.annotations.NotNull;

public class MyAction extends AnAction {
	@Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        PsiElement psiElement = e.getData(PlatformDataKeys.PSI_ELEMENT);
        if (psiElement != null) {
            PsiManager manager = psiElement.getManager();
            IElementType elementType = psiElement.getNode().getElementType();
            // 如果光标所在位置是一个Java方法
            if ("METHOD".equals(elementType.toString())) {
                PsiMethodImpl m = (PsiMethodImpl) psiElement;
                // 控制台输出m1
                System.out.println(m.getName());
                // 获取当前方法所在类信息
                PsiClass psiClass = m.getContainingClass();
                PsiMethod[] methods = psiClass.findMethodsByName("m1", true);
                for (PsiMethod method : methods) {
		            // 方法名,本示例打印m1
		            System.out.println(method.getName());
		            // 该方法是否有参数,本示例打印true
		            System.out.println(method.hasParameters());
		            PsiParameterList parameterInfo = method.getParameterList();
		            PsiParameter[] parameters = parameterInfo.getParameters();
		            // 挨个参数打印,本示例打印p1,p2
		            for (PsiParameter parameter : parameters) {
		                System.out.println(parameter.getName());
		            }
		        }      
            }
        }
    }
}