java.lang.reflect.Method.getGenericReturnType() 方法示例
描述
java.lang.reflect.Method.getGenericReturnType() 方法返回一个 Type 对象,该对象表示此 Method 对象所表示的方法的正式返回类型。
声明
以下是 java.lang.reflect.Method.getGenericReturnType() 方法的声明。
public Type getGenericReturnType()
返回
一个 Type 对象,该对象表示底层方法的正式返回类型。
异常
GenericSignatureFormatError −如果泛型方法签名不符合 Java 虚拟机规范中指定的格式。
TypeNotPresentException − 如果底层方法的任何参数类型引用了不存在的类型声明。
MalformedParameterizedTypeException − 如果底层方法的任何参数类型引用了由于任何原因无法实例化的参数化类型。
示例
以下示例显示了 java.lang.reflect.Method.getGenericReturnType() 方法的用法。
package com.tutorialspoint;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods();
Type returnType = methods[0].getGenericReturnType();
System.out.println(returnType);
}
}
class SampleClass {
private String sampleField;
public String getSampleField() {
return sampleField;
}
public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
}
让我们编译并运行上述程序,这将产生以下结果 −
class java.lang.String