Retrofit源码分析 (一.基础知识annotation认识)

来源:互联网 发布:js取消页面滚动条 编辑:程序博客网 时间:2024/05/21 11:04

元注解

Java 定义了4个标准的meta-annotation类型(在java.lang.annotation包里,可阅读所有的注解相关信息)
1. @Target
2. @Retention
3. @Documented
4. @Inherited

(1)Target注解在枚举java.lang.annotation.ElementType 中有很多字段,如下

1.CONSTRUCTOR:用于描述构造器2.FIELD:用于描述域3.LOCAL_VARIABLE:用于描述局部变量4.METHOD:用于描述方法5.PACKAGE:用于描述包6.PARAMETER:用于描述参数7.TYPE:用于描述类、接口(包括注解类型) 或enum声明

(2)Retention注解在枚举java.lang.annotation.RetentionPolicy中有很3个字段。

1.SOURCE:在源文件中有效(即源文件保留)2.CLASS::在class文件中有效(即class保留)3.RUNTIME:在运行时有效(即运行时保留)

(3)Documented 是一个标记注解,没有成员。被标注为公共API,用javadoc此类的工具文档化的。

(4)Inherited 是一个标记注解,阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修
饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。对此找了一篇技术文章。
非常不错,关于java 注解中元注解Inherited的使用详解

以上就是基础知识的准备,下面代码解释一下相关内容。

写一个注解RetrofitFM 类。

package com.rulang;import java.lang.annotation.*;/** * Created by rulang on 2017/8/24. */@Target({ElementType.METHOD,ElementType.FIELD})      // 表示只能注释在字段上面哦@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到@Documented@interface RetrofitFM {    // 对于这个value我要说明一下,也是我当初很郁闷的地方    //为什么大多说注解都有value呢?    /**     当注解中使用的属性名为value时,对其赋值时可以不指定属性的名称而直接写上属性值接口;     除了value意外的变量名都需要使用name=value的方式赋值。     @interface Rulang{     String value();     }     @Rulang("Rulang")     public void likeDoSomething() {}     */    String value() default "";    public enum RetrofitType{ Get,POST,DELETE};    RetrofitType retrofittype() default RetrofitType.POST;}

再写一个注解类RetrofitPost

package com.rulang;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Created by rulang on 2017/8/24. */@Target(ElementType.TYPE)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到//如果不写的话,讲无法获取到字节码,就不能判断他是不是注解呢@Retention(RetentionPolicy.RUNTIME) public  @interface RetrofitPost {    String value() default "";}

注解已经准备好了,写一个实体类,用上面的注解对其注解。

package com.rulang;/** * Created by rulang on 2017/8/24. */@RetrofitPost("rulang")public class RetrofitFactory {    @RetrofitFM("RetrofitFactory")    private String mName;    @RetrofitFM(retrofittype= RetrofitFM.RetrofitType.DELETE)    public void goHome() {    }}

注解和实体都有了,最后就是解析注解了。

package com.rulang;import java.lang.reflect.Field;import java.lang.reflect.Method;/** * Created by rulang on 2017/8/24. * * 有注解,肯定有解析。 * 不然注解也没什么卵用 * */public class RetrofitParser {    /**     * 测试和解析相关数据     */    public static void main(String[] jiexieQi) {        //取到想要处理的类,得到字节码。注解和反射联系很紧密,下一章直接是讲注解哦。         Class clz=RetrofitFactory.class;        /**         * 这类式于扫描Class是不是某个注解,然后做想应的功能处理         */        boolean isAto= clz.isAnnotation();        System.out.println(isAto); // 打印:false  说明RetrofitFactory 不是个注解类哦         boolean isRetrofitFactory=clz.isAnnotationPresent(RetrofitPost.class);         System.out.println(isRetrofitFactory); // 打印:true 说明RetrofitFactory是RetrofitPost直接哦        RetrofitPost mRetrofitPost= (RetrofitPost) clz.getAnnotation(RetrofitPost.class);        System.out.println(mRetrofitPost.value()); // 打印:rulang        Field[] fields = clz.getDeclaredFields();// 获取当前类的所有字段        for(Field field :fields){            if(field.isAnnotationPresent(RetrofitFM.class)){                RetrofitFM retrofitfm = (RetrofitFM) field.getAnnotation(RetrofitFM.class);                // 之所有有POST,那是因为默认为POST,哈哈                System.out.println(retrofitfm.value() +" - " +retrofitfm.retrofittype()); // RetrofitFactory - POST            }        }        Method[] methods=clz.getDeclaredMethods();      // 获取当前类的所有字段        for(Method method :methods){            if(method.isAnnotationPresent(RetrofitFM.class)){                RetrofitFM retrofitfm = (RetrofitFM) method.getAnnotation(RetrofitFM.class);                // retrofitfm.value() 没打印值,说明我没写值,默认是""                System.out.println(retrofitfm.value() +" - " +retrofitfm.retrofittype()); //  - DELETE            }        }    }}

知识点就讲这多,剩下的需要自己领悟和自己深入研究。
第一章结束………
下章见