测试代码:
package test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
@Mul("a")
@Mul("b")
public class AnnotationCheck {
public static void main(String[] args) {
if (AnnotationCheck.class.isAnnotationPresent(Muls.class)) {
Muls muls = AnnotationCheck.class.getAnnotation(Muls.class);
Arrays.asList(muls.value()).forEach(
mul -> System.out.print(mul.value() + " "));
}
System.out.println("\nmul annotation : "
+ AnnotationCheck.class.isAnnotationPresent(Mul.class));
}
}
@Repeatable(Muls.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Mul {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Muls {
Mul[] value();
}
输出:
a b
mul annotation : false