Java Constructor getAnnotatedReturnType()方法及示例

Java Constructor getAnnotatedReturnType()方法及示例

构造函数 类的 getAnnotatedReturnType() 方法用于返回一个AnnotatedType对象,该对象代表AnnotatedType,用于指定构造函数对象的返回类型。返回的AnnotatedType代表AnnotatedType本身的实现或其任何子接口,如AnnotatedArrayType、AnnotatedParameterizedType、AnnotatedTypeVariable、AnnotatedWildcardType。AnnotatedType表示任何类型的潜在注释使用,包括数组类型、参数化类型、类型变量或当前在Java虚拟机中运行的通配符类型。

语法

public AnnotatedType getAnnotatedReturnType()

参数: 此方法不接受任何东西。

返回值: 该方法返回一个 AnnotatedType的对象 ,代表该Executable所代表的方法或构造函数的返回类型。

下面的程序说明了getAnnotatedReturnType()方法:

程序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 {
}

输出。

Type: Demo
Annotations: [@customAnnotatedType()]

程序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 {
}

输出。

Type: Shape
Annotations: [@ShapeProperties()]

参考文献: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getAnnotatedReturnType()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程