How can I perform the searches Java IDEs do for me

2019-06-15 18:16发布

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条回答
等我变得足够好
2楼-- · 2019-06-15 18:53

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!

查看更多
登录 后发表回答