java反射和注解3-仿照retrofit组装接口参数

news/2024/5/19 4:01:00 标签: java, retrofit, 反射, 注解, 动态代理

本片文章将用反射注解仿照retrofit只需要传入一个带有给定注解的接口,通过调用接口就能直接将传入的数据和注解进行结合,生成对应参数

1,自定义注解

对字段的修饰

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface AnnotationField {
    String value();
}

仿照GET方法注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationGet {
    String value();
}

通过动态代理实现注解和数据拼装

public class RetrofitCopy {

    public <T> T create(final Class<T> service) {

        return (T) Proxy.newProxyInstance(
                service.getClassLoader(),
                new Class[]{service},
                new AssemblyParametersInvocationHandler()
        );

    }

    private class AssemblyParametersInvocationHandler implements InvocationHandler {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            StringBuilder builder = new StringBuilder();

            for (Annotation annotation : method.getAnnotations()) {
                if (annotation instanceof AnnotationGet) {
                    AnnotationGet get = (AnnotationGet) annotation;
                    builder.append(get.value());
                }
            }

            Parameter[] parameters = method.getParameters();

            for (int i = 0; i < parameters.length; i++) {

                Parameter parameter = parameters[i];
                for (Annotation annotation : parameter.getAnnotations()) {
                    builder.append(getParementerAnnotationValue(annotation));
                    builder.append("=");
                }
                builder.append(args[i]);
                if (i < parameters.length - 1) {
                    builder.append("?");
                }

            }
            Log.e("RetrofitCopy", "net: " + builder.toString());

            return null;
        }
    }

    private String getParementerAnnotationValue(Annotation annotation) {
        String data = "";
        if (annotation instanceof AnnotationField) {
            AnnotationField field = (AnnotationField) annotation;
            return field.value();
        }

        return data;

    }
    
}

创建接口api

public interface Api {
    @AnnotationGet("http://baidu.com/")
    void getData(
            @AnnotationField("name")
            String name,
            @AnnotationField("age") int age
    );
}

执行调用代码

 val api = RetrofitCopy().create(Api::class.java);
        api.getData("copyNet1", 1000);
        api.getData("copyNet2", 2000);
        api.getData("copyNet3", 3000);
        api.getData("copyNet4", 4000);
        api.getData("copyNet5", 5000);
        api.getData("copyNet6", 6000);
        api.getData("copyNet7", 7000);

打印数据如下

这个就是retrofit封装参数的原理,是不是和retrofit一样,只需要接口,然后接口调用方法就可以了


http://www.niftyadmin.cn/n/5222127.html

相关文章

2020年1月31日 Go生态洞察:pkg.go.dev的未来步骤

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

C++ day41 动态规划 整数拆分 不同的二叉搜索树

题目1&#xff1a;343 整数拆分 题目链接&#xff1a;整数拆分 对题目的理解 将正整数n&#xff0c;拆分成k个正整数的和&#xff08;k>2&#xff09;使得这些整数的乘积最大化&#xff0c;返回最大乘积 动规五部曲 1&#xff09;dp数组的含义以及其下标i的含义 dp[i]…

2023年亚太地区数学建模大赛 C 题

我国新能源电动汽车的发展趋势 新能源汽车是指以先进技术原理、新技术、新结构的非常规汽车燃料为动力来源&#xff08;非常规汽车燃料指汽油、柴油以外的燃料&#xff09;&#xff0c;将先进技术进行汽车动力控制和驱动相结合的汽车。新能源汽车主要包括四种类型&#xff1a;…

Vatee万腾的科技远行:vatee数字化力量的前瞻发现

在当今数字化风潮中&#xff0c;Vatee万腾以其强大的科技远行能力&#xff0c;成为引领行业的先锋。Vatee不仅在数字化领域展现出卓越的实力&#xff0c;更以前瞻性的创新引领着科技的未来之路。 Vatee万腾以其数字化力量勇攀科技远峰。Vatee万腾通过对前沿技术的不断探索和实践…

vue3中readonly和shallowReadonly

readonly: 深度只读数据 获取一个对象 (响应式或纯对象) 或 ref 并返回原始代理的只读代理。 只读代理是深层的&#xff1a;访问的任何嵌套 property 也是只读的。 shallowReadonly 浅只读数据 创建一个代理&#xff0c;使其自身的 property 为只读&#xff0c;但不执行…

RH2288H V3服务器使用ISO安装系统

1.配置和服务器相同网段地址&#xff0c;RH2288H V3服务器bmc管理网口默认IP是192.168.2.100/24&#xff0c;默认用户root&#xff0c;默认Huawei12#$&#xff0c;网线连接BMC口&#xff0c;登录。默认密码可以在开机时按del键进入配置页面修改 2.配置raid&#xff0c;生产环境…

强烈推荐:零售行业升级教程,现学现用

新零售模式是随着科技的迅猛发展而崭露头角的一种零售业态。在这个数字化时代&#xff0c;消费者的购物习惯和期望正在发生根本性的变化&#xff0c;推动着传统零售业寻找创新的方式来满足不断变化的市场需求。 自动售货机作为新零售模式的一部分&#xff0c;以其高效、便捷、智…

JS严格模式(use strict)详解

严格模式是在 ECMAScript5&#xff08;ES5&#xff09;中引入的&#xff0c;在严格模式下&#xff0c;JavaScript 对语法的要求会更加严格&#xff0c;一些在正常模式下能够运行的代码&#xff0c;在严格模式下将不能运行。 添加严格模式&#xff0c;主要有以下几个目的&#x…