java通过反射获取返回值类型

[ccn lang="java" tab_size="4" theme="blackboard" width="550" ]
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class TempTest {
public static void main(String[] args) throws Exception {
Method[] methods = TempTest.class.getDeclaredMethods();
for (Method method : methods) {
System.out.println("method:" + method.getName());// 方法名
// //////////////方法的参数
System.out.println(" paramTypeType: ");
Type[] paramTypeList = method.getGenericParameterTypes();// 方法的参数列表
for (Type paramType : paramTypeList) {
System.out.println(" " + paramType);// 参数类型
if (paramType instanceof ParameterizedType)/**//* 如果是泛型类型 */{
Type[] types = ((ParameterizedType) paramType)
.getActualTypeArguments();// 泛型类型列表
System.out.println(" TypeArgument: ");
for (Type type : types) {
System.out.println(" " + type);
}
}
}
// //////////////方法的返回值
System.out.println(" returnType: ");
Type returnType = method.getGenericReturnType();// 返回类型
System.out.println(" " + returnType);
if (returnType instanceof ParameterizedType)/**//* 如果是泛型类型 */{
Type[] types = ((ParameterizedType) returnType)
.getActualTypeArguments();// 泛型类型列表
System.out.println(" TypeArgument: ");
for (Type type : types) {
System.out.println(" " + type);
}
}
}
}
public static String method1(List list) {
return null;
}
private static Map< String, Double> method2(Map< String, Object> map) {
return null;
}
}
[/ccn]
运行结果:

C:\temp>java TempTest
method:method1
paramTypeType:
interface java.util.List
returnType:
class java.lang.String

method:method2
paramTypeType:
java.util.Map< java.lang.String, java.lang.Object>
TypeArgument:
class java.lang.String
class java.lang.Object
returnType:
java.util.Map< java.lang.String, java.lang.Double>
TypeArgument:
class java.lang.String
class java.lang.Double

method:main
paramTypeType:
class [Ljava.lang.String;
returnType:
void