【发布时间】:2017-01-11 08:56:50
【问题描述】:
我正在查看其他人的一些 ByteBuddy 代码。他使用 ByteBuddy 生成运行时子类,这些子类用作代理将他的运行时的一些管理代码实现到特定对象中。
Class<? extends T> newSubClass = new ByteBuddy(ClassFileVersion.ofThisVm())
.subclass(classType)
.defineField("_core", Object.class, Visibility.PUBLIC) //<---
.method(ElementMatchers.isDeclaredBy(classType))
.intercept(InvocationHandlerAdapter.of((proxy, method, m_args) -> {
//TODO: Need to replace core with _core as core is a function argument and will make it bound
return proxyHandler(core, method, m_args); //<--
}))
.make()
.load(roleType.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
T proxy = ReflectionHelper.newInstance(newSubClass, args);
newSubClass.getField("_core").set(proxy, core);
为了不将 core 对象直接绑定到 lambda 中,我想使用新定义的字段 _core,这样我就可以重用生成的类(而不是在每次调用函数时重新生成它)。
有没有办法做到这一点?
提前致谢。
【问题讨论】:
标签: java subclassing byte-buddy