简体   繁体   English

Bytebuddy - 代理私有注解方法

[英]Bytebuddy - proxy private annotated method

I am proxying my objects with ByteBuddy and all works fine.我正在用 ByteBuddy 代理我的对象,一切正常。 When i annotate a method with @Test then they should be proxied another way.当我用 @Test 注释一个方法时,它们应该以另一种方式代理。 So i decided to separate my InvocationHandler into two.所以我决定把我的 InvocationHandler 分成两部分。 So far so good.到目前为止,一切都很好。

But now, when i want to add private, @Test-annotated methods they wont get proxied/intercepted.但是现在,当我想添加私有的、@Test 注释的方法时,它们不会被代理/拦截。 Only public methods will be intercepted.只有公共方法会被拦截。 Any ideas why?任何想法为什么?

// 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();

If you define custom methods, your matchers will not be applied to those.如果您定义自定义方法,您的匹配器将不会应用于这些方法。 You would need to specify the Implementation to also apply these matchers.您需要指定Implementation来应用这些匹配器。 Alternatively, you can create a class with the additional methods and then intercept this type by creating another proxy.或者,您可以使用其他方法创建 class,然后通过创建另一个代理来拦截此类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM