Search code examples
javabyte-buddy

Under what circumstances will a ByteBuddy proxy class be unable to access its own defined private fields?


I'm trying to understand this inscrutable error message:

java.lang.IllegalStateException: Cannot access private final com.foo.TestMethodInvoker$Foo com.bar.$MyProxy.methodHousing from class com.bar.$MyProxy
at net.bytebuddy.implementation.MethodCall$TargetHandler$ForField$Location$ForExplicitField.resolve(MethodCall.java:2406)
    at net.bytebuddy.implementation.MethodCall$TargetHandler$ForField$Factory.make(MethodCall.java:2444)
    at net.bytebuddy.implementation.MethodCall$Appender.<init>(MethodCall.java:3500)
    at net.bytebuddy.implementation.MethodCall.appender(MethodCall.java:676)
    at net.bytebuddy.dynamic.scaffold.MethodRegistry$Handler$ForImplementation.compile(MethodRegistry.java:233)
    at net.bytebuddy.dynamic.scaffold.MethodRegistry$Handler$ForImplementation.compile(MethodRegistry.java:205)
    at net.bytebuddy.dynamic.scaffold.MethodRegistry$Default$Prepared.compile(MethodRegistry.java:740)
    at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:218)
    at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:204)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:3595)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:3819)

I have a unit test (TestMethodInvoker) that defines a public static nested class named Foo.

My ByteBuddy recipe defines a private final com.foo.TestMethodInvoker$Foo field named methodHousing.

I use FieldAccessor.of to do various things with this field. (Or I'm trying to!)

The error message appears to be telling me that my proxy class cannot access its own private field (!).

Under what circumstances would this be true? Or what have I done wrong?


Solution

  • You are running into a bug which I just fixed on the master branch. This will be released in Byte Buddy 1.10.14.

    If you want to avoid this bug in the meantime, avoid the explicit field reference as an argument but supply a matcher that resolves the field.