Advertisement
Guest User

Untitled

a guest
May 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2. import java.lang.reflect.ParameterizedType;
  3. import java.lang.reflect.Type;
  4. import java.util.Collection;
  5.  
  6. /**
  7. * Created by tom on 26.05.16.
  8. */
  9. public class GenericReflectionExample {
  10.  
  11. public static void main(String[] args) throws Exception{
  12.  
  13. Processor<?> processor = new StringProcessor();
  14.  
  15. Method method = processor.getClass().getMethod("process", Collection.class);
  16. Type parameterType = method.getGenericParameterTypes()[0];
  17. System.out.println(parameterType);
  18.  
  19. System.out.println(((ParameterizedType)parameterType).getActualTypeArguments()[0]);
  20.  
  21. }
  22.  
  23. interface Processor<T>{
  24.  
  25. void process(Collection<T> collection);
  26. }
  27.  
  28. static class StringProcessor implements Processor<String> {
  29.  
  30. @Override
  31. public void process(Collection<String> collection) {
  32. System.out.println("Consume String");
  33. }
  34. }
  35.  
  36. static class IntegerProcessor implements Processor<Integer> {
  37.  
  38. @Override
  39. public void process(Collection<Integer> collection) {
  40. System.out.println("Consume Integer");
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement