Open In App

Constructor getAnnotatedReturnType() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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 {
}


Output:

Type: Demo
Annotations: [@customAnnotatedType()]

Program 2:




// 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 {
}


Output:

Type: Shape
Annotations: [@ShapeProperties()]

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



Last Updated : 25 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads