英文:
Endless loop and StackOverflow with a proxy
问题
Sure, here's the translated code portion:
我正在尝试做一个棘手的事情。我有一个方法 `ObjectFactory.getObject()`,它返回一个需要实现 `Serializable` 接口的 `Object`。我试图创建一个已经存在的 `ObjectFactory` 实例的代理,拦截它的 `getObject()` 方法,然后将返回的对象包装到一个实现 `Serializable` 接口的代理中。
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
...
ObjectFactory<?> serializableObjectFactory = (ObjectFactory<?>) Proxy.newProxyInstance(CurrentClass.class.getClassLoader(), new Class[]{ObjectFactory.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//在这里我遇到了 StackOverflow 异常。我猜,它只是递归地调用了自身。不知道为什么会这样。
Object result = ((ObjectFactory<?>) proxy).getObject();
//在这里让对象实现 Serializable 接口
return Mixin.create(new Class[]{Serializable.class, result.getClass()}, new Object[]{result});
}
});
...
return super.get(name, serializableObjectFactory);
}
当代码执行到 Object result = ((ObjectFactory<?>) proxy).getObject()
时,似乎会递归地调用自身(我遇到了 StackOverflow
异常),而我不知道为什么会这样。
我有两个关于这个问题的猜测,但不能确定:
- 我可能错误地使用了代理。但是我已经检查过,似乎是正确的。
- 在
invoke()
方法中的对象代理可能是一个代理,因此它可能在无限循环中调用自身。除了Advise
之外,我没有找到如何将这个代理解包装成一个真实的对象,但它并不适用于这个代理。
您有关于问题原因的猜测吗?
英文:
I'm trying to make the tricky thing. I have method ObjectFactory.getObject()
which returns an Object
that needs to implement Serializable
. I'm trying to make a proxy of an already existing instance of ObjectFactory
, intercept its method getObject()
and then wrap returned object to a proxy, which implements Serializable
.
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
...
ObjectFactory<?> serializableObjectFactory = (ObjectFactory<?>)Proxy.newProxyInstance(CurrentClass.class.getClassLoader(), new Class[]{ObjectFactory.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//Here I'm getting StackOverflow exception. I guess, it just invokes itself recursively. Don't know why
Object result = ((ObjectFactory<?>) proxy).getObject();
//Here I make object implement Serializable
return Mixin.create(new Class[]{Serializable.class, result.getClass()}, new Object[] {result});
}
});
...
return super.get(name, serializableObjectFactory);
}
When code comes to Object result = ((ObjectFactory<?>) proxy).getObject()
it seems just invokes itself recursively(I'm getting StackOverflow
exception) and I have no idea why.
I have two guesses about which I can't be sure:
- I use proxy in the wrong way. But I checked and it seems to be fine.
- Object proxy in method
invoke()
comes as a proxy so it might invoke itself in an endless loop. I didn't find how to unwrap this proxy to a real object besidesAdvise
, but it doesn't suit this proxy.
Do you have any guesses what's wrong?
答案1
得分: 1
让我们看一下这一行:
Object result = ((ObjectFactory<?>) proxy).getObject();
你认为会发生什么?
- 它调用了代理。
- 代理调用了您的 InvocationHandler。
- 回到步骤1。
也许尝试在 objectFactory
上调用方法?
英文:
Let's take a look at this line:
Object result = ((ObjectFactory<?>) proxy).getObject();
What do you think is going to happen?
- It invokes the proxy.
- The proxy invokes your InvocationHandler.
- Go back to step 1.
Maybe call the method on objectFactory
instead?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论