您的位置:首页 > 编程语言 > Java开发

java注解的简单使用

2016-05-30 15:20 507 查看

今天要来说一下java的注解api,记得以前写子类重写父类的方法时,都会看到一个@Override的东西,当时不知道这玩意是什么,现在才知道是javase5中内置的标准注解。然后第一次真正了解注解的时候是在使用xutils框架的时候,那时候觉得注解很牛逼,使用起来很方便,但是完全不知道是怎么实现的,最近因为项目用到了注解,所以重新学习了一遍,了解了一下使用过程,特地来记录一下学习的内容。废话不多说,代码是最后的老师。

java注解的简介:

java的注解是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。具体见java注解-百度百科

java注解的使用流程:

定义一个注解类:
<span style="font-family:SimSun;font-size:18px;">@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Get {
String value() default "";
}</span>

用@Target指定ElementType属性

<span style="font-family:SimSun;font-size:18px;">public enum ElementType {
// 用于类,接口,枚举但不能是注解
TYPE,
// 字段上,包括枚举值
FIELD,
// 方法,不包括构造方法
METHOD,
// 方法的参数
PARAMETER,
// 构造方法
CONSTRUCTOR,
// 本地变量或catch语句
LOCAL_VARIABLE,
// 注解类型(无数据)
ANNOTATION_TYPE,
// Java包
PACKAGE
}</span>
@Retention限制注解的使用范围,在什么地方会保留,在什么地方会失效。
<span style="font-family:SimSun;font-size:18px;">public enum RetentionPolicy {
//此类型保留在源码当中,会被编译器所丢弃
SOURCE,
//此类型注解会保留在class文件中,但JVM会忽略它
CLASS,
//此类型注解会保留在class文件中,JVM会读取它
RUNTIME
}</span>
既然定义了一个注解类,我们当然要在代码中使用它了:
<span style="font-family:SimSun;font-size:18px;">@Get("http://api/reflectannotionproxy/getData")
String getData();//具体方法名与返回值</span>
我们在程序当中使用了注解方法,那如何去读取使用了注解信息的方法呢?这个时候我们又要用到java的反射机制了:
<span style="font-family:SimSun;font-size:18px;">try{
<span style="white-space:pre">	</span>Class c = Test.class;
Method method = c.getDeclaredMethod("getData");
if(method.isAnnotationPresent(Get.class)){//判断该方法上是否有Get注解
Get get = method.getAnnotation(Get.class);//获取注解类对象
if(get != null){
String url = get.value();//获取注解类上的参数,就是那一串url
}
}catch (Exception e){
e.printStackTrace();
}</span>

通过这种方式,可以获取的这个方法的值,然后我们可以对这个值进行处理,我们还可以去声明一个方法参数注解类,如下:
<span style="font-family:SimSun;font-size:18px;">@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Parames {
String value() default "";
}</span>

这个时候我们可以把上面的代码改动一下,获取方法的值,以及参数名
<span style="font-family:SimSun;font-size:18px;">@Get("http://api/reflectannotionproxy/getData")
String getData(@Parames("type") String type, @Parames("description") String description);</span>


然后我们同样用反射的方式去获取它:
<span style="font-family:SimSun;font-size:18px;">try{
Class c = Test.class;
Method method = c.getDeclaredMethod("getData");
if(method.isAnnotationPresent(Get.class)){//判断该方法上是否有Get注解
Get get = method.getAnnotation(Get.class);//获取注解类对象
if(get != null){
String url = get.value();//获取注解类上的参数,就是那一串url
ArrayList<String> arrayList = new ArrayList<>();
Annotation[][] annotations = method.getParameterAnnotations();//获取方法参数的二位数组
if (annotations != null && annotations.length > 0) {
for (int i = 0;i<annotations.length;i++) {
Annotation[] annotations1 = annotations[i];
Log.i("info", "annotations1========" + annotations1);
for (Annotation annotation : annotations1) {
Log.i("info", "annotation========" + annotation);
if (annotation instanceof Parames) {
Parames parames = ((Parames) annotation);
arrayList.add(parames.value());//获得方法参数名("type","description"),具体参数由你调用方法时传入
}
}
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}</span>

这就是java注解常见的一些使用方法,用注解可以打造类restful风格的网络请求框架!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 注解 反射机制