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

相关内容

热门资讯

透视真的!wpk私人辅助,wp... 透视真的!wpk私人辅助,wpk透视辅助靠谱吗,微扑克教程(原来真的有挂)1、wpk透视辅助靠谱吗a...
透视透视!wepoker透视脚... 透视透视!wepoker透视脚本,wepoker有辅助器吗(切实存在有挂)1、完成wepoker有辅...
透视新版!wpk透视插件,wp... 透视新版!wpk透视插件,wpk刷入池率脚本,AI教程(一直是有挂);亲,关键说明,wpk刷入池率脚...
透视ai代打!wepoker透... 透视ai代打!wepoker透视脚本是什么,wepoker有什么规律(都是真的是有挂)wepoker...
透视美元局!wepoker底牌... 透视美元局!wepoker底牌透视,wepoker透视辅助下载(真是真的是有挂)1、wepoker透...
透视好牌!wpk作弊,wpk私... 透视好牌!wpk作弊,wpk私人局辅助是真的吗,必赢教程(都是真的是有挂);wpk私人局辅助是真的吗...
透视游戏!wepoker透视器... 透视游戏!wepoker透视器免费,安装不了wepoker(总是是真的有挂)1、每一步都需要思考,不...
透视有挂!wpk辅助购买,wp... 透视有挂!wpk辅助购买,wpk显示有作弊,微扑克教程(原来是有挂);wpk显示有作弊辅助器中分为三...
透视科技!we-poker是什... 透视科技!we-poker是什么软件,we-poker辅助(竟然是真的有挂)1、让任何用户在无需we...
透视插件!wpk私人局辅助是真... 透视插件!wpk私人局辅助是真的吗,wpk有那种辅助吗,必胜教程(都是是有挂);在进入wpk私人局辅...