【问题标题】:Getting info from PsiMethod in Intellij IDEA plugin从 Intellij IDEA 插件中的 PsiMethod 获取信息
【发布时间】:2013-11-01 19:34:34
【问题描述】:

我正在为我的项目编写 Intellij IDEA 插件,但我遇到了一个问题 - 我无法从我的代码中获得有关方法 (PsiMethod) 的一些信息。

首先,我想知道这个方法是否公开。 其次,我想获得参数类的完全限定名称。目前我正在这样做:

method.getReturnTypeNoResolve().getInternalCanonicalText()

但它没有提供标准 JVM 类的全名(带有包名),例如 StringList

更新第一个问题用以下代码解决:

PsiUtil.getAccessLevel(method.getModifierList()) == PsiUtil.ACCESS_LEVEL_PUBLIC

但我仍然无法获得完全限定的类名

更新 2这是我的代码的完整列表:

Project currentProject = DataKeys.PROJECT.getData(e.getDataContext());

    PsiClass abstractComponentClass = JavaPsiFacade.getInstance(currentProject).findClass("com.mjolnirr.lib.component.AbstractComponent", GlobalSearchScope.allScope(currentProject));

    TreeClassChooser result = TreeClassChooserFactory
            .getInstance(currentProject)
            .createInheritanceClassChooser("Choose the class to generate manifest",
                    GlobalSearchScope.projectScope(currentProject),
                    abstractComponentClass,
                    false,
                    false,
                    null);
    result.showDialog();

    PsiClass classToGenerate = result.getSelected();

    List<ManifestMethod> methods = new ArrayList<ManifestMethod>();

    for (PsiMethod method : classToGenerate.getAllMethods()) {
        //  If this method is inherited from the Object class we don't need it
        if (isComponentInitialize(method)) {
            continue;
        }

        List<ManifestParameter> parameters = new ArrayList<ManifestParameter>();

        for (PsiParameter param : method.getParameterList().getParameters()) {
            parameters.add(new ManifestParameter(param.getType().getCanonicalText().replaceAll("\\<.*?\\>", "")));
        }

        if (method.getReturnType() != null) {
            ManifestMethod manifestMethod = new ManifestMethod(method.getName(),
                    method.getReturnTypeNoResolve().getInternalCanonicalText().replaceAll("\\<.*?\\>", ""),
                    parameters);

            if (!methods.contains(manifestMethod) && isPublic(method)) {
                System.out.println("->" + method.getReturnType().getCanonicalText());

                methods.add(manifestMethod);
            }
        }
    }

【问题讨论】:

  • 这很奇怪,因为 getInternalCanonicalText()getCanonicalText() 都为我返回 java.lang.String。将 IDEA 12.1.6 和 IDEA IU-129.713 用于插件 JDK。您是指返回类型或参数类型的完全限定名称吗?
  • @vikingsteve 两者。嗯,我12.1.6试试,可能是bug什么的
  • 是的,试试这个,并从 12.1.6 (IDEA IU 129.whatever) 创建一个新的 IDEA JDK
  • @vikingsteve 我忘了问你IDEA的版本——是社区版吗?
  • @vikingsteve 刚刚检查过 - 我有 12.1.6 社区版

标签: java intellij-idea intellij-plugin


【解决方案1】:

已解决 - 我的测试 IDEA 实例连接了错误的 Java SDK。

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2013-03-27
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多