0

I have some questions when I reflect genericType.First of all,I have a class declaration like this:

public class JoyPageWebDTO<T> {
    private int recordCount;
    private int pageSize;
    private int pageIndex;
    private List<T> records;
}

And T is:

public class AuditSpuWebDTO implements Serializable {
    private String indications;
    private String forbidden;
    private int shopID;
    private String shopName;
    private String city;
    private Date updateTime;
    private int auditStatus;
    private String auditContent;
}

The result of JoyPageWebDTO after request like this: [http://i.stack.imgur.com/zeK1O.jpg][1]

The question is how can I get the T(AuditSpuWebDTO) from JoyPageWebDTO? I want's get the value of AuditSpuWebDTO.I wrote some code as following to deal this,however,failed!

public static void reflect(Class<?> clazz) {
    for (Field field : clazz.getDeclaredFields()) {
        //get the static type of the field
        Class<?> fieldType = field.getType();
        //if it's String,
        if (fieldType == String.class) {
            // save/use field
        }
        //if it's String[],
        else if (fieldType == String[].class) {
            // save/use field
        }
        //if it's List or a subtype of List,
        else if (List.class.isAssignableFrom(fieldType)) {
            //get the type as generic
            ParameterizedType fieldGenericType = (ParameterizedType) field.getGenericType();
            //get it's first type parameter
            Type type = fieldGenericType.getActualTypeArguments()[0];
            Class<?> fieldTypeParameterClazz;
            if (type instanceof ParameterizedType) {
                fieldTypeParameterClazz = (Class<?>) ((ParameterizedType) type).getRawType();
            } else if(type instanceof TypeVariable){
                //todo how to get the class????
            }else{
                fieldTypeParameterClazz = (Class<?>) type;
            }
            //if the type parameter is String,
            if (fieldTypeParameterClazz == String.class) {
                // save/use field
            }
        }
    }
}

When I reflect about the generic type:T from List,the type of the result is TypeVariable,how can I get the Class<?> from TypeVariable? The todo which I mark is the key crucial problem I have.

yonney.yang
  • 125
  • 8
  • Possible duplicate of [Get generic type of class at runtime](http://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) – Sam Jul 23 '16 at 08:07
  • 2
    Quick answer: not possible. – Sam Jul 23 '16 at 08:07

1 Answers1

0

The only thing List knows about it's generic type is that it has a name T

As Java uses type erasure, it's not possible to obtain this information at runtime. What is possible to obtain is when you have a specific type compiled into the class e.g.

// create a subclass of ArrayList with a specific type.
List<String> list = new ArrayList<String>() { };

However, a plain List or ArrayList has no type compiled into that class so its not possible to retrieve any more information from it.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075