Created
August 1, 2013 06:06
-
-
Save kdlan/6128766 to your computer and use it in GitHub Desktop.
Test for method generic type parameter info with proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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