Skip to content

Instantly share code, notes, and snippets.

@kdlan
Created August 1, 2013 06:06
Show Gist options
  • Save kdlan/6128766 to your computer and use it in GitHub Desktop.
Save kdlan/6128766 to your computer and use it in GitHub Desktop.
Test for method generic type parameter info with proxy
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Test {
public interface TestService{
List<Map<String,Object>> list(List<Map<String,Object>> list);
}
public static class TestServiceImpl implements TestService{
public List<Map<String, Object>> list(List<Map<String, Object>> list) {
return list;
}
}
public static TestService proxy(TestService bean){
return (TestService) Proxy.newProxyInstance(Test.class.getClassLoader(), new Class[]{TestService.class}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return null;
}
});
}
static Type[] getGenericTypeParams(TestService bean) throws Exception{
//这里使用getClass而不是直接TestService.class
Class<?> clazz=bean.getClass();
return clazz.getDeclaredMethod("list", List.class).getGenericParameterTypes();
}
public static void main(String[] args) throws Exception{
TestService rawBean=new TestServiceImpl();
TestService proxyBean=proxy(rawBean);
System.out.println(Arrays.toString(getGenericTypeParams(rawBean)));
System.out.println(Arrays.toString(getGenericTypeParams(proxyBean)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment