eclipse里面建工程, 三个source文件
package com.sap;
import java.lang.annotation.*;
@Target(ElementType.PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PkgAnnotation {
String value();
String desc();
}
@PkgAnnotation(value="anno value", desc="i am description for annotation")
package com.sap;
package com.sap;
import java.lang.annotation.*;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Package pkg = Package.getPackage("com.sap");
boolean flag = pkg.isAnnotationPresent(PkgAnnotation.class);
if (flag){
Annotation[] annos =pkg.getAnnotations();
for(Annotation an : annos){
if (an instanceof PkgAnnotation){
System.out.println(((PkgAnnotation) an).value());
System.out.println(((PkgAnnotation) an).desc());
}
}
}
}
}
输出:
anno value
i am description for annotation