How to get String constants values from java file inside a plugin
Answered
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"
;
}
Please sign in to leave a comment.
You don't need reflection, you can do this with PSI. If you have the virtualFile of your `Constants.java`, then you can get the corresponding PsiFile as below:
Using "psiFile.classes" you get get access to PsiClass. This has a lot of methods to get Fields, Methods, Parmeters, etc...