當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。