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

3
  • 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 in disguise. Commented Apr 28, 2018 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. Commented Apr 28, 2018 at 23:17
  • yes, I try reflection but for class file. Commented Apr 28, 2018 at 23:19

1 Answer 1

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.