📜  Java中的构造函数 getAnnotatedReturnType() 方法及示例

📅  最后修改于: 2022-05-13 01:55:21.116000             🧑  作者: Mango

Java中的构造函数 getAnnotatedReturnType() 方法及示例

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

句法:

public AnnotatedType getAnnotatedReturnType()

参数:此方法不接受任何内容。

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

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

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

方案二:

// 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: Java/lang/reflect/Constructor.html#getAnnotatedReturnType()