当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Constructor getAnnotatedReturnType()用法及代码示例


构造函数类的getAnnotatedReturnType()方法用于返回AnnotatedType对象,该对象表示AnnotatedType以指定构造函数对象的返回类型。返回的AnnotatedType表示AnnotatedType本身或其任何sub-interfaces的实现,例如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()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Constructor getAnnotatedReturnType() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。