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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。