Here you can find the source of getGenericsTypeFromCollectionField(Field field)
Parameter | Description |
---|---|
field | Field |
public static Class<?> getGenericsTypeFromCollectionField(Field field)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import java.util.Set; public class Main { /**//from w w w . ja va 2s.c o m * Gets generics info despite runtime type erasure. Hack alert, obviously. * @param field Field * @return Class (if found) or null if not */ public static Class<?> getGenericsTypeFromCollectionField(Field field) { Class<?> clazz = null; if (List.class.isAssignableFrom(field.getType()) || Set.class.isAssignableFrom(field.getType())) { field.setAccessible(true); ParameterizedType ptype = (ParameterizedType) field.getGenericType(); Type[] types = ptype.getActualTypeArguments(); if (types != null && types.length > 0) { clazz = (Class<?>) types[0]; } } return clazz; } }