Parse Java source code file into PSI file and do some operations on the PSI elements
Answered
Hi,
Is it possible to parse a java source code file (like a path of the file) and convert it into a PSI file so that I could iterate over the PSI elements in it?
I saw a few similar posts and tried their approaches. For example in below, a PsiManager is used to create (or find) the virtual file. However, a "Project project" parameter is needed ("mProject" in the below example) and I do not know how to get that parameter. Could someone shed some light on this?
Thank you! :)
The code below is obtained from https://gist.github.com/agaricusb/4367023
if (!virtualFile.isDirectory() && "java".equalsIgnoreCase(virtualFile.getExtension()) ) {
PsiManager psiMgr = PsiManager.getInstance(mProject);
PsiFile file = psiMgr.findFile(virtualFile);
if (!(file instanceof PsiJavaFile) ) {
PsiJavaFile jFile = PsiJavaFile)psiMgr.getElementFactory().createFileFromText("temp.java", file.getText());
// do whatever you want with your PsiJavaFile
}
Please sign in to leave a comment.
Project is always required to setup necessary SDK, libraries, and other configurations (e.g., framework settings)
https://plugins.jetbrains.com/docs/intellij/project-structure.html
Please explain what the actual context is where your code should run.
Hi, my context is set up project variable to use with PSI and Virtual Files, but documentation seems a bit unclear about what to do