简体   繁体   中英

ByteBuddy - creating proxy, but with original object as field

I'am trying to create a proxy with ByteBuddy. Is it possible to use the InvocationHandlerAdapter but to adapt it that the original object can be hold as field too? Actually i am using it like this:

 Class<T> proxy = (Class<T>) byteBuddy
                .subclass(clazz)
                .name(clazz.getSimpleName() + "Implementing" + TaskOps.class.getSimpleName() + "Proxy")
                .method(ElementMatchers.isAnnotatedWith(Task.class))
                .intercept(InvocationHandlerAdapter.of(taskInvocationHandler))
                .method(ElementMatchers.isDeclaredBy(TaskOps.class))
                .intercept(InvocationHandlerAdapter.of(taskInvocationHandler.getTaskOpsInvocationHandler())).make()
                .load(clazz.getClassLoader()).getLoaded();

Everything works fine. But i want to keep the proxied object as field "original" in the created proxy (or in the invocation handler). Is that possible?

Any help is appreciated.

Yes, this is of course possible using the adapter's toField method where the fields must be of type InvocationHandler . You can define the field on the proxy and it's your responsibility to set an instance before taking a proxy into use.

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