How can I perform the searches Java IDEs do for method references programmatically? [closed]

本秂侑毒 提交于 2019-12-08 16:53:50

问题


You know the find all references feature of eclipse (Search > References > Workspace or Ctrl-Shift-G)? How can I run that programmatically?

I have a large codebase that I need to audit for security violations and need to chain about a dozen conditions.

Are there libraries out there that can analyze large projects (I have 1GB of source files) in Java? Here's the questions I need to answer:

  • Pass in a list of Interfaces, find all implementations of those interfaces
  • Search that list for invocations of our security library
  • Search every method referenced in the list above to confirm that the proper authorization library call was made.

I can do this manually in a day, but would rather spend 2 writing a nice script to do the work for me.

Any leads on libraries I can use to script these common tasks that Eclipse does so nicely? Ideally, I would like to perform them command-line, so they can be repeatable and scripted, but obviously will take what I can get.


回答1:


You could write your own plugin in IntelliJ IDEA to accomplish this.

Programmatically, you can utilize the PSI (program structure interface) tree to search for various code elements (classes, variables, methods, statements, etc), navigate around your code base and then do whatever you need to do.

Here are some references:

  1. IntelliJ IDEA Plugin Development

  2. PSI Cookbook

  3. PsiViewer Plugin

All of the things that you need to do (find implementations, search for invocations, search methods for a particular statement) are possible.

If you download the source code for the community edition of IntelliJ IDEA you can look around in the code and see how these things are done.

Or you can post specific questions here (tag with intellij-plugin) and people like CrazyCoder might be kind enough to help you out :)

With regards to the scope of the code base (1 GB you say?), you'd just have to try it and see how you go. Worst case, you could analyze your code base in parts.

In any case, plugin development is extremely powerful in IntelliJ IDEA and I recommend this approach for any sort of java code analysis or refactorings that you want to control programmatically yourself (and aren't already covered by the out-of-the-box refactorings).

Good luck!




回答2:


It's six years later, so this probably won't help you much. Maybe it will help somebody else...

I have a number of useful Eclipse JDT search methods here, that make use of SearchEngine, et al. For example:

    /**
     * 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;
}


来源:https://stackoverflow.com/questions/17455521/how-can-i-perform-the-searches-java-ides-do-for-method-references-programmatical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!