public class Client {
private Map<String, Object> objectMap;
public void test(Map<String, User> map, String string) {
}
public Map<User, Bean> test() {
return
null
;
}
@Test
public void testFieldType() throws NoSuchFieldException {
Field field = Client.class.getDeclaredField(
"objectMap"
);
Type gType = field.getGenericType();
System.out.println(field.getType());
System.out.println(gType);
System.out.println(
"**************"
);
if
(gType
instanceof
ParameterizedType) {
ParameterizedType pType = (ParameterizedType) gType;
Type[] types = pType.getActualTypeArguments();
for
(Type type : types) {
System.out.println(type.toString());
}
}
}
@Test
public void testParamType() throws NoSuchMethodException {
Method testMethod = Client.class.getMethod(
"test"
, Map.class, String.class);
Type[] parameterTypes = testMethod.getGenericParameterTypes();
for
(Type type : parameterTypes) {
System.out.println(
"type -> "
+ type);
if
(type
instanceof
ParameterizedType) {
Type[] actualTypes = ((ParameterizedType) type).getActualTypeArguments();
for
(Type actualType : actualTypes) {
System.out.println(
"\tactual type -> "
+ actualType);
}
}
}
}
@Test
public void testReturnType() throws NoSuchMethodException {
Method testMethod = Client.class.getMethod(
"test"
);
Type returnType = testMethod.getGenericReturnType();
System.out.println(
"return type -> "
+ returnType);
if
(returnType
instanceof
ParameterizedType) {
Type[] actualTypes = ((ParameterizedType) returnType).getActualTypeArguments();
for
(Type actualType : actualTypes) {
System.out.println(
"\tactual type -> "
+ actualType);
}
}
}
}