Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Method;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import java.util.Collection;
- /**
- * Created by tom on 26.05.16.
- */
- public class GenericReflectionExample {
- public static void main(String[] args) throws Exception{
- Processor<?> processor = new StringProcessor();
- Method method = processor.getClass().getMethod("process", Collection.class);
- Type parameterType = method.getGenericParameterTypes()[0];
- System.out.println(parameterType);
- System.out.println(((ParameterizedType)parameterType).getActualTypeArguments()[0]);
- }
- interface Processor<T>{
- void process(Collection<T> collection);
- }
- static class StringProcessor implements Processor<String> {
- @Override
- public void process(Collection<String> collection) {
- System.out.println("Consume String");
- }
- }
- static class IntegerProcessor implements Processor<Integer> {
- @Override
- public void process(Collection<Integer> collection) {
- System.out.println("Consume Integer");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement