包级注释..以下是一个例子:Package[] packages = Package.getPackages();for (Package p : packages) {
MyPackageAnnotation annotation = p.getAnnotation(MyPackageAnnotation.class);
if (annotation != null) {
Class>[] implementations = annotation.implementationsOfPet();
for (Class> impl : implementations) {
System.out.println(impl.getSimpleName());
}
}}
以及注释定义:@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.PACKAGE)public @interface MyPackageAnnotation {
Class>[] implementationsOfPet() default {};}
并且必须在名为package-info.java在那个包裹里。以下是样本内容:@MyPackageAnnotation(implementationsOfPet = {Dog.class, Cat.class})package examples.reflections;
注意,只有ClassLoader知道的包才会通过调用Package.getPackages().