简体   繁体   English

Foo无法投射到Foo

[英]Foo cannot be cast to Foo

I am looping through ArrayList<Bar> . 我循环遍历ArrayList<Bar> Foo extends Bar . Foo扩展了Bar I'm trying to find an instance of Foo in the list. 我正试图在列表中找到一个Foo实例。

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实例的方法,我可以检查它是否是使用instanceof的Foo instanceof ,然后将BarFoo而没有任何问题:

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 以下是该问题的真正来源: 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. 我正在使用一个名为Bukkit的API,它允许为名为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. 我将去官方的Bukkit论坛并在那里尝试我的问题,因为它变得更具特定于API的问题,但我将把这一切留给那些对这个问题感兴趣的人。

My guess is that you're loading Foo from two places. 我的猜测是你从两个地方加载Foo In your loop, try printing out bar.getClass().getClassLoader() and Foo.class.getClassLoader() . 在循环中,尝试打印出bar.getClass().getClassLoader()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. 基本上,就JVM而言,从不同类加载器加载的两个具有相同名称的类完全不同 - 您不能从一个类转换为另一个类。 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. 请注意,当您使用instanceof ,会检查类型是否真正兼容,因此它注意到不同的类加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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