我如何确定Java中的通用字段的类型? [英] How can I determine the type of a generic field in Java?

查看:95
本文介绍了我如何确定Java中的通用字段的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图确定一个班级中的一个领域的类型。我已经看到了所有的内省方法,但还没有弄清楚如何去做。这将用于从java类生成xml / json。我在这里查看了一些问题,但没有找到我需要的。



示例:

  class Person {
public final String name;
public final List< Person>儿童;
}

当我编组这个对象时,我需要知道 chidren 字段是类型 Person 的对象列表,所以我可以正确编组它。



我曾试过(字段:Person.class.getDeclaredFields()){


  System.out.format(Type:%s%n,field.getType()); 
}

但是这只会告诉我它是 List ,而不是 List



感谢

解决方案

查看从Java教程中获取字段类型 Trail:The Reflection API

基本上,您需要做的是获取所有 java.lang.reflect.Field ,然后调用 字段# getType() 上的每一个(检查下面的编辑)。要获取所有对象字段(包括公共,受保护,封装和专用访问字段),只需使用 Class.getDeclaredFields() 。像这样:

  for(Field field:Person.class.getDeclaredFields()){
System.out。格式(类型:%s%n,field.getType());
System.out.format(GenericType:%s%n,field.getGenericType());

编辑: =https://stackoverflow.com/users/49237/wowest> wowest 在评论中,您实际上需要致电 Field#getGenericType() ,如果返回的 Type ParameterizedType ,然后相应地获取参数。使用 ParameterizedType#getRawType() ParameterizedType#getActualTypeArgument() 来获取原始类型和一个类型为分别为 ParameterizedType 。下面的代码演示了这个:

  for(Field field:Person.class.getDeclaredFields()){
System。 out.print(Field:+ field.getName()+ - );
Type type = field.getGenericType();
if(type instanceof ParameterizedType){
ParameterizedType pType =(ParameterizedType)type;
System.out.print(Raw type:+ pType.getRawType()+ - );
System.out.println(Type args:+ pType.getActualTypeArguments()[0]);
} else {
System.out.println(Type:+ field.getType());
}
}

然后输出:

 字段:name  - 类型:class java.lang.String 
字段:children - 原始类型:接口java.util.List - 类型args: class foo.Person


I have been trying to determine the type of a field in a class. I've seen all the introspection methods but haven't quite figured out how to do it. This is going to be used to generate xml/json from a java class. I've looked at a number of the questions here but haven't found exactly what I need.

Example:

class Person {
    public final String name;
    public final List<Person> children;
}

When I marshall this object, I need to know that the chidren field is a list of objects of type Person, so I can marshall it properly.

I had tried

for (Field field : Person.class.getDeclaredFields()) {
    System.out.format("Type: %s%n", field.getType());
}

But this will only tell me that it's a List, not a List of Persons

Thanks

解决方案

Have a look at Obtaining Field Types from the Java Tutorial Trail: The Reflection API.

Basically, what you need to do is to get all java.lang.reflect.Field of your class and call Field#getType() on each of them (check edit below). To get all object fields including public, protected, package and private access fields, simply use Class.getDeclaredFields(). Something like this:

for (Field field : Person.class.getDeclaredFields()) {
    System.out.format("Type: %s%n", field.getType());
    System.out.format("GenericType: %s%n", field.getGenericType());
}

EDIT: As pointed out by wowest in a comment, you actually need to call Field#getGenericType(), check if the returned Type is a ParameterizedType and then grab the parameters accordingly. Use ParameterizedType#getRawType() and ParameterizedType#getActualTypeArgument() to get the raw type and an array of the types argument of a ParameterizedType respectively. The following code demonstrates this:

for (Field field : Person.class.getDeclaredFields()) {
    System.out.print("Field: " + field.getName() + " - ");
    Type type = field.getGenericType();
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType)type;
        System.out.print("Raw type: " + pType.getRawType() + " - ");
        System.out.println("Type args: " + pType.getActualTypeArguments()[0]);
    } else {
        System.out.println("Type: " + field.getType());
    }
}

And would output:

Field: name - Type: class java.lang.String
Field: children - Raw type: interface java.util.List - Type args: class foo.Person

这篇关于我如何确定Java中的通用字段的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆