方法类 | Java中的 getGenericReturnType() 方法
Java.lang.reflect.Method 类的getGenericReturnType()方法返回一个表示返回类型的 Type 对象,在编码时在方法中声明。因此,getGenericReturnType() 方法返回方法对象的返回类型。
如果形式返回类型是参数化类型,则为其返回的 Type 对象必须准确反映源代码中使用的实际类型参数。例如,对于方法 public T getValue(){},如果将类型 T 替换为参数化类型(即 List),则 getGenericReturnType() 将返回“Java.util.List< Java.lang.String>”为返回类型。
句法:
public Type getGenericReturnType()
返回值:它返回一个 Type 对象,表示方法对象的正式返回类型。
异常:此方法抛出以下异常:
- GenericSignatureFormatError – 如果通用方法签名与 JVM 规范中指定的格式不同。
- TypeNotPresentException – 如果返回类型指的是不存在的类型声明。
- MalformedParameterizedTypeException – 如果基础返回类型引用了由于任何原因无法实例化的参数化类型。
例子:
Code:
public class demo{
public T getValue(){}
}
Explanation:
In the above method when we going to apply getGenericReturnType() method
it is going to return T as a generic return type.
Code:
public class demo{
public List getValue(){}
}
Explanation:
In the above method when we going to apply getGenericReturnType() method
it is going to return java.util.List as a generic return type
下面的程序说明了 Method 类的 getGenericReturnType() 方法
程序 1:通过在方法上应用 getGenericReturnType() 打印方法对象的返回类型。
Java
// Program to apply getGenericReturnType() method
// of Method Class.
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
// Main method
public static void main(String[] args)
{
try {
// create class object
Class classobj = demoForReturnParam.class;
// get Method Object
Method[] methods = classobj.getMethods();
// iterate through methods
for (Method method : methods) {
// taking only method defined in the demo class
if (method.getName().equals("setValue")
|| method.getName().equals("getValue")
|| method.getName().equals("setManyValues")) {
// apply getGenericReturnType() method
Type returnParam = method.getGenericReturnType();
// print return Types of method Object
System.out.println("\nMethod Name : "
+ method.getName());
System.out.println("Return Type Details: "
+ returnParam);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// a simple class
class demoForReturnParam {
// method returning int value
public int setValue()
{
System.out.println("setValue");
return 24;
}
// method returning string value
public String getValue()
{
System.out.println("getValue");
return "getValue";
}
// method returning nothing
public void setManyValues(int value1,
String value3)
{
System.out.println("setManyValues");
}
}
Java
// Program to show how to apply
// getGenericReturnType() method of Method Class
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
// Main method
public static void main(String[] args)
{
try {
// create class object
Class classobj = DemoForReturnParam.class;
Method[] methods = classobj.getMethods();
// check whether setManyValues() method
// contains string parameter or not
for (Method method : methods) {
if (method.getName().equals("setValue")) {
boolean flag = containsReturnParameter(method,
(Type)java.lang.String.class);
System.out.println("setValue()"
+ " contains int return type: "
+ flag);
}
}
// check whether setManyValues() method
// contains int parameter or not
for (Method method : methods) {
if (method.getName().equals("setManyValues")) {
boolean flag = containsReturnParameter(method,
(Type) int.class);
System.out.println("setManyValues()"
+ " contains int return type: "
+ flag);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
// check whether method object
// have same return type or not
private static boolean
containsReturnParameter(Method method, Type parameterName)
{
// get return type using getGenericReturnType()
Type returnParameter = method.getGenericReturnType();
// check contains return parameter or not
if (returnParameter == parameterName) {
return true;
}
return false;
}
}
// a simple class
class DemoForReturnParam {
// method returning int value
public void setValue()
{
System.out.println("setValue");
}
// method returning nothing
public int setManyValues(int value1, String value3)
{
System.out.println("setManyValues");
return 21;
}
}
输出:
Method Name : setManyValues
Return Type Details: void
Method Name : getValue
Return Type Details: class java.lang.String
Method Name : setValue
Return Type Details: int
程序 2:给一个 Return 参数类型作为输入来检查方法对象是否具有相同的返回类型。
Java
// Program to show how to apply
// getGenericReturnType() method of Method Class
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
// Main method
public static void main(String[] args)
{
try {
// create class object
Class classobj = DemoForReturnParam.class;
Method[] methods = classobj.getMethods();
// check whether setManyValues() method
// contains string parameter or not
for (Method method : methods) {
if (method.getName().equals("setValue")) {
boolean flag = containsReturnParameter(method,
(Type)java.lang.String.class);
System.out.println("setValue()"
+ " contains int return type: "
+ flag);
}
}
// check whether setManyValues() method
// contains int parameter or not
for (Method method : methods) {
if (method.getName().equals("setManyValues")) {
boolean flag = containsReturnParameter(method,
(Type) int.class);
System.out.println("setManyValues()"
+ " contains int return type: "
+ flag);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
// check whether method object
// have same return type or not
private static boolean
containsReturnParameter(Method method, Type parameterName)
{
// get return type using getGenericReturnType()
Type returnParameter = method.getGenericReturnType();
// check contains return parameter or not
if (returnParameter == parameterName) {
return true;
}
return false;
}
}
// a simple class
class DemoForReturnParam {
// method returning int value
public void setValue()
{
System.out.println("setValue");
}
// method returning nothing
public int setManyValues(int value1, String value3)
{
System.out.println("setManyValues");
return 21;
}
}
输出:
setValue() contains int return type: false
setManyValues() contains int return type: true
参考:
https://docs.oracle.com/javase/8/docs/api/java Java