Java:获取类中的所有变量名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2126714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: get all variable names in a class
提问by ufk
I have a class and I want to find all of its public fields(not methods). How can I do this?
我有一个类,我想找到它的所有公共字段(不是方法)。我怎样才能做到这一点?
Thanks!
谢谢!
采纳答案by Bozho
Field[] fields = YourClassName.class.getFields();
returns an array of all public variables of the class.
返回类的所有公共变量的数组。
getFields()
return the fields in the whole class-heirarcy. If you want to have the fields defined only in the class in question, and not its superclasses, use getDeclaredFields()
, and filter the public
ones with the following Modifier
approach:
getFields()
返回整个类继承中的字段。如果您只想在相关类中定义字段,而不是在其超类中定义字段,请使用getDeclaredFields()
并public
通过以下Modifier
方法过滤这些字段:
Modifier.isPublic(field.getModifiers());
The YourClassName.class
literal actually represents an object of type java.lang.Class
. Check its docs for more interesting reflection methods.
的YourClassName.class
字面实际上代表类型的对象java.lang.Class
。检查其文档以获取更有趣的反射方法。
The Field
class above is java.lang.reflect.Field
. You may take a look at the whole java.lang.reflect
package.
Field
上面的类是java.lang.reflect.Field
. 你可以看看整个java.lang.reflect
包。
回答by cellepo
https://docs.oracle.com/javase/tutorial/reflect/class/classMembers.htmlalso has charts for locating methods and constructors.
https://docs.oracle.com/javase/tutorial/reflect/class/classMembers.html也有用于定位方法和构造函数的图表。
回答by Amit Kaneria
As mentioned by few users, below code can help find all the fields in a given class.
正如少数用户所提到的,下面的代码可以帮助查找给定类中的所有字段。
TestClass testObject= new TestClass().getClass();
Method[] methods = testObject.getMethods();
for (Method method:methods)
{
String name=method.getName();
if(name.startsWith("get"))
{
System.out.println(name.substring(3));
}else if(name.startsWith("is"))
{
System.out.println(name.substring(2));
}
}
However a more interesting approach is below:
然而,更有趣的方法如下:
With the help of Hymanson library, I was able to find all class properties of type String/integer/double, and respective values in a Map class. (without using reflections api!)
在 Hymanson 库的帮助下,我能够找到 String/integer/double 类型的所有类属性,以及 Map 类中的相应值。(不使用反射 api!)
TestClass testObject = new TestClass();
com.fasterxml.Hymanson.databind.ObjectMapper m = new com.fasterxml.Hymanson.databind.ObjectMapper();
Map<String,Object> props = m.convertValue(testObject, Map.class);
for(Map.Entry<String, Object> entry : props.entrySet()){
if(entry.getValue() instanceof String || entry.getValue() instanceof Integer || entry.getValue() instanceof Double){
System.out.println(entry.getKey() + "-->" + entry.getValue());
}
}
回答by Sahil Chhabra
You can use any of the two based on your need:
您可以根据需要使用两者中的任何一个:
Field[] fields = ClassName.class.getFields(); // returns inherited members but not private members.
Field[] fields = ClassName.class.getDeclaredFields(); // returns all members including private members but not inherited members.
To filter only the public fields from the above list (based on requirement) use below code:
要仅过滤上述列表中的公共字段(根据要求),请使用以下代码:
List<Field> fieldList = Arrays.asList(fields).stream().filter(field -> Modifier.isPublic(field.getModifiers())).collect(
Collectors.toList());