0

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).

Idan Str
  • 614
  • 1
  • 11
  • 33
  • 2
    Please consider telling us more details of what you're trying to do (not 100% clear to me) and more importantly, about your motivation behind your request. I can't help wondering if this is an [XY Problem](http://xyproblem.info/) in disguise. – Hovercraft Full Of Eels Apr 28 '18 at 23:11
  • 1
    I'm not familiar with JSystem, but it sounds like what you're trying to do is what's called "reflection". Searching for that term might point you in the right direction. – Frank Riccobono Apr 28 '18 at 23:17
  • yes, I try reflection but for class file. – Idan Str Apr 28 '18 at 23:19

1 Answers1

2

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);
  }