写在前面
关于java注解的基础内容这里就不多进行解释,直接演示工作中注解不经常使用到的功能
Inherited说明
在注解上注解上@Inherited的作用是什么呢?从字面意思可以知道是继承,具体作用直接代码演示
不使用Inherited注解的情况
- 首先定义一个注解Littlehow,注解上先不加Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Littlehow {
String value();
}
- 定义测试类
public class Test {
public static void main(String[] args) {
Annotation[] annotations = BB.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
}
}
}
}
@Littlehow("this littlehow is AA")
class AA {
}
class BB extends AA {
}
运行测试类Test发现BB上并没有出现注解Littlehow
使用Inherited的情况(一)
- 首先定义一个注解Littlehow,加Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public @interface Littlehow {
String value();
}
- 测试类与【不使用Inherited注解的情况】的测试类保持一致
运行将输出
this littlehow is AA
使用Inherited的情况(二)
-
Littlehow注解与【使用Inherited的情况(一)】的Littlehow注解保持一致
-
测试类如下:
public class TestB {
public static void main(String[] args) {
Annotation[] annotations = BB.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
}
}
}
}
@Littlehow("this littlehow is AA")
class AA {
}
/**
* 此时BB注解上加上了注解Littlehow
*/
@Littlehow("this littlehow is BB")
class BB extends AA {
}
运行将输出
this littlehow is BB
小结
注解Inherited表示注解的继承性,
1.当子类无注解时,会继承父类的注解;
2.当子类有注解时,将使用子类自己的注解;
Repeatable说明
在注解上注解上@Repeatable的作用是什么呢?从字面意思可以知道是重复,具体作用直接代码演示
不使用Repeatable注解的情况
-
Littlehow注解与【使用Inherited的情况(一)】的Littlehow注解保持一致
-
定义测试类
public class Test {
public static void main(String[] args) {
Annotation[] annotations = AA.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
}
}
}
}
@Littlehow("this littlehow is AA1")
@Littlehow("this littlehow is AA2")
class AA {
}
编译异常,无法进行编译,提示Littlehow不是一个合法的java.lang.annotation.Repeatable注解
使用Repeatable的情况(无Inherited)
- 定义注解(这里需要定义两个注解)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//当有重复时,使用哪个注解来进行重复注解的收录
@Repeatable(Littlehows.class)
public @interface Littlehow {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Littlehows {
Littlehow[] value();
}
- 定义测试类
public class Test {
public static void main(String[] args) {
Annotation[] annotations = AA.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println(((Littlehow) annotation).value());
} else if (Littlehows.class.equals(type)) {
System.out.println("进入重复注解中");
Littlehows littlehows = (Littlehows) annotation;
for (Littlehow littlehow : littlehows.value()) {
System.out.println(littlehow.value());
}
}
}
}
}
@Littlehow("this littlehow is AA1")
@Littlehow("this littlehow is AA2")
class AA {
}
运行将输出:
进入重复注解中
this littlehow is AA1
this littlehow is AA2
使用Repeatable的情况(有Inherited)
- 定义注解(这里需要定义两个注解)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
//当有重复时,使用哪个注解来进行重复注解的收录
@Repeatable(Littlehows.class)
public @interface Littlehow {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public @interface Littlehows {
Littlehow[] value();
}
- 定义测试类(父类两个相同注解)
public class Test {
public static void main(String[] args) {
Annotation[] annotations = BB.class.getAnnotations();
for (Annotation annotation : annotations) {
Class type = annotation.annotationType();
if (Littlehow.class.equals(type)) {
System.out.println("进入单个注解中");
System.out.println(((Littlehow) annotation).value());
} else if (Littlehows.class.equals(type)) {
System.out.println("进入重复注解中");
Littlehows littlehows = (Littlehows) annotation;
for (Littlehow littlehow : littlehows.value()) {
System.out.println(littlehow.value());
}
}
}
}
}
@Littlehow("this littlehow is AA1")
@Littlehow("this littlehow is AA2")
class AA {
}
@Littlehow("this littlehow is BB")
class BB extends AA {
}
运行将输出:
进入重复注解中
this littlehow is AA1
this littlehow is AA2
进入单个注解中
this littlehow is BB
- 定义测试类,重写AA类(父类一个相同注解)
@Littlehow("this littlehow is AA")
class AA {
}
@Littlehow("this littlehow is BB")
class BB extends AA {
}
运行将输出:
进入单个注解中
this littlehow is BB
小结
Repeatable作用为定义可重复注解的注解,当同一个类上有相同注解时,将合并为一个复合注解;当设置可继承时,子类和父类各一个可重复注解不会被合成;
Repeatable作用的时机:当同一个类上同时注解多个可重复注解时,在编译时就会将其合并;所以运行时获取到的是复合注解。
写在后面
littlehow 写于 2020/05/29