简体   繁体   中英

Foo cannot be cast to Foo

I am looping through ArrayList<Bar> . Foo extends Bar . I'm trying to find an instance of Foo in the list.

ArrayList<Bar> list = new ArrayList<Bar>();

// fill list with items that are instances of Foo and Bar

for (int i = 0; i < list.size(); i++)
{
  Bar bar = list.get(i);

  if (bar.getClass().getName().equals("com.me.Foo")) // "if(bar instanceof Foo)" never passes
  {
    System.out.println("bar is an instance of Foo");
    Foo foo = (Foo) bar;
  }
}

If this section of code gets run I get this error:

java.lang.ClassCastException: com.me.Foo cannot be cast to com.me.Foo
// stack

However, in another spot, I have a method that returns an instance of Bar and I can check to see if it's an instance of Foo using instanceof and then cast Bar to Foo with no problems:

Bar bar = returnBar(); // returns an instance of Bar, but the variable 
                       // it returns could be an instance of Foo
if (bar instanceof Foo)
{
  System.out.println("bar is an instance of Foo");
  Foo foo = (Foo) bar;
}

What is different here?

Here's the real source of the issue: http://pastie.org/private/jhyhovn7mm4ghxlxsmweog

I'm working with an API called Bukkit which allows for custom plugins for servers for a game called Minecraft. The documentation can be found here for those really interested in the issue. I'm going to go to the official Bukkit forums and try my question there, since it is becoming more of an API specific issue, but I'll leave all this here for those that were interested in the problem.

My guess is that you're loading Foo from two places. In your loop, try printing out bar.getClass().getClassLoader() and Foo.class.getClassLoader() . I suspect they'll show different classloaders. (It's possible that you've got two classloaders loading from the same place, of course, which is equally bad.)

Basically, as far as the JVM is concerned, two classes with the same name loaded from different classloaders are entirely different - you can't cast from one to the other. Assuming I'm right about this, the usual solution is to try to work out why they're being loaded from different classloaders, and fix that.

Note that when you use instanceof instead, that checks whether the types are really compatible, so it will notice the different classloaders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM