Backend Development 6 min read

Using PSI in IntelliJ IDEA Plugin Development

This article explains the concept of PSI (Program Structure Interface) in IntelliJ IDEA, demonstrates how to obtain PsiFile objects, manipulate PsiJavaFile, PsiClass, PsiField, and add annotations and parameters, providing a practical guide for plugin developers.

58 Tech
58 Tech
58 Tech
Using PSI in IntelliJ IDEA Plugin Development

Background – The previous article introduced basic knowledge of IDEA plugin development; this article focuses on using PSI (Program Structure Interface) in IDEA plugins.

What is PSI – PSI stands for Program Structure Interface, a layer in the IDEA platform that parses files and creates syntax and semantic models, enabling operations on project files.

PsiFile – The root of all file content structures; all file‑related operations are performed on a PsiFile.

There are five ways to obtain a PsiFile object in the official documentation:

From an event: e.getData(LangDataKeys.PSI_FILE);

From a virtual file: PsiManager.getInstance(project).findFile();

From a document: PsiDocumentManager.getInstance(project).getPsiFile();

From an element in the file: psiElement.getContainingFile();

By file name in the project: FilenameIndex.getFilesByName(project, name, scope);

Creating a file in an IDEA project results in a FileTest.java file, initially empty and marked as a non‑compilable Java file.

PsiJavaFile – If the PsiFile represents a Java file, it can be cast to a PsiJavaFile , which contains package statements, import lists, and classes.

PsiClass – Represents a class in a Java file; operations on the class (e.g., generating code, adding annotations) are performed via PsiClass. Right‑click → Generate shows many class‑related actions.

Adding an annotation to a class requires wrapping the write operation in WriteCommandAction.runWriteCommandAction() . After adding the annotation, the IDE may flag missing imports, which can be resolved by invoking the import‑assist feature.

PsiField – Used to manipulate class fields. Fields can be added, and annotations can be attached to them. Annotation parameters are set via PsiAnnotation.setDeclaredAttributeValue() .

Creating PSI expressions for annotation parameters can be done using the PSI factory methods (illustrated in the article).

PsiMethod – Similar to PsiField, methods are manipulated via PsiMethod objects (not detailed here).

PsiType – Handles the data types of fields, including primitive, array, collection, and reference types. For reference types, one must retrieve the class’s fields and their default values.

Conclusion – The article covered essential PSI components used in IDEA plugin development, providing code snippets and screenshots to illustrate each step.

Javacode generationIntelliJ IDEAIDEplugin developmentPSI
58 Tech
Written by

58 Tech

Official tech channel of 58, a platform for tech innovation, sharing, and communication.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.