微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Bytebuddy - 代理私有注释方法

如何解决Bytebuddy - 代理私有注释方法

我正在使用 ByteBuddy 代理我的对象,并且一切正常。当我用 @Test 注释一个方法时,它们应该以另一种方式代理。所以我决定将我的 InvocationHandler 一分为二。到目前为止一切顺利。

但是现在,当我想添加私有的、@Test 注释的方法时,它们不会被代理/拦截。只会拦截公共方法。任何想法为什么?

// return created proxy instance
@SuppressWarnings("unchecked")
Class<T> proxy = (Class<T>) byteBuddy
    .subclass(clazz)
    .implement(Proxy.class)
    .defineField("_origin",Object.class,Visibility.PRIVATE)
    .defineConstructor(Visibility.PUBLIC)
    .withParameter(Object.class)
    .intercept(MethodCall.invoke(clazz.getDeclaredConstructor()).andThen(FieldAccessor.ofField("_origin").setsArgumentAt(0)))
    .name(clazz.getSimpleName() + "Proxy")
    .method(ElementMatchers.isAnnotatedWith(Test.class))
    .intercept(InvocationHandlerAdapter.of(testInvocationHandler))
    .method(ElementMatchers.isDeclaredBy(AdditionalTest.class))
    .intercept(InvocationHandlerAdapter.of(testInvocationHandler.getAdditionalTestInvocationHandler()))
    .method(ElementMatchers.isDeclaredBy(Proxy.class)).intercept(InvocationHandlerAdapter.of(testInvocationHandler.getAdditionalTestInvocationHandler())).make()
    .load(clazz.getClassLoader()).getLoaded();

解决方法

如果您定义自定义方法,您的匹配器将不会应用于这些方法。您还需要指定 Implementation 以应用这些匹配器。或者,您可以使用其他方法创建一个类,然后通过创建另一个代理来拦截此类型。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。