简体   繁体   English

如何获取类文件的类参数

[英]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 . 然后让另一个java项目在这种情况下读取类名X ,在这种情况下读取类的所有参数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. 我尝试使用JSystem-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). 对于了解JSystem的人,我尝试制作JRunner但使用Web gui(带有spring)。

You want to use java reflection. 您要使用Java反射。 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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM