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、玩家可以在南通长牌有挂吗软件透明挂俱...
4分钟了解“都莱辅助软件”详细... 4分钟了解“都莱辅助软件”详细开挂辅助教程-真是有挂1、用户打开应用后不用登录就可以直接使用,点击都...
1分钟了解“吉祥小程序填大坑辅... 1分钟了解“吉祥小程序填大坑辅助器”详细开挂辅助工具-其实真的有挂1、每一步都需要思考,不同水平的挑...
六分钟了解“陕西三代辅助”详细... 六分钟了解“陕西三代辅助”详细开挂辅助神器-切实真的是有挂1、点击下载安装,陕西三代辅助插件透视分类...
3分钟了解“闲娱江西脚本”详细... 3分钟了解“闲娱江西脚本”详细开挂辅助技巧-其实是有挂1、闲娱江西脚本机器人多个强度级别选择2、闲娱...
1分钟了解“黑侠破解约局吧”详... 1分钟了解“黑侠破解约局吧”详细开挂辅助技巧-原来是真的挂1、让任何用户在无需黑侠破解约局吧AI插件...
第5分钟了解“蜀山四川小程序辅... 第5分钟了解“蜀山四川小程序辅助挂视频”详细开挂辅助app-其实存在有挂1、让任何用户在无需蜀山四川...
两分钟了解“好友赣南能装挂吗”... 两分钟了解“好友赣南能装挂吗”详细开挂辅助攻略-切实是有挂好友赣南能装挂吗辅助器中分为三种模型:好友...
十分钟了解“欢聚水鱼透视器”详... 十分钟了解“欢聚水鱼透视器”详细开挂辅助攻略-本来是真的挂1、进入到欢聚水鱼透视器黑科技之后,能看到...
第1分钟了解“九酷众游辅助”详... 第1分钟了解“九酷众游辅助”详细开挂辅助app-其实真的有挂1、实时九酷众游辅助开挂更新:用户可以随...