Saturday, October 25, 2025
HomeLanguagesJavaMethod Class | getAnnotatedReturnType() method in Java

Method Class | getAnnotatedReturnType() method in Java

The java.lang.reflect.Method.getAnnotatedReturnType() method returns an AnnotatedType object which represents AnnotatedType to specify return type of Method Object. If Method object is created for a constructor, then AnnotatedType object specifies the type of the constructed object. If Method object is created for a method, the AnnotatedType object specifies the use of a type to specify the return type of the method specified in the source code.

The returned AnnotatedType represents implementation of AnnotatedType itself or any of its sub-interfaces like AnnotatedArrayType, AnnotatedParameterizedType, AnnotatedTypeVariable, AnnotatedWildcardType. AnnotatedType represents the potentially annotated use of any type including an array type, a parameterized type, a type variable, or a wildcard type currently running in Java Virtual Machine.

Syntax:

public AnnotatedType getAnnotatedReturnType()

Return Value: This method returns an AnnotatedType object which represents AnnotatedType to specify return type of Method Object.

Below programs illustrates getAnnotatedReturnType() method of Method class:

Example 1:Use AnnotatedType() for a specific method given as Input.

The program contains a Method name getAddressMethod which contains AnnotatedType. So this program is going to get details of AnnotatedType, contained by getAddressMethod Method.




// Java program to demonstrate how to
// apply getAnnotatedReturnType() method
// of Method Class.
  
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.util.Arrays;
  
public class GFG {
  
    // Creating custom AnnotatedType
    @Target({ ElementType.TYPE_USE })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface customAnnotatedType {
    }
  
    // a sample method with return type String and
    // AnnotatedType is @customAnnotatedType
    public @customAnnotatedType String getAddress()
    {
        return null;
    }
  
    // main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class classobj = GFG.class;
  
            // create method object of getAddress
            Method getAddressMethod = null;
  
            Method[] methods = classobj.getMethods();
            for (Method m : methods) {
                if (m.getName().equals("getAddress"))
                    getAddressMethod = m;
            }
  
            // get AnnotatedType for return type
            AnnotatedType annotatedType = getAddressMethod
                                              .getAnnotatedReturnType();
  
            // print AnnotatedType details with Method name
            System.out.println("Method Name: "
                               + getAddressMethod.getName());
  
            System.out.println("Type: "
                               + annotatedType.getType().getTypeName());
  
            System.out.println("Annotations: "
                               + Arrays.toString(annotatedType.getAnnotations()));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Output:

Method Name: getAddress
Type: java.lang.String
Annotations: [@GFG$customAnnotatedType()]

Example 2: To print Annotation on array type or multidimensional array or Generic for Methods of class GFG.




// Java program to demonstrate how to
// apply getAnnotatedReturnType() method
// of Method Class.
  
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.List;
  
public class systemUTCMethodDemo<T> {
  
    // Creating custom AnnotatedType
    @Target({ ElementType.TYPE_USE })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface customAnnotatedType {
    }
  
    // a sample method with Annotation on array type
    public @customAnnotatedType String[] getAddress()
    {
        return null;
    }
  
    // a sample method on Annotation on multidimensional array
    public String[] @customAnnotatedType[] getvalues()
    {
        return null;
    }
  
    // a sample method on Annotation on a type with generic
    public @customAnnotatedType List<T> getWords()
    {
        return null;
    }
  
    // main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class classobj = systemUTCMethodDemo.class;
  
            // create method object of getAddress and getValues
            Method[] methods = classobj.getMethods();
  
            for (Method m : methods) {
  
                // if method object is for getAddress and getValues
                // then print @customAnnotatedType for both
                if (m.getName().equals("getAddress")
                    || m.getName().equals("getvalues")
                    || m.getName().equals("getWords")) {
                    printDetails(m);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    public static void printDetails(Method m)
    {
  
        // get AnnotatedType for return type
        AnnotatedType annotatedType = m.getAnnotatedReturnType();
  
        // print AnnotatedType details with Method name
  
        System.out.println("Method Name: " + m.getName());
        System.out.println("Type: " + annotatedType.getType().getTypeName());
        System.out.println("Annotations: " + annotatedType);
        System.out.println();
    }
}


Output:

Method Name: getWords
Type: java.util.List
Annotations: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@12a3a380

Method Name: getvalues
Type: java.lang.String[][]
Annotations: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl@29453f44

Method Name: getAddress
Type: java.lang.String[]
Annotations: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl@5cad8086

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedType.html

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS