Java.lang.Class.getAnnotations() 方法
描述
java.lang.Class.getAnnotations() 方法返回此元素上存在的所有注释。 如果此元素没有注释,则返回一个长度为零的数组。 该方法的调用者可以随意修改返回的数组; 它不会影响返回给其他调用者的数组。
声明
以下是 java.lang.Class.getAnnotations() 方法的声明。
public Annotation[] getAnnotations()
参数
NA
返回值
此方法返回此元素上存在的所有注释。
异常
NA
示例
下面的例子展示了 java.lang.Class.getAnnotations() 方法的使用。
package com.tutorialspoint;
import java.lang.annotation.Annotation;
public class ClassDemo {
public static void main(String []args) {
ClassDemo cls = new ClassDemo();
Class c = cls.getClass();
Annotation[] a = c.getAnnotations();
if(a.length != 0) {
for(Annotation val : a) {
System.out.println(val.toString());
}
} else {
System.out.println("Annotations is not present...");
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Annotations is not present...

