三、自定义注解
2016-06-13 13:20
176 查看
注解的使用我就不讲了.这里讲讲自定义注解吧.学习自定义注解之前,要先了解几个元注解(用来修饰注解的注解=.=)
元注解
Target
作用
源代码
参数
Document
作用
源代码
参数
Retention
作用
源代码
参数
Inherited
作用
源代码
参数
自定义注解
最简单注解
带参数的注解
读取注解信息
由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等
SOURCE:在源文件中有效(即源文件保留)
CLASS:在class文件中有效(即class保留)
RUNTIME:在运行时有效(即运行时保留)
被注解的类
测试
运行结果
谢谢http://blog.csdn.net/yixiaogang109/article/details/7328466
元注解
Target
作用
源代码
参数
Document
作用
源代码
参数
Retention
作用
源代码
参数
Inherited
作用
源代码
参数
自定义注解
最简单注解
带参数的注解
读取注解信息
元注解
@Target
作用
用来标注注解的作用域源代码
@Documented //可以包含在文档中 @Retention(RetentionPolicy.RUNTIME) //annotaion保留级别 @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
参数
@Target(ElementType.TYPE) //接口、类、枚举、注解 @Target(ElementType.FIELD) //字段、枚举的常量 @Target(ElementType.METHOD) //方法 @Target(ElementType.PARAMETER) //方法参数 @Target(ElementType.CONSTRUCTOR) //构造函数 @Target(ElementType.LOCAL_VARIABLE)//局部变量 @Target(ElementType.ANNOTATION_TYPE)//注解 @Target(ElementType.PACKAGE) ///包
由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等
@Document
作用
说明该注解将被包含在javadoc中源代码
@Documented @Retention(RetentionPolicy.RUNTIME)//在class中可反射得到 @Target(ElementType.ANNOTATION_TYPE)//修饰注解类型 public @interface Documented { }
参数
无@Retention
作用
注解的保留级别源代码
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
参数
取值java.lang.annotation.RetentionPolicy
SOURCE:在源文件中有效(即源文件保留)
CLASS:在class文件中有效(即class保留)
RUNTIME:在运行时有效(即运行时保留)
@Inherited
作用
说明子类可以继承父类中的该注解源代码
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Inherited { }
参数
无自定义注解
最简单注解
//注解 public @interface SimpleAnnotation { } //被注解的类 @SimpleAnnotation public class AnnotationTest { }
带参数的注解
//注解 @Target(ElementType.METHOD) //作用于方法 public @interface ParamAnnotation { String[] value1() default "abc"; String valuxxx() default "hello world"; } //被注解的类 @SimpleAnnotation public class AnnotationTest { @ParamAnnotation public void setName(){} }
读取注解信息
public class ParamExplain { //方法注解 public static <T> void parseMethodAnnotation(Class<T> clazz) { T obj = null; try { obj = clazz.newInstance(); for (Method method : clazz.getDeclaredMethods()) { ParamAnnotation methodAnnotation = method.getAnnotation(ParamAnnotation.class); if (methodAnnotation != null) { //通过反射调用带有此注解的方法 Log.e("value1=", methodAnnotation.valuxxx()); } } } catch (Exception e) { e.printStackTrace(); } } //获得类注解 public static <T> void parseClassAnnotation(Class<T> clazz) { T obj = null; try { obj = clazz.newInstance(); SimpleAnnotation simpleAnnotation = (SimpleAnnotation) clazz.getAnnotation(SimpleAnnotation.class); if (simpleAnnotation != null) { Log.e("simpleAnnotation=", "simple annotation find"); } } catch (Exception e) { e.printStackTrace(); } } }
被注解的类
@SimpleAnnotation public class AnnotationTest { @ParamAnnotation(valuxxx = "Not default") public void setName(){} }
测试
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.test_tv); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ParamExplain.parseMethodAnnotation(AnnotationTest.class); ParamExplain.parseClassAnnotation(AnnotationTest.class); } }); } }
运行结果
value1=: Not default simpleAnnotation=: simple annotation find
谢谢http://blog.csdn.net/yixiaogang109/article/details/7328466
相关文章推荐
- Java反射随记
- AS3中的反射(速记) 分析
- C# 反射(Reflection)的用处分析
- C#通过反射创建自定义泛型
- 通俗易懂的C#之反射教程
- C#根据反射和特性实现ORM映射实例分析
- 深入反射生成数组的详解
- C#利用反射来判断对象是否包含某个属性的实现方法
- C#利用反射技术实现去掉按钮选中时的边框效果
- C#泛型和反射实例解析
- C# Dynamic关键字之:dynamic为什么比反射快的详解
- 传智播客学习之java 反射
- java中利用反射调用另一类的private方法的简单实例
- Java 反射之私有字段和方法详细介绍
- php使用反射插入对象示例分享
- java反射使用示例分享
- java利用反射实现动态代理示例
- 用户控件(ASCX)向网页(ASPX)传值使用反射实现
- 基于Java回顾之反射的使用分析
- 详解JDK 5 Annotation 注解之@Target的用法介绍