Reputation: 872
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?
Upvotes: 1
Views: 1106
Reputation: 18336
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.
Upvotes: 2
Reputation: 21984
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.
Upvotes: 4