这个问题与我之前的问题有关:How to create a default constructor with Byte Buddy
我正在创建一个子类,它首先在对某个实例进行委托方法调用之前设置一些上下文。这已经很好地解决了一个问题。
加载动态创建的子类时出现以下错误。
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)
这是目前我的字节伙伴设置,它适用于几乎所有情况,除了导致显示错误 的“ setTimeoutManager
”和“ ”方法。setState
// 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))))
我需要为二传手设置不同的设置吗?
还是由抽象类引起的?
我真的不明白为什么它说对受保护数据的访问不正确,这是否意味着私有字段?
我的类型层次结构看起来像这样。声明设置器的最顶层基类:
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;
}
}
扩展:
public abstract class AbstractSpaceSaga<SAGA_STATE extends SpaceSagaState, MESSAGE extends Message>
extends AbstractSaga<SAGA_STATE> {
}
最后再次扩展:
public class ProxyTestSaga
extends AbstractSpaceSaga<SpaceSagaState, TestRequest> {
@StartsSaga
public void handle(final TestRequest request) {
}
@EventHandler
public void handle(final TestEvent event) {
}
}
我希望代码可以理解,如果需要,我可以随时添加更多信息。