您的位置:首页 > 其它

三、自定义注解

2016-06-13 13:20 176 查看
注解的使用我就不讲了.这里讲讲自定义注解吧.学习自定义注解之前,要先了解几个元注解(用来修饰注解的注解=.=)

元注解
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息