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

2

10

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.

Gundry answered 3/7, 2013 at 18:24 Comment(2)
Eclipse is open source. Here is its documentation.Altorelievo
Maybe structural search in intellij could help? jetbrains.com/idea/documentation/ssr.htmlHaro
G
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!

Gouge answered 3/7, 2013 at 20:4 Comment(0)
H
0

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;
}
Herbherbaceous answered 8/7, 2019 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.