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
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = demoForReturnParam. class ;
Method[] methods = classobj.getMethods();
for (Method method : methods) {
if (method.getName().equals( "setValue" )
|| method.getName().equals( "getValue" )
|| method.getName().equals( "setManyValues" )) {
Type returnParam = method.getGenericReturnType();
System.out.println( "\nMethod Name : "
+ method.getName());
System.out.println( "Return Type Details: "
+ returnParam);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class demoForReturnParam {
public int setValue()
{
System.out.println( "setValue" );
return 24 ;
}
public String getValue()
{
System.out.println( "getValue" );
return "getValue" ;
}
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
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = DemoForReturnParam. class ;
Method[] methods = classobj.getMethods();
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);
}
}
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();
}
}
private static boolean
containsReturnParameter(Method method, Type parameterName)
{
Type returnParameter = method.getGenericReturnType();
if (returnParameter == parameterName) {
return true ;
}
return false ;
}
}
class DemoForReturnParam {
public void setValue()
{
System.out.println( "setValue" );
}
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–
Last Updated :
21 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...