How to get String constants values from java file inside a plugin


I am writing a plugin which gets all the String constants and their values from the java file. So instead of parsing the file i am using java reflection to get the values. To get all the fields i am using ClassName.getDeclardFields(). How to use reflection on virtual file to get all the declared field or how to get all the declared string constants and their values inside virtual file.

The String constant file is like this


public class Constants {

   public static final String one = "1";

 public static final String two = "2";

 public static final String three = "3";


1 comment

You don't need reflection, you can do this with PSI. If you have the virtualFile of your ``, then you can get the corresponding PsiFile as below:

val psiFile = PsiManager.getInstance(project).findFile(virtualFile)

Using "psiFile.classes" you get get access to PsiClass. This has a lot of methods to get Fields, Methods, Parmeters, etc... 


Please sign in to leave a comment.