502. Java 反射 - 编写 MessageInterceptor 类

文章目录

  • [502. Java 反射 - 编写 MessageInterceptor 类](#502. Java 反射 - 编写 MessageInterceptor 类)
    • [1. 设计思路](#1. 设计思路)
    • [2. MessageInterceptor 实现](#2. MessageInterceptor 实现)
    • [3. 示例讲解](#3. 示例讲解)
    • [4. 总结](#4. 总结)

502. Java 反射 - 编写 MessageInterceptor 类

1. 设计思路

拦截器的目标是:

  1. 在调用目标方法前 → 校验或修改参数。
  2. 调用目标方法 → 执行实际的业务逻辑。
  3. 在调用目标方法后 → 处理返回值或执行额外逻辑。

在这个例子中,我们希望拦截 SomeInterceptedService.message() 方法,并且:

  • 检查参数是否合法(非空、非空字符串)。
  • 调用原始方法,获取结果。
  • 在结果末尾加上 " [was intercepted]" 来表明它被拦截过。

2. MessageInterceptor 实现

java 复制代码
public class MessageInterceptor implements Interceptor<SomeInterceptedService, String> {

    @Override
    public String intercept(
            SomeInterceptedService service, Method interceptedMethod, Object... arguments) {
        try {
            if (arguments.length == 1) {

                // ✅ 1. 参数校验
                String input = (String) arguments[0];
                Objects.requireNonNull(input, "Input is null");
                if (input.isEmpty()) {
                    throw new IllegalArgumentException("Input is empty");
                }

                // ✅ 2. 调用目标方法
                String result = (String) interceptedMethod.invoke(service, arguments);

                // ✅ 3. 修改返回值
                return result + " [was intercepted]";
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Error invoking method reflectively", e);
        }

        throw new IllegalArgumentException(
                "Arguments should contain exactly one argument of type String");
    }
}

3. 示例讲解

原始服务类

java 复制代码
public class SomeInterceptedService {
    @Intercept(MessageInterceptor.class)
    public String message(String input) {
        return input.toUpperCase();
    }
}

测试调用

java 复制代码
public class Main {
    public static void main(String[] args) throws Exception {
        String output = (String) ServiceFactory.invoke(SomeInterceptedService.class, "message", "hello");
        System.out.println("Final output = " + output);
    }
}

输出结果

java 复制代码
Final output = HELLO [was intercepted]

4. 总结

  • 参数校验 :如果传入 null 或空字符串,会抛出异常,阻止目标方法的执行。
  • 拦截逻辑 :我们在调用 message() 前后加入了额外逻辑(参数校验 + 结果追加说明)。
  • 反射调用 :通过 interceptedMethod.invoke(service, arguments) 来执行实际的业务方法。
  • 增强返回值 :返回值被拦截器修改,增加了 " [was intercepted]"
相关推荐
mldong11 小时前
给若依加审批流,不用 Flowable
java·spring boot·架构
Wang's Blog13 小时前
Java框架快速入门: Spring Security+OAuth2之数据库和实体类的RBAC改造
java·数据库·spring
讳疾忌医丶13 小时前
深度拆解 RocksDB 内核:基于 C++17 的 FIFO 调度状态机与温度阶梯自愈设计
java·c++·算法·架构
lsswear14 小时前
Python 并发 线程
开发语言·python
郑州光合科技余经理14 小时前
国际版外卖系统:税率字段怎么和订单主流程解耦
android·java·开发语言·前端·后端·php·ai编程
毅炼14 小时前
Rover-Suite 开源发布:轻量服务注册中心与网关一体化方案
java·后端·系统架构·gateway
软件课代表CiCi14 小时前
运行库是什么?电脑缺运行库怎么办?c++运行库缺失修复全攻略
开发语言·c++·windows·电脑·dll修复·dll丢失·运行库
梦想平凡14 小时前
百游棋牌源代码开发搭建教程(十):隔离部署、备份恢复与双端验收
java·前端·javascript·数据库·源代码管理
Ivanqhz14 小时前
MLIR OpBuilder
开发语言·python·mlir
sunshine22 girl16 小时前
Idea中如何搜索
java·ide·intellij-idea