fork download
  1. import java.io.*;
  2. import java.lang.reflect.*;
  3. import java.util.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone {
  7. static class TypeToken<T> {
  8. Type getType() {
  9. ParameterizedType t = (ParameterizedType) getClass().getGenericSuperclass();
  10. return t.getActualTypeArguments()[0];
  11. }
  12. }
  13.  
  14. static <T> TypeToken<T> genericToken() {
  15. return new TypeToken<T>() {};
  16. }
  17.  
  18. public static void main(String[] args) {
  19. Collection<Integer> list = new ArrayList<>();
  20. System.out.println(list.getClass().getGenericInterfaces()[0]);
  21.  
  22. TypeToken<Collection<Integer>> tt = new TypeToken<Collection<Integer>>() {};
  23.  
  24. System.out.println(tt.getClass().getGenericSuperclass());
  25. System.out.println(tt.getType());
  26.  
  27. TypeToken<Collection<Integer>> g = genericToken();
  28. System.out.println(g.getType());
  29. }
  30. }
  31.  
Success #stdin #stdout 0.1s 27708KB
stdin
Standard input is empty
stdout
java.util.List<E>
Ideone.Ideone$TypeToken<java.util.Collection<java.lang.Integer>>
java.util.Collection<java.lang.Integer>
T