Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaMethod Class | getGenericReturnType() Method in Java

Method Class | getGenericReturnType() Method in Java

The getGenericReturnType() method of java.lang.reflect.Method class returns a Type object that represent the return type, declared in method at time of coding. Hence, getGenericReturnType() method returns the return type of method object.

If a formal return type is a parameterized type, the Type object returned for it must accurately reflect the actual type parameters used in the source code. For example, for method public T getValue(){}, if the type T is substituted with a parameterized type (i.e., List), then getGenericReturnType() will return “java.util.List<java.lang.String>” as return type.

Syntax: 

public Type getGenericReturnType()

Return Value: It returns a Type object that represent the formal return type of the method object.

Exception: This method throws following exceptions: 

  • GenericSignatureFormatError – if the generic method signature is not same as the format specified in The JVM Specification.
  • TypeNotPresentException – if return type refers to a non-existent type declaration. 
  • MalformedParameterizedTypeException – if the underlying return type refers to a parameterized type that cannot be instantiated for any reason.

Examples:  

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

Below programs illustrates getGenericReturnType() method of Method class 

Program 1: Print Return type of Method Object by applying getGenericReturnType() on a method. 

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");
    }
}


Output: 

Method Name : setManyValues
Return Type Details: void

Method Name : getValue
Return Type Details: class java.lang.String

Method Name : setValue
Return Type Details: int

 

Program 2: Giving a Return parameter type as input to check whether method object have same return type or not.

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;
    }
}


Output: 

setValue() contains int return type: false
setManyValues() contains int return type: true

 

Reference: 
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericReturnType–
 

RELATED ARTICLES

Most Popular

Recent Comments