自定义注解(Annotation)
来源:互联网 发布:stc89c51rc单片机简介 编辑:程序博客网 时间:2024/06/09 16:07
没有系统的学习过Java,对注解的认识单一只停留在“重载@Override
”和“弃用@Deprecated
”这两个上,最近需要对Dragger2分析,里面涉及了一些自定义注解。如下先给我写的一个自定义注解的Demo,最后会列出我编写demo时,遗漏的一些点。
自定义注解
package com.test;import java.lang.annotation.ElementType;import java.lang.annotation.Target;import java.lang.annotation.Retention;import java.lang.annotation.Documented;import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)@Target(value={ElementType.ANNOTATION_TYPE, ElementType.FIELD})@Documentedpublic @interface MethodInfo1 { String name1() default ""; int version1() default 1;}
package com.test;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)public @interface MethodInfo2 { String name2(); int version2();}
- 定义注解和接口定义类似,通过在interface前面加一个@,如:@interface
- 属性是一个一个无参数的函数命名方式,返回值是这个属性的类型
- 通过default设置属性的默认值,如:String name1() default “name_test”;
注解使用
package com.test;public class AnnotationTest { @MethodInfo1(name1 = "111111111111", version1 = 1) String name1; @MethodInfo2(name2 = "2222222222222", version2 = 2) String name2;}
在属性、方法、类等上使用注解
注解只与类有关,与类对象无关
注解反射调用
private static void func(){ try { Class<?> annotationClass = AnnotationTest.class;//Class.forName("com.test.AnnotationTest"); Field[] annotationFields = annotationClass.getDeclaredFields(); System.out.println("annotationFields " + annotationFields[0]); for(Field field : annotationFields) { if (field.isAnnotationPresent(MethodInfo1.class)) { MethodInfo1 methodInfo1 = field.getAnnotation(MethodInfo1.class); System.out.println("name1:" + methodInfo1.name1() + ", version1:" + methodInfo1.version1()); } else if (field.isAnnotationPresent(MethodInfo2.class)) { MethodInfo2 methodInfo2 = field.getAnnotation(MethodInfo2.class); System.out.println("name2:" + methodInfo2.name2() + ", version2:" + methodInfo2.version2()); } } } catch (Exception e) { e.printStackTrace(); } }
结果:
annotationFields java.lang.String com.test.AnnotationTest.name1
name1:key_name, version1:1
name2:2222222222222, version2:2
注意事项:
- 自定义注解一定要要加上“
@Retention(RetentionPolicy.RUNTIME)
”修饰,表示运行时加载,否则反射不到内容- 获取属性时注意方法的调用,
Field[] annotationFields = annotationClass.getDeclaredFields();
,此处是获取属性,函数也有类似方法调用。
0 0
- 自定义Annotation(注解)
- 自定义注解(Annotation)
- 注解(Annotation)自定义注解
- 自定义注解(Annotation)入门
- Annotation(注解)_注解的自定义
- Java:注解(Annotation)自定义注解入门
- 注解(Annotation)自定义注解入门
- Java注解 (Annotation)自定义注解入门
- 注解(Annotation)自定义注解入门
- Java:注解(Annotation)自定义注解入门
- Annotation(自定义注解)反射获取注解
- 注解(Annotation)自定义注解入门
- 注解(Annotation)自定义注解入门
- 注解(Annotation)自定义注解入门
- 注解(Annotation)自定义注解入门
- 注解(Annotation)自定义注解入门
- JAVA 注解(Annotation)自定义注解入门
- java注解(Annotation)自定义注解入门
- sort命令以及uniq命令
- Android 音乐盒完整版
- leetcode 564. Find the Closest Palindrome
- Android Studio引入第三方框架的方法总结
- @EnableAutoConfiguration
- 自定义注解(Annotation)
- 矩阵求导简要笔记
- HDU 5976 Detachment 逆元
- epoll 或者 kqueue 的原理(摘自知乎蓝形参)
- iOS崩溃日志收集
- java中讲讲PrintStream的用法,举例?
- ACM_HDU 2031 进制转换
- JS比较运算符及null和Undefined区别
- golang实现ringbuffer(不定长)