fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.lang.reflect.*;
  7.  
  8. public class Main {
  9.  
  10. private static void doit(Object x, String name) {
  11. System.out.println("Processing " + name);
  12. Type[] ifs = x.getClass().getGenericInterfaces();
  13. System.out.println(ifs.length);
  14. for (Type c : ifs) {
  15. System.out.println(c);
  16. Type[] tps = ((ParameterizedType)c).getActualTypeArguments();
  17. for (Object tp : tps) {
  18. System.out.println("===="+tp);
  19. }
  20. }
  21. }
  22.  
  23. public static void main (String[] args) throws java.lang.Exception
  24. {
  25.  
  26. MyInterface<? extends Number> x = new DoubleImpl();
  27. MyInterface<? extends Number> y = new IntImpl();
  28. MyInterface<? extends Number> z = new FloatImpl();
  29. doit(x, "double");
  30. doit(y, "int");
  31. doit(z, "float");
  32. }
  33. }
  34.  
  35. interface MyInterface<T extends Number> {
  36. T getVal();
  37. }
  38.  
  39. class DoubleImpl implements MyInterface<Double> {
  40. public Double getVal() {return 42.42; }
  41. }
  42. class IntImpl implements MyInterface<Integer> {
  43. public Integer getVal() {return 42; }
  44. }
  45. class FloatImpl implements MyInterface<Float> {
  46. public Float getVal() {return 42.24F; }
  47. }
  48.  
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
Processing double
1
MyInterface<java.lang.Double>
====class java.lang.Double
Processing int
1
MyInterface<java.lang.Integer>
====class java.lang.Integer
Processing float
1
MyInterface<java.lang.Float>
====class java.lang.Float