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:
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 {
public static void main(String[] args)
{
try {
Class demo = Demo. class ;
Constructor[] cons
= demo.getConstructors();
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 {
public @customAnnotatedType Demo()
{
}
}
@Target ({ ElementType.TYPE_USE })
@Retention (RetentionPolicy.RUNTIME)
@interface customAnnotatedType {
}
|
Output:
Type: Demo
Annotations: [@customAnnotatedType()]
Program 2:
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 {
public static void main(String[] args)
{
try {
Class shape = Shape. class ;
Constructor[] cons
= shape.getConstructors();
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 {
public @ShapeProperties Shape()
{
}
}
@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
Share your thoughts in the comments
Please Login to comment...