Search code examples
javajsystem

How can i get class parameters of class file


I want to read parameters of a class after compilation. I only need the names.

For example:

public class X {
   public String y;
   ...
}

I want to compile the code to a class file. Then have another java project read the names of classes in this case X and all of class's parameters in this case y.

Can i do it? If its possible, are the parameters have to be public?

Thanks.

EDIT:

I tried to use JSystem - an automation framework in java. To do it, do I need to read a class file and read all the class names and those parameters? I hope its understandable.

For people who know JSystem, I tried to make JRunner but web gui(with spring).


Solution

  • You want to use java reflection. The following code will give you all classes in a package:

     Reflections reflections = new Reflections("my.project.prefix");
    
     Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);
    

    Then to get the properties of a class you can do the following:

     for (Field f : getClass().getDeclaredFields()) {
        System.out.println("Field: " + f);
      }