简体   繁体   中英

Bytebuddy - proxy private annotated method

I am proxying my objects with ByteBuddy and all works fine. When i annotate a method with @Test then they should be proxied another way. So i decided to separate my InvocationHandler into two. So far so good.

But now, when i want to add private, @Test-annotated methods they wont get proxied/intercepted. 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. Alternatively, you can create a class with the additional methods and then intercept this type by creating another proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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