开发者

isAssignableFrom function of Class.java

开发者 https://www.devze.com 2022-12-10 09:43 出处:网络
I am trying following Context ctx = (Context) jndiCntx.lookup(fSTANDARD_ENVIRONMENT); Object obj = ctx.lookup(fSTANDARD_JNDINAME);

I am trying following

Context ctx = (Context) jndiCntx.lookup(fSTANDARD_ENVIRONMENT);

Object obj = ctx.lookup(fSTANDARD_JNDINAME);

And following code is returning me false

MyClass.class.isAssignableFrom(obj.getClass())

although

MyClass.class.getName().equalsIgnoreCase(obj.getClass().getName()) returns true.

I am not able to cast obj to MyClass as it th开发者_运维问答rows ClassCastException.

What could be the issue?


My guess is that the class has been loaded by two different classloaders.

Look at obj.getClass().getClassLoader() vs calling getClassLoader() in your current code.

Is the class itself available from two different jar files? That's normally an easy one to sort out. It's harder if you've got two separate classloaders which both use the same jar file.

What container are you running in? I suggest you look at the container-specific documentation for classloaders... for example, here's the Tomcat 5.5 ClassLoader how-to.


hang on, why are you calling "equalsIgnoreCase" to check the classnames rather than just plain "equals"? Is it possible you've got a conflict between (nearly) matching names?

0

精彩评论

暂无评论...
验证码 换一张
取 消