ByteBuddy:如何将方法委托/转发到volatile字段



我有(伪代码)像这样:

final Class<OUT> newClass = (Class<OUT>) new ByteBuddy()
.subclass(Object.class)
.name(newName)
.implement(SomeInterface.class, SomeOtherInterface.class)
.method(ElementMatchers.isMethod())                            
.intercept(
    ExceptionMethod.throwing(UnsupportedOperationException.class,
                            "calling this method is not supported"))
// in fact I am matching for more than a single method here
.method(ElementMatchers.named("getUuid"))
.intercept(
    MethodDelegation.toInstanceField(SomeOtherInterface.class, "delegate"))
.make()
.load(aBusinessEntityClass.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();

我目前的问题是:我需要我的委托字段是volatile。我怎样才能做到呢?

toInstanceField API在Byte Buddy 1.5.0中被淘汰,取而代之的是一个新的仪表API,您可以显式地定义字段:

new ByteBuddy()
  .defineField("delegate", SomeOtherInterface.class, VOLATILE)
  .method(ElementMatchers.named("getUuid"))
  .intercept(MethodDelegation.toField("delegate"));

这允许做其他事情,如添加注释等。

该方法现在在所有Implementation上实现。新版本今天发布了

好的,找到了一个适合我的解决方案,以防有人感兴趣:

final Class<OUT> newClass = (Class<OUT>) new ByteBuddy()
.subclass(Object.class)
.name(newName)
.implement(SomeInterface.class, SomeOtherInterface.class)
.method(ElementMatchers.isMethod())                            
.intercept(
    ExceptionMethod.throwing(UnsupportedOperationException.class,
                        "calling this method is not supported"))
// in fact I am matching for more than a single method here
.method(ElementMatchers.named("getUuid"))
.intercept(
    MethodDelegation.toInstanceField(SomeOtherInterface.class, "delegate"))
// -- HERE THE FIELD IS MODIFIED AGAIN, IN THIS CASE AS  --
// -- PRIVATE & VOLATILE                                 --
.field(ElementMatchers.named("delegate"))                       
.transform(Transformer.ForField.withModifiers(FieldManifestation.VOLATILE, Visibility.PRIVATE))
.make()
.load(aBusinessEntityClass.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();

解决方案是随后使用transform()和适当的Transformer.ForField.withModifiers()调用field()来修改字段。

希望能帮助大家面对这个问题。

最新更新