Advertisement
domenico_86

How to get GenericTypes from a Field (java)

Nov 27th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public static List<Class<?>> getGenericTypes(Field f){
  2.         List<Type> list = Arrays.asList(((ParameterizedType) f.getGenericType()).getActualTypeArguments());
  3.         return list.stream()
  4.             .map((a)->(Class<?>)a)
  5.             .collect(Collectors.toList());
  6.     }
  7.  
  8. /* example */
  9. public class A(){
  10.      List<String> list = new ArrayList<>();
  11.  
  12.      public static void main(String[] args){
  13.          Field f = A.class.getDeclaredField("list");
  14.          System.out.println(getGenericTypes(f)); // output --> class java.lang.String
  15.      }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement