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


Java Constructor getAnnotation()用法及代碼示例


如果存在這樣的注釋,則使用Constructor類的getAnnotation()方法為指定的類型獲取此構造函數對象注釋,否則為null。指定的類型作為參數傳遞。

用法:

public <T extends Annotation> T
  getAnnotation(Class<T> annotationClass)

參數:此方法接受單個參數注解類,該參數表示與注解類型相對應的Class對象。


返回值:如果此元素上存在指定的注釋類型,則此方法返回該元素的注釋,否則返回null。

異常:如果給定的注釋類為null,則此方法引發NullPointerException。

以下示例程序旨在說明getAnnotation()方法:
示例1:

// Java program to demonstrate 
// Constructor.getAnnotation() method 
  
import java.lang.annotation.Annotation; 
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; 
  
public class GFG { 
  
    public static void main(String... args) 
        throws NoSuchMethodException 
    { 
  
        // Create Constructor Object 
        Constructor[] constructors 
            = Demo.class.getConstructors(); 
  
        // Create annotation object 
        Annotation annotation 
            = constructors[0] 
                  .getAnnotation(PathVar.class); 
  
        if (annotation instanceof PathVar) { 
  
            PathVar customAnnotation 
                = (PathVar)annotation; 
            System.out.println( 
                "Path: "
                + customAnnotation.Path()); 
        } 
    } 
} 
  
// Demo class 
class Demo { 
    public Demo(@PathVar(Path = "Demo/usr") 
                String str) {} 
} 
  
// PathVar interface 
@Target({ ElementType.TYPE_USE }) 
@Retention(RetentionPolicy.RUNTIME) 
@interface PathVar { 
    public String Path(); 
}

輸出:

Path: Demo/usr

示例2:

// Java program to demonstrate 
// Constructor.getAnnotation() method 
  
import java.lang.annotation.Annotation; 
import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
import java.lang.reflect.Constructor; 
  
public class GFG { 
  
    public static void main(String... args) 
        throws NoSuchMethodException 
    { 
  
        // Create Constructor Object 
        Constructor[] constructors 
            = Maths.class.getConstructors(); 
  
        // Create annotation object 
        Annotation annotation 
            = constructors[0] 
                  .getAnnotation(Calculator.class); 
  
        System.out.println( 
            "Annotation:"
            + annotation.toString()); 
    } 
} 
  
// Demo class 
@Calculator(add = "Adding value", 
            subtract = "Subtracting Value") 
class Maths { 
  
    @Calculator(add = "Adding value", 
                subtract = "Subtracting Value") 
    public Maths() {} 
} 
  
// Calculator interface 
@Retention(RetentionPolicy.RUNTIME) 
@interface Calculator { 
    public String add(); 
    public String subtract(); 
}

輸出:

Annotation : @Calculator(add=Adding value, subtract=Subtracting Value)

參考:https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getAnnotation(java.lang.Class)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Constructor getAnnotation() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。