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


Java Field getAnnotatedType()用法及代码示例


java.lang.reflect.Field的getAnnotatedType()方法用于返回annonatedType对象,该对象表示使用类型指定字段的声明类型。该类的每个字段都由某些AnnotatedType声明。AnnotatedType表示可能注释的任何类型的使用,包括当前在Java虚拟机中运行的数组类型,参数化类型,类型变量或通配符类型。返回的AnnotatedType实例可以是AnnotatedType本身的实现,也可以是其sub-interfaces之一的实现:AnnotatedArrayType,AnnotatedParameterizedType,AnnotatedTypeVariable,AnnotatedWildcardType。

用法:

public AnnotatedType getAnnotatedType()

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


返回:此方法返回一个对象,该对象表示此Field表示的字段的声明类型。

以下示例程序旨在说明getAnnotatedType()方法:
示例1:

// Java program to illustrate 
// getAnnotatedType() method 
  
import java.lang.reflect.AnnotatedType; 
import java.lang.reflect.Field; 
import java.util.Arrays; 
  
public class GFG { 
  
    private int number; 
  
    public static void main(String[] args) 
        throws NoSuchFieldException 
    { 
  
        // get Field object 
        Field field 
            = GFG.class
                  .getDeclaredField("number"); 
  
        // apply getAnnotatedType() method 
        AnnotatedType annotatedType 
            = field.getAnnotatedType(); 
  
        // print the results 
        System.out.println( 
            "Type: "
            + annotatedType 
                  .getType() 
                  .getTypeName()); 
        System.out.println( 
            "Annotations: "
            + Arrays 
                  .toString( 
                      annotatedType 
                          .getAnnotations())); 
    } 
}
输出:
Type: int
Annotations: []

示例2:

// Java Program to illustrate 
// getAnnotatedType() method 
  
import java.lang.annotation.*; 
import java.lang.reflect.*; 
import java.util.Arrays; 
  
public class GFG { 
  
    private int @SpecialNumber[] number; 
  
    public static void main(String[] args) 
        throws NoSuchFieldException 
    { 
        // get Field object 
        Field field 
            = GFG.class
                  .getDeclaredField("number"); 
  
        // apply getAnnotatedType() method 
        AnnotatedType annotatedType 
            = field.getAnnotatedType(); 
  
        // print the results 
        System.out.println( 
            "Type: "
            + annotatedType 
                  .getType() 
                  .getTypeName()); 
        System.out.println( 
            "Annotations: "
            + Arrays 
                  .toString( 
                      annotatedType 
                          .getAnnotations())); 
        System.out.println( 
            "Declared Annotations: "
            + Arrays 
                  .toString( 
                      annotatedType 
                          .getDeclaredAnnotations())); 
    } 
  
    @Target({ ElementType.TYPE_USE }) 
    @Retention(RetentionPolicy.RUNTIME) 
    private @interface SpecialNumber { 
    } 
}
输出:
Type: int[]
Annotations: [@GFG$SpecialNumber()]
Declared Annotations: [@GFG$SpecialNumber()]

参考文献: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getAnnotatedType–



相关用法


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