Thursday, June 11, 2026
HomeLanguagesJavaConstructor getAnnotatedReturnType() method in Java with Examples

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:




// 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()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS