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()调用原始方法。根据你的需要选择适合的方式。

相关内容

热门资讯

第8分钟专业!新荣耀辅助软件,... 第8分钟专业!新荣耀辅助软件,闲来辅助平台(都是存在有挂)-哔哩哔哩1)新荣耀辅助软件辅助挂:进一步...
第九分钟必备!山西大唐辅助器,... 第九分钟必备!山西大唐辅助器,欢乐茶馆免费辅助设置(其实存在有挂)-哔哩哔哩1、打开软件启动之后找到...
第7分钟科普!开心十三张哈局辅... 第7分钟科普!开心十三张哈局辅助,欢聚水鱼只能辅助教程(本来真的是有挂)-哔哩哔哩1、任何欢聚水鱼只...
第9分钟专业!科乐游戏辅助脚本... 第9分钟专业!科乐游戏辅助脚本下载,四川熊猫辅助软件下载(果然是有挂)-哔哩哔哩1、首先打开四川熊猫...
1分钟曝光!天天爱柳州辅助,椰... 1分钟曝光!天天爱柳州辅助,椰子大厅辅助脚本(真是真的有挂)-哔哩哔哩1、完成椰子大厅辅助脚本的残局...
第六分钟专业!小程序边锋干橙眼... 第六分钟专业!小程序边锋干橙眼辅助,超级三加一辅助软件(都是真的有挂)-哔哩哔哩1、每一步都需要思考...
一分钟专业!开心泉州免费辅助,... 一分钟专业!开心泉州免费辅助,火神大厅辅助(总是真的是有挂)-哔哩哔哩1、游戏颠覆性的策略玩法,独创...
第1分钟普及!大菠萝游戏佛手在... 第1分钟普及!大菠萝游戏佛手在线辅助怎么用,新樱花互娱源码(一直存在有挂)-哔哩哔哩1、上手简单,内...
四分钟专业!天天微友有辅助吗,... 四分钟专业!天天微友有辅助吗,瓜瓜丰城双剑旧版攻略(都是是真的挂)-哔哩哔哩1、进入游戏-大厅左侧-...
8分钟揭露!九九山城万州辅助,... 8分钟揭露!九九山城万州辅助,微信小游戏破解版(竟然是真的挂)-哔哩哔哩该软件可以轻松地帮助玩家将九...