Byte-Buddy: 方法拦截 InvocationHandler vs 方法委托到 GeneralInterceptor
创始人
2025-01-12 20:30:26
0

在Byte-Buddy中,你可以使用InvocationHandler或GeneralInterceptor来拦截方法。下面是使用这两种方式的代码示例:

  1. 使用InvocationHandler拦截方法:
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

public class InvocationHandlerExample {

    public static class MyInvocationHandler {

        @RuntimeType
        public static Object intercept(@AllArguments Object[] args,
                                       @SuperCall Callable callable,
                                       @This Object target,
                                       @Origin Method method) throws Exception {
            System.out.println("Before method: " + method.getName());
            // 在方法调用前执行额外的逻辑
            Object result = callable.call(); // 调用原始方法
            // 在方法调用后执行额外的逻辑
            System.out.println("After method: " + method.getName());
            return result;
        }
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        Class dynamicType = new ByteBuddy()
                .subclass(Object.class)
                .method(any())
                .intercept(MethodDelegation.to(MyInvocationHandler.class))
                .make()
                .load(InvocationHandlerExample.class.getClassLoader())
                .getLoaded();

        try {
            Object instance = dynamicType.newInstance();
            instance.toString();
            instance.hashCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上面的示例中,我们创建了一个InvocationHandler类MyInvocationHandler,它的intercept方法将在方法调用前后执行。使用Byte-Buddy的MethodDelegation将方法委托给该InvocationHandler。

  1. 使用GeneralInterceptor拦截方法:
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
import net.bytebuddy.matcher.ElementMatchers;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

public class GeneralInterceptorExample {

    public static class MyInterceptor {

        @RuntimeType
        public static Object intercept(@AllArguments Object[] args,
                                       @SuperCall Callable callable,
                                       @This Object target,
                                       @Origin Method method) throws Exception {
            System.out.println("Before method: " + method.getName());
            // 在方法调用前执行额外的逻辑
            Object result = callable.call(); // 调用原始方法
            // 在方法调用后执行额外的逻辑
            System.out.println("After method: " + method.getName());
            return result;
        }
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        DynamicType.Unloaded dynamicType = new ByteBuddy()
                .subclass(Object.class)
                .method(ElementMatchers.any())
                .intercept(MethodDelegation.withDefaultConfiguration()
                        .withBinders(MyInterceptor.Binder.INSTANCE)
                        .to(new MyInterceptor()))
                .make();

        try {
            Class loaded = dynamicType.load(GeneralInterceptorExample.class.getClassLoader())
                    .getLoaded();
            Object instance = loaded.newInstance();
            instance.toString();
            instance.hashCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上面的示例中,我们创建了一个GeneralInterceptor类MyInterceptor,它的intercept方法将在方法调用前后执行。使用Byte-Buddy的MethodDelegationwithBinders将方法委托给该GeneralInterceptor。

这两种方式的区别在于InvocationHandler是通过反射调用原始方法,而GeneralInterceptor是通过Callable.call()调用原始方法。根据你的需要选择适合的方式。

相关内容

热门资讯

三分钟明白!欢乐达人暗宝辅助工... 您好:欢乐达人暗宝辅助工具这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
四分钟详情!蜀山四川智能辅助插... 蜀山四川智能辅助插件是一款专注玩家量身打造的游戏记牌类型软件,在蜀山四川智能辅助插件这款游戏中我们可...
1分钟讲解!一起宁德510k脚... 1分钟讲解!一起宁德510k脚本(辅助挂)其实是有挂(必备教程开挂辅助插件),一起宁德510k脚本是...
5分钟透视!广西友乐软件辅助(... 5分钟透视!广西友乐软件辅助(辅助挂)一贯是有挂(必备教程开挂辅助插件)>>您好:软件加136704...
第六分钟讲解!花花生活圈私人局... 您好:花花生活圈私人局辅助器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
6分钟详情!边锋老友填大坑辅助... 边锋老友填大坑辅助是一款专注玩家量身打造的游戏记牌类型软件,在边锋老友填大坑辅助这款游戏中我们可以记...
第九分钟发现!随意玩房卡代理有... 第九分钟发现!随意玩房卡代理有挂吗(辅助挂)果然有挂(必胜教程开挂辅助下载)【无需打开直接搜索加薇1...
四分钟了解!途游四川bug(辅... 途游四川bug 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
第8分钟明白!情怀透明器看手机... 第8分钟明白!情怀透明器看手机纸牌(辅助挂)其实真的是有挂(AI教程开挂辅助软件);超受欢迎的情怀透...
第六分钟透视!微乐小程序游戏破... 第六分钟透视!微乐小程序游戏破解器(辅助挂)原来是有挂(普及教程开挂辅助神器);无需打开直接搜索薇:...