Interface MethodInterceptor

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface MethodInterceptor
Method Interceptor Interface 方法拦截器接口

Intercepts method invocations on proxy objects.

拦截代理对象上的方法调用。

Features | 主要功能:

  • Method invocation interception - 方法调用拦截
  • Chainable interceptors (andThen, compose) - 可链接拦截器
  • Factory methods: passThrough, constant, throwing - 工厂方法

Usage Examples | 使用示例:

MethodInterceptor logger = (proxy, method, args, invoker) -> {
    System.out.println("Calling: " + method.getName());
    return invoker.invoke(args);
};

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: No (args may be null for no-arg methods) - 空值安全: 否(无参方法的args可能为null)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • intercept

      Object intercept(Object proxy, Method method, Object[] args, MethodInvoker invoker) throws Throwable
      Intercepts a method invocation 拦截方法调用
      Parameters:
      proxy - the proxy object | 代理对象
      method - the method being invoked | 被调用的方法
      args - the method arguments | 方法参数
      invoker - the method invoker for calling the original | 用于调用原始方法的调用器
      Returns:
      the result | 结果
      Throws:
      Throwable - if an error occurs | 如果发生错误
    • passThrough

      static MethodInterceptor passThrough()
      Creates an interceptor that does nothing and just invokes the original 创建一个不做任何事只调用原始方法的拦截器
      Returns:
      the pass-through interceptor | 透传拦截器
    • constant

      static MethodInterceptor constant(Object value)
      Creates an interceptor that always returns a value 创建一个总是返回指定值的拦截器
      Parameters:
      value - the value to return | 要返回的值
      Returns:
      the constant interceptor | 常量拦截器
    • throwing

      static MethodInterceptor throwing(Throwable exception)
      Creates an interceptor that throws an exception 创建一个总是抛出异常的拦截器
      Parameters:
      exception - the exception to throw | 要抛出的异常
      Returns:
      the throwing interceptor | 抛异常拦截器
    • andThen

      default MethodInterceptor andThen(MethodInterceptor next)
      Chains this interceptor with another 将此拦截器与另一个链接
      Parameters:
      next - the next interceptor | 下一个拦截器
      Returns:
      the chained interceptor | 链接后的拦截器
    • compose

      default MethodInterceptor compose(MethodInterceptor before)
      Creates an interceptor that runs before this one 创建一个在此拦截器之前运行的拦截器
      Parameters:
      before - the before interceptor | 之前的拦截器
      Returns:
      the chained interceptor | 链接后的拦截器