Intellij Plugin : how to get the source code in a data structure?
What I am looking for should definitely exist somewhere, but I dont know where it is.
What I am making is a plugin that analyzes the Java/Groovy source code on building a project where this plugin should be enabled.
The one thing I dont know is how to get the source code of a specific module (and its dependencies) in a data structure so that all packages, classes/interfaces/enums, fields/functions and even the execution statements inside the functions/field initializers so that the plugin can analyze the code.
Basic question: How do I get it?
(Please note this is for Java AND Groovy source code.
And preferably if there are dependencies that are given in bytecode, it should decompile them and include it inside the data structure that is given to analyze.)
请先登录再写评论。
IntelliJ plugins can access source code via PSI interfaces, look PSI Files page in our documentation for details.
So... I am required to go through every single Psi element? (including whitespaces, semicolons, etc)
No, you can iterate over interesting elements only using a visitor (JavaRecursiveElementWalkingVisitor for java). Or you can call 'getClasses' method on PsiJavaFile to get classes from it, and then call 'getFields', 'getMethods' of returned PsiClass to inspect fields, methods, etc.
The problem with the visitors would be that there are no "interesting" elements... every single element has to be read out.
I now get all types and their fields and methods, but the body of the methods is still a mystery for me.
With
I suppose I should be able to get the entire statement implementation, but I get lost on the values of those statements.
Is there some documentation with what everything means?
For example, given the code
I would, in some way, like to be able to find the differences between the targets.
Aka,
This simple thing for example is essential for my parser to work properly, but I dont really know how this is done with the PsiMethodCallExpression fields/methods.
I now use https://github.com/javaparser/javaparser to get the data which works fine for the time being.
And it also allows a standalone version as it is not bound to Intellij.
Surely you can you any third-party parser, but it would be much more efficient to use PSI structures, because they are already built and IDEA automatically updates them when you change code in the editor.
In order to get the target element for PsiMethodCallExpression you can invoke getMethodExpression, then invoke getQualifierExpression on the result of the call. As described in its javadoc, getQualifierReference will return the part of the reference up to the latest dot. In general qualified of a method call can be any expression, but in your cases it will be PsiReferenceExpression again (in case 4 it'll be null though). So you can caset the qualifier to PsiReferenceExpression and call 'resolve'. It'll return the element the reference points to, i.e. PsiField in case 1, PsiParameter in case 2, PsiClass in case 3.