@Documented
@Documented
用于描述其它类型的annotation应该被作为被标注的程序成员的公共API。
如下代码
@Documented //添加文档注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationTest {
}
@Inherited:
@Inherited
元注解是一个标记注解, @Inherited
阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited
修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited //添加继承注解
public @interface AnnotationTest {
}
@Repeatable
这个是在java8添加的注解特性,@Repeatable
的值表示可重复注释类型的包含注释类型。
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Repeatables {
RepeatablesTest[] value();
}
@Repeatable(Repeatables.class)
public @interface RepeatablesTest {
String value() default “哈哈哈”;
}
例子
==
我们简单的介绍了注解的常用的5个注解元,解下来我们通过例子来实现注解。
代码如下,根据上面的提示我们进行测试。
在看例子之前我们先来了解一下常用的几个方法。
-
Class.getAnnotations() 获取所有的注解,包括自己声明的以及继承的
-
Class.getAnnotation(Class< A > annotationClass) 获取指定的注解,该注解可以是自己声明的,也可以是继承的
-
Class.getDeclaredAnnotations() 获取自己声明的注解
-
Class.getDeclaredField(String name); //获取该类的声明字段
-
Class.getDeclaredMethods();//返回的是一个Method[]数组
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationTest {
String name() default “小明”; //表示默认为小明。
}
@AnnotationTest() //在该类添加注解
public class TestAnnotationMain {
public static void main(String[] args) {
boolean hasAnnotation = TestAnnotationMain.class.isAnnotationPresent(AnnotationTest.class);
if (hasAnnotation) {
AnnotationTest annotation = TestAnnotationMain.class.getAnnotation(AnnotationTest.class);
System.out.println(annotation.name());
}
}
}
打印输出结果:
如果我们想更改name的值可以这么弄
@AnnotationTest(name = “小刚”)
public class TestAnnotationMain {
public static void main(String[] args) {
boolean hasAnnotation = TestAnnotationMain.class.isAnnotationPresent(AnnotationTest.class);
if (hasAnnotation) {
AnnotationTest annotation = TestAnnotationMain.class.getAnnotation(AnnotationTest.class);
System.out.println(annotation.name());
}
}
}
如果我们想给一个类的属性进行赋值可以这么做
1.创建一个注解如下
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationTest1 {
String value(); //value来定义
}
2.引用该主解
public class Test {
@AnnotationTest1(value = “小云”) //引用注解进行赋值
public String name;
}
3.测试
public class TestAnnotation1Main {
public static void main(String[] args) {
try {
Field name = Test.class.getDeclaredField(“name”);//该该类的字段
name.setAccessible(true);
AnnotationTest1 annotationTest1 = name.getAnnotation(AnnotationTest1.class);//获取该字段的注解
if (annotationTest1 != null) {
System.out.println(annotationTest1.value()); //输出值
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
获取方法上的注解类 如AnnotationTest2
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationTest2 {
//todo无任何方法或者属性
}
public class Test {
@AnnotationTest1(value = “小云”)
public String name;
@AnnotationTest2 //目的获取该AnnotationTest2
public void fun() {
System.out.println(“方法执行”);
}
}
获取
public class TestAnnotation1Main {
public static void main(String[] args) {
try {
Field name = Test.class.getDeclaredField(“name”); //获取该类的声明字段
name.setAccessible(true);
AnnotationTest1 annotationTest1 = name.getAnnotation(AnnotationTest1.class);//获取该字段的注解
if (annotationTest1 != null) {
System.out.println(annotationTest1.value()); //输出值
}
Method fun = Test.class.getDeclaredMethod(“fun”);
if (fun != null) {
Annotation[] annotations = fun.getAnnotations();
for (int i = 0; i < annotations.length; i++) {
System.out.println(annotations[i].annotationType().getSimpleName());
}
}
} catch (NoSuchFieldException | NoSuchMethodException e) {
e.printStackTrace();
}
}
}
举例
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
public class MyTest { //进行获取注解方法的全部数据
@MyAnnotation
public void mytestLoad() {
System.out.println(“测试加载”);
}
@MyAnnotation
public void mytestRequest() {
System.out.println(“测试请求”);
}
@MyAnnotation
public void mytestProgress() {
System.out.println(“测试进度”);
}
@MyAnnotation
public void mytestError() {
System.out.println(1 );
}
///该方法不执行
public void mytestNoAnno(){
System.out.println(“没有注解的方法”);
}
}
public class TestMain {
public static void main(String[] args) {
MyTest myTest = new MyTest();
Method[] methods = myTest.getClass().getDeclaredMethods();
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
助。**
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-1446XIZz-1715756402423)]
[外链图片转存中…(img-3MJMWene-1715756402423)]
[外链图片转存中…(img-mMya9ogt-1715756402423)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!