[英]Replace InvocationHandler in java.lang.reflect.Proxy
I need to replace InvocationHandler in a Proxy object. 我需要替换Proxy对象中的InvocationHandler。 But there is only getter for it and no setter. 但是,只有吸气剂而不是吸气剂。 Why is this so and is there any workaround? 为什么会这样,并且有任何解决方法?
Thanks 谢谢
I think you'll need to keep the same invocation handler but have it change its behaviour. 我认为您需要保留相同的调用处理程序,但要更改其行为。 This will be closer to what you're looking for if you use the State pattern in the invocation handler: 如果在调用处理程序中使用State模式,则将更接近您要查找的内容:
public class Handler implements InvocationHandler
{
HandlerState state = new NormalState();
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Make all method calls to the state object, not this object.
this.state.doStuff();
return this.state.doOtherStuff();
}
public void switchToErrorState(){
this.state = new ErrorState();
}
}
public interface HandlerState
{
public void doStuff();
public String doOtherStuff();
}
public class ErrorState implements HandlerState
{
@Override
public void doStuff() {
// Do stuff we do in error mode
}
@Override
public String doOtherStuff() {
// Do other error mode stuff.
return null;
}
}
public class NormalState implements HandlerState
{
@Override
public void doStuff() {
// Do normal stuff
}
@Override
public String doOtherStuff() {
// Do normal other stuff
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.