Retrofit之动态代理

关于代理有静态代理和动态代理

静态代理:代理类通过实现与目标对象相同的接口,并在类中维护一个代理对象。通过构造器塞入目标对象,赋值给代理对象,进而执行代理对象实现的接口方法,并实现前拦截,后拦截等所需的业务功能。
public interface ICustomer {
    void buyMac();
}
public class ZhangSanBuy implements ICustomer {
    @Override
    public void buyMac() {
        System.out.println("ICustomer:=张三买东西");
    }
}
public class ProxyBuy implements ICustomer {
    private ICustomer customer;
    public ProxyBuy(ICustomer customer) {
        this.customer = customer;
    }
    @Override
    public void buyMac() {
        System.out.println("ICustomer:=代理购买");
        customer.buyMac();
    }
}
public class RunningResult {

    public static void main(String args[]) {
          ZhangSanBuy zhangSanBuy = new ZhangSanBuy();
        ProxyBuy proxyBuy = new ProxyBuy(zhangSanBuy);
        proxyBuy.buyMac();    
    }
}

运行结果如下:

ICustomer:=代理购买
ICustomer:=张三买东西

从结果中就可以看到在代码运行前就知道代理的是张三,所以是静态代理

动态代理:动态的在内存中构建代理对象(需要我们制定要代理的目标对象实现的接口类型),即利用JDK的API生成指定接口的对象,也称之为JDK代理或者接口代理。

利用Retrofit的注解内容获取接口上的注解内容和注解参数的demo

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface GitHubService {
    @FormUrlEncoded
    @POST("ytnc/admin/auth/login")
    Call serviceApi(@Field("userName") String userName, @Field("password") String password);
}
import android.support.annotation.Nullable;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import retrofit2.http.Field;
import retrofit2.http.POST;

public class ProxyUtil {

    public static  T create(final Class service) {

        return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[]{service},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, @Nullable Object[] args)
                            throws Throwable {
                        getRequestType(method);
                        getParameters(method, args);
                        return null;
                    }
                });
    }

    //获取注解方法上的注解内容
    private static void getRequestType(Method method) {
        Annotation[] annotations = method.getAnnotations();
        for (Annotation annotation : annotations) {
            if(annotation instanceof POST){
                String value = ((POST) annotation).value();
                System.out.println("动态代理方法注解内容="+value);
            }
        }
    }

    //获取参数
    private static void getParameters(Method method, @Nullable Object[] args) {
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            int length = parameterAnnotations[i].length;
            if (length == 0) {
                System.out.println("没有添加Annotation参数");
            } else {
                Annotation[] annotation = parameterAnnotations[i];
                for (Annotation anno : annotation) {
                    if (anno instanceof Field) {
                        String key = ((Field) anno).value();
                        System.out.println("动态代理参数key==" + key+"&value="+args[i]);
                    }
                }
            }

        }
    }
}
public class RunningResult {
    public static void main(String args[]) {
        GitHubService service = ProxyUtil.create(GitHubService.class);
        service.serviceApi("xiaoma","123456");

    }
}

运行结果如下:

动态代理方法注解内容=ytnc/admin/auth/login
动态代理参数key==userName&value=xiaoma
动态代理参数key==password&value=123456
通过动态代理可以获取接口里的方法上的注解内容和注解参数,也是Retrofit的核心知识点之一。

你可能感兴趣的:(Retrofit之动态代理)