【问题标题】:How can I perform the searches Java IDEs do for method references programmatically? [closed]如何以编程方式执行 Java IDE 对方法引用的搜索? [关闭]
【发布时间】:2013-07-01 14:06:05
【问题描述】:

您知道 Eclipse 的查找所有引用功能(搜索 > 引用 > 工作区或 Ctrl-Shift-G)吗?如何以编程方式运行它?

我有一个庞大的代码库,我需要对其安全违规进行审计,并且需要链接大约十几个条件。

是否有可以在 Java 中分析大型项目(我有 1GB 的源文件)的库?以下是我需要回答的问题:

  • 传入接口列表,查找这些接口的所有实现
  • 在该列表中搜索我们的安全库的调用
  • 搜索上面列表中引用的每个方法,以确认进行了正确的授权库调用。

我可以在一天内手动完成这项工作,但宁愿花 2 时间编写一个漂亮的脚本来为我完成这项工作。

我可以使用哪些库来编写 Eclipse 出色完成的这些常见任务的脚本?理想情况下,我想在命令行中执行它们,以便它们可以重复和编写脚本,但显然会采用我能得到的。

【问题讨论】:

标签: java eclipse intellij-idea intellij-plugin static-code-analysis


【解决方案1】:

您可以在 IntelliJ IDEA 中编写自己的插件来完成此操作。

以编程方式,您可以利用 PSI(程序结构接口)树来搜索各种代码元素(类、变量、方法、语句等),在您的代码库中导航,然后执行任何操作你需要做的。

以下是一些参考资料:

  1. IntelliJ IDEA Plugin Development

  2. PSI Cookbook

  3. PsiViewer Plugin

您需要做的所有事情(查找实现、搜索调用、搜索特定语句的方法)都是可能的。

如果您下载 IntelliJ IDEA 社区版的源代码,您可以查看代码并了解这些操作是如何完成的。

或者您可以在此处发布具体问题(标记为intellij-plugin),CrazyCoder 之类的人可能会很乐意帮助您:)

关于代码库的范围(您说 1 GB?),您只需要尝试一下,看看效果如何。最坏的情况,你可以分部分分析你的代码库。

无论如何,插件开发在 IntelliJ IDEA 中是非常强大的,我推荐这种方法用于任何类型的 Java 代码分析或您希望自己以编程方式控制的重构(并且还没有被开箱即用的重构所涵盖)。

祝你好运!

【讨论】:

    【解决方案2】:

    六年过去了,所以这可能对你没有多大帮助。也许它会帮助别人......

    我有许多有用的 Eclipse JDT 搜索方法 here,它们利用了 SearchEngine 等。例如:

        /**
         * Collects the methods that access the specified member
         * @param element the field or method whose accessors are being determined
         * @param scope the elements being examined, e.g. this class or this package
         * @return the collection of methods that access the indicated member
         */
        public static Set<IMethod> calculateCallingMethods(
                IJavaElement element,
                IJavaSearchScope scope)
                throws CoreException {
            SearchEngine searchEngine = new SearchEngine();
            SearchParticipant[] participants =
                new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
            SearchPattern callingMethodPattern = SearchPattern.createPattern(
                    element, REFERENCES);
            MethodCollector methodCollector = new MethodCollector();
            searchEngine.search(callingMethodPattern, participants, scope,
                    methodCollector, null);
            Set<IMethod> callers = methodCollector.getResults();
            return callers;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2010-10-08
      • 1970-01-01
      相关资源
      最近更新 更多