Java Class getDeclaredAnnotationsByType() 方法

2025年3月21日 | 阅读 1 分钟

java Class 类的 getDeclaredAnnotationsByType() 方法会根据指定的类型返回该元素的注解,如果存在则直接返回,否则返回 null。它会忽略继承的注解。

语法

参数

annotationClass - 对应注解类型的 Class 对象

返回值

注解

抛出

NullPointerException

示例 1

输出

[@java.lang.Deprecated()]
[@ClassgetDeclaredAnnotationbytypeExample1$Annotation1()]

示例 2

输出

[@java.lang.Deprecated()]
[@ClassgetDeclaredAnnotationbytypeExample2$Annotation1()]
ClassgetDeclaredAnnotationbytypeExample2.subClass1
 
下一个主题Java类