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

相关内容

热门资讯

玩家交流"佛手在线大... 玩家交流"佛手在线大菠萝技巧"开挂(透视)辅助透视讲解有挂-曝光教程;无需打开直接搜索加薇13670...
实测分享"邯郸胡乐挂... 实测分享"邯郸胡乐挂辅助"开挂(下载)辅助下载有挂分析-玩家教你>>您好:软件加136704302中...
科普攻略"福建兄弟1... 科普攻略"福建兄弟13水破解版"开挂(脚本)辅助脚本有挂功能-细节揭秘 >>您好:软件加薇13670...
关于"金虎爷辅助器&... 您好:金虎爷辅助器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
推荐十款"福建大玩家... 您好:福建大玩家辅助工具这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
透视透视挂"河洛杠次... 您好:河洛杠次插件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
透视神器"闲逸辅助正... 透视神器"闲逸辅助正版免费下载"开挂(下载)辅助下载有挂方针-安装教程;无需打开直接搜索打开薇:13...
透视肯定"92营口辅... 您好:这款92营口辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款92营口辅助游戏中打牌都会发现很...
透视辅助"边锋老友杰... 边锋老友杰克辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,边锋老友杰克辅助作为一种经典的娱乐方...
分享一款"情怀辅助器... 分享一款"情怀辅助器"开挂(安装)辅助安装有挂透明挂-2026新版总结这是一款可以让一直输的玩家,快...