Constructor getAnnotatedReturnType() method in Java with Examples

The getAnnotatedReturnType() method of a Constructor class is used to return an AnnotatedType object which represents AnnotatedType to specify return type of Constructor Object. 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()

Parameters: This method accepts nothing.



Return value: This method returns an object of AnnotatedType representing the return type of the method or constructor represented by this Executable.

Below programs illustrate the getAnnotatedReturnType() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Constructor.getAnnotatedReturnType() method
  
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.util.Arrays;
  
public class GFG {
  
    // main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class demo = Demo.class;
  
            // get Constructor object array
            // from the class object
            Constructor[] cons
                = demo.getConstructors();
  
            // get AnnotatedType for return type
            AnnotatedType annotatedType
                = cons[0]
                      .getAnnotatedReturnType();
  
            System.out.println(
"Type: "
                               + annotatedType
.getType(
.getTypeName());
  
            System.out.println(
"Annotations: "
                               + Arrays
.toString(
annotatedType
.getAnnotations()));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  
class Demo {
  
    // AnnotatedType is @customAnnotatedType
    public @customAnnotatedType Demo()
    {
        // do stuffs
    }
}
  
// Creating custom AnnotatedType
@Target({ ElementType.TYPE_USE })
@Retention(RetentionPolicy.RUNTIME)
@interface customAnnotatedType {
}

chevron_right


Output:

Type: Demo
Annotations: [@customAnnotatedType()]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Constructor.getAnnotatedReturnType() method
  
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.util.Arrays;
  
public class GFG {
  
    // main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class shape = Shape.class;
  
            // get Constructor object array
            // from the class object
            Constructor[] cons
                = shape.getConstructors();
  
            // get AnnotatedType for return type
            AnnotatedType annotatedType
                = cons[0]
                      .getAnnotatedReturnType();
  
            System.out.println(
                "Type: "
                + annotatedType
                      .getType()
                      .getTypeName());
  
            System.out.println(
                "Annotations: "
                + Arrays.toString(
                      annotatedType.getAnnotations()));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  
class Shape {
  
    // AnnotatedType is @customAnnotatedType
    public @ShapeProperties Shape()
    {
        // do stuffs
    }
}
  
// Creating custom AnnotatedType
@Target({ ElementType.TYPE_USE })
@Retention(RetentionPolicy.RUNTIME)
@interface ShapeProperties {
}

chevron_right


Output:

Type: Shape
Annotations: [@ShapeProperties()]

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getAnnotatedReturnType()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.