java.lang.Package.getAnnotations()%uA0方法返回当前这个元素上的所有注释。 (如果这个元素没有注释返回长度为零的数组。)该方法的调用者可以随意修改返回数组这对其他调用者返回的数组产生任何影响。
声明
以下是java.lang.Package.getAnnotations()方法的声明
public Annotation[] getAnnotations()
参数
-
NA
返回值
此方法返回当前这个元素上的所有注释
异常
-
NA
例子
下面的例子显示lang.Object.getAnnotations()方法的使用。
package com.yiibai import java.lang.annotation.Annotation import java.lang.annotation.Retention import java.lang.annotation.RetentionPolicy import java.lang.reflect.Method // declare a new annotation @Retention(RetentionPolicy.RUNTIME) @interface Demo { String str() int val() } public class PackageDemo { // set values for the annotation @Demo(str = "Demo Annotation", val = 100) // a method to call in the main public static void example() { PackageDemo ob = new PackageDemo() try { Class c = ob.getClass() // get the method example Method m = c.getMethod("example") // get the annotations Annotation[] annotation = m.getAnnotations() // print the annotation for (int i = 0 i < annotation.length i++) { System.out.println(annotation[i]) } } catch (NoSuchMethodException exc) { exc.printStackTrace() } } public static void main(String args[]) { example() } }
让我们来编译和运行上面的程序,这将产生以下结果:
@com.yiibai.Demo(str=Demo Annotation, val=100)