ClassCastException问题
我有以下代码:
Object backingBean = facesContextHandler.getBackingBean("UserCredentialsBean");
UserCredentialsBean userCredentBean = (UserCredentialsBean) backingBean;
当我调试它时,我在 Eclipse 中的表达式视图中有以下内容:
backingBean.getClass() -> myPackage.UserCredentialsBean
backingBean instanceOf myPackage.UserCredentialsBean -> false
所以上面的转换失败了......
这是怎么回事?
更新:其他“症状”:我在会话超时后遇到此问题
有什么想法吗?
I have the following code:
Object backingBean = facesContextHandler.getBackingBean("UserCredentialsBean");
UserCredentialsBean userCredentBean = (UserCredentialsBean) backingBean;
While I'm debugging it I have the following in my expressions view in Eclipse:
backingBean.getClass() -> myPackage.UserCredentialsBean
backingBean instanceOf myPackage.UserCredentialsBean -> false
So casting above fails...
How can this be?
Update: Additional "symptom": I get this issue after session timeout
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题。我只能想到两种可能性。
1- 空对象。对于 null 对象,instanceOf 通常会失败。只需确保 bean 已初始化即可。
2-类加载器问题。如果同一类的两个对象由两个不同的类加载器加载,则instanceOf将失败。
这不是一个包罗万象的清单,只是我能想到的两件事。
Interesting question. I can only think of two possibilities.
1- Null Object. instanceOf usually fails for null object. Just make sure that the bean is initialized.
2- Class Loader Issue. If two objects of same class are loaded by two different class loaders, then instanceOf will fail.
This is not an all inclusive list, just two things I could think of.
类加载器肯定有问题。最有可能的是应用程序被重新部署(因此创建了新的类加载器实例),但旧对象仍然保留在(磁盘序列化?)会话中或内存中。
类名相同,但类加载器实例不同。 Instanceof 查看完整的类名和类加载器的相等性。
PS 这其实是一个很常见的问题。当后台线程醒来时,却发现应用程序已重新部署,线程的类加载器消失,然后抛出 NoClassDefFound 或 ClassCast ,这常常是可见的,这让那些并不总是意识到这实际上是僵尸的开发人员感到非常有趣。之前的部署,并尝试在其代码中查找错误。
ClassLoader issue for sure. Most probably the application got redeployed (thus new classloader instance was created), but the old object remained either in (disk-serialized?) session or in memory.
Class name is the same, but class loader instance is different. Instanceof looks at complete class name AND equality of classloaders.
P.S. This is actually a quite common issue. It is often visible when a background thread wakes up only to find out that the application was redeployed, the thread's classloader is gone and then it throws either NoClassDefFound or ClassCast to the big amusement of developers who not always realize this is actually a zombie from the previous deployment, and trying to find a bug in their code.