This question is related to my previous quesiton here: How to create a default constructor with Byte Buddy
I am creating a subclass which first sets up some context before delegatig method invocation to some instance. This already works quite well with one issue remaining.
I get the following error when loading my dynamically created subclass.
java.lang.VerifyError: Bad access to protected data in invokevirtual
Exception Details:
Location:
com/frequentis/ps/service/test/saga/ProxyTestSaga$ByteBuddy$Rm8DV3Lj.setTimeoutManager(Lcom/codebullets/sagalib/timeout/TimeoutManager;)V @3: invokevirtual
Reason:
Type 'com/frequentis/ps/service/test/saga/ProxyTestSaga' (current frame, stack[0]) is not assignable to 'com/frequentis/ps/service/test/saga/ProxyTestSaga$ByteBuddy$Rm8DV3Lj'
Current Frame:
bci: @3
flags: { }
locals: { 'com/frequentis/ps/service/test/saga/ProxyTestSaga$ByteBuddy$Rm8DV3Lj', 'com/codebullets/sagalib/timeout/TimeoutManager' }
stack: { 'com/frequentis/ps/service/test/saga/ProxyTestSaga' }
Bytecode:
0x0000000: b200 0cb6 0010 57b1
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
at java.lang.Class.getDeclaredField(Class.java:2068)
at net.bytebuddy.implementation.LoadedTypeInitializer$ForStaticField.onLoad(LoadedTypeInitializer.java:124)
at net.bytebuddy.implementation.LoadedTypeInitializer$Compound.onLoad(LoadedTypeInitializer.java:200)
at net.bytebuddy.implementation.LoadedTypeInitializer$Compound.onLoad(LoadedTypeInitializer.java:200)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.initialize(DynamicType.java:3497)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:3485)
at com.frequentis.ps.service.test.saga.DynamicSagaTypeBuilder.buildAndLoad(DynamicSagaTypeBuilder.java:65)
at com.frequentis.ps.service.test.saga.MoreAbstractSpaceSagaUnitTest.generateProxyClassForSagaUnderTest(MoreAbstractSpaceSagaUnitTest.java:274)
at com.frequentis.ps.service.test.saga.AbstractSpaceSagaUnitTest.enhance(AbstractSpaceSagaUnitTest.java:105)
at com.frequentis.ps.service.test.saga.ProxyTestSagaTest.before(ProxyTestSagaTest.java:27)
This is currently my byte buddy setup, which works for almost all cases except for the "setTimeoutManager
" and "setState
" methods which result in the shown error.
// called within the unit test base class (as shown in the call stack above)
builder = new ByteBuddy()
.subclass(sagaUnderTestClass, ConstructorStrategy.Default.IMITATE_SUPER_TYPE_PUBLIC);
// define default ctor if necessary that passes "null" values to the super ctor
builder.method(isAnnotatedWith(StartsSaga.class).or(isAnnotatedWith(EventHandler.class)))
.intercept(MethodDelegation.to(new ForwardingContextSetupInterceptor<(sagaUnderTest, contextSetter))
.appendParameterBinder(Pipe.Binder.install(Forwarder.class)))
.method(isPublic()
.and(isDeclaredBy(sagaUnderTest.getClass()).or(isDeclaredBy(AbstractSaga.class)
.and(not(isAnnotatedWith(StartsSaga.class))).and(not(isAnnotatedWith(EventHandler.class))))
.intercept(MethodDelegation.to(sagaUnderTest))))
Do I need a different setup for setters?
or is it caused by the abstract class?
I dont really understand why it says bad access to protected data, does it mean the private field?
My type hierachy looks like this. The top most base class which declares the setters:
public abstract class AbstractSaga<SAGA_STATE extends SagaState> implements Saga<SAGA_STATE>, NeedTimeouts, NeedContext {
private SAGA_STATE state;
private boolean completed;
private TimeoutManager timeoutManager;
private ExecutionContext context;
protected AbstractSaga() {
completed = false;
}
// i have omitted some method for clarity
protected ExecutionContext context() {
return context;
}
@Override
public SAGA_STATE state() {
return state;
}
@Override
public void setState(final SAGA_STATE state) {
this.state = state;
}
@Override
public boolean isFinished() {
return completed;
}
protected void setFinished() {
completed = true;
}
@Override
public void setTimeoutManager(final TimeoutManager timeoutManager) {
this.timeoutManager = timeoutManager;
}
}
Extended by:
public abstract class AbstractSpaceSaga<SAGA_STATE extends SpaceSagaState, MESSAGE extends Message>
extends AbstractSaga<SAGA_STATE> {
}
And finally again extended by:
public class ProxyTestSaga
extends AbstractSpaceSaga<SpaceSagaState, TestRequest> {
@StartsSaga
public void handle(final TestRequest request) {
}
@EventHandler
public void handle(final TestEvent event) {
}
}
I hope the code is somehow understandable, I can add more info if required any time.