当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Method类 getGenericReturnType()用法及代码示例


java.lang.reflect.Method类的getGenericReturnType()方法返回一个Type对象,该对象表示返回类型,该类型在编码时在方法中声明。因此,getGenericReturnType()方法返回方法对象的返回类型。

如果正式返回类型是参数化类型,则为其返回的Type对象必须准确反映源代码中使用的实际类型参数。例如,对于公共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<java.lang.String> as a generic return type

以下示例程序旨在说明Method类的getGenericReturnType()方法
程序1:通过在方法上应用getGenericReturnType()来打印方法对象的返回类型。

// 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"); 
    } 
}
输出:
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参数类型作为输入,以检查方法对象是否具有相同的返回类型。

// 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/lang/reflect/Method.html#getGenericReturnType-




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | getGenericReturnType() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。