[英]Determine if a field is a type I created, using reflection
Assuming I have an object and I took it fields: 假设我有一个对象,并将其带入字段:
Field[] fields = obj.getFields();
Now I'm iterating through each one and would like to print their members if it's some kind of class, otherwise just use field.get(obj)
in case it's a string, int or anything that this command will print its value, not just the reference pointer. 现在,我要遍历每个对象,如果是某种类,则希望打印其成员,否则,如果它是字符串,int或此命令将打印其值的任何东西,则仅使用field.get(obj)
,而不仅仅是参考指针。
How can I detect it? 如何检测到它?
You can get, without instantiation required, the Type
of each field of a class like this: 您可以在不需要实例化的情况下获得此类的每个字段的Type
:
public class GetFieldType {
public static void main (String [] args) {
Field [] fields = Hello.class.getFields();
for (Field field: fields) {
System.out.println(field.getGenericType());
}
}
public static class Hello {
public ByeBye bye;
public String message;
public Integer b;
...
}
}
You can use instanceof
to tell the objects apart. 您可以使用instanceof
区分对象。
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
Object[] objects = new Object[4];
objects[0] = new Integer(2);
objects[1] = "StringTest";
objects[2] = new BigDecimal(2.00d);
for (Object obj : objects) {
if (obj != null) {
if (obj instanceof BigDecimal) {
System.out.println("bigdecimal found " + obj);
} else if (obj instanceof String) {
System.out.println("String found " + obj);
} else {
System.out.println("Integer found " + obj);
}
}
else{
System.out.println("object is null");
}
}
}
If you need to test if an object is from your project, you can look at the package name and compare it to your project's package name. 如果需要测试对象是否来自项目,则可以查看程序包名称并将其与项目的程序包名称进行比较。
You can either do this on the declared type of the field or on the runtime type of the field contents. 您可以在字段的声明类型或字段内容的运行时类型上执行此操作。 The snippet below demonstrates the latter approach: 下面的代码段演示了后一种方法:
SomeClass foo = new SomeClass();
for (Field f : foo.getClass().getDeclaredFields()) {
boolean wasAccessible = f.isAccessible();
try {
f.setAccessible(true);
Object object = f.get(foo);
if (object != null) {
if (object.getClass().getPackage().getName()
.startsWith("your.project.package")) {
// one of yours
}
} else {
// handle a null value
}
} finally {
f.setAccessible(wasAccessible);
}
}
Do remember that obj.getFields();
请记住, obj.getFields();
only returns publicly-accessible fields. 仅返回可公开访问的字段。 You may want to consider getDeclaredFields()
as I've done above. 您可能需要像上面所做的那样考虑getDeclaredFields()
。 If you stick with getFields()
, you can omit the accessibility code in the above example. 如果坚持使用getFields()
,则可以在上面的示例中省略可访问性代码。
With a bit of work, you could distinguish your classes by the classloader that loaded them. 通过一些工作,您可以通过加载它们的类加载器来区分您的类。 Take a look at this: 看看这个:
Find where java class is loaded from 查找从何处加载Java类
Though this could help, it's not going to be the silver bullet for your problem, mainly because: 尽管这可能有所帮助,但这并不是解决问题的灵丹妙药,主要是因为:
what I understood is you what to go recursive in object hierarchy and get values of primitives 我了解的是您要在对象层次结构中递归并获取基元的值
public class ParentMostClass {
int a = 5;
OtherClass other = new OtherClass();
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException {
ParentMostClass ref = new ParentMostClass();
printFiledValues(ref);
}
public static void printFiledValues(Object obj)
throws IllegalArgumentException, IllegalAccessException {
Class<? extends Object> calzz = obj.getClass();
Field[] fileds = obj.getClass().getDeclaredFields();
for (Field field : fileds) {
Object member = field.get(obj);
// you need to handle all primitive, they are few
if (member instanceof String || member instanceof Number) {
System.out.println(calzz + "->" + field.getName() + " : "
+ member);
} else {
printFiledValues(member);
}
}
}
}
public class OtherClass {
int b=10;
}
I got output as 我得到了输出
class com.example.ParentMostClass->a : 5
class com.example.OtherClass->b : 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.