Java:ClassCastException;同一个ClassLoader?

时间:2014-04-14 23:33:12

标签: java classloader classcastexception bukkit

我在施法方面遇到麻烦,我无法弄明白。根据我的研究,当你将超类投射到你的班级时,导致ClassCastException的原因是,他们都使用不同的类加载器。

我做了一个简单的System.out.println()来检查类加载器是否不同,但它们都打印出相同的结果,@11cb01a

为了达到我想要完成的目标,我正在尝试为自己创建一个简单的API来制作迷你游戏(我正在使用Bukkit API)并且我在两个位置发现了错误拥有ClassCastException。首先,我有两个类,ArenaBaseArenaManagerBase。我有一个名为ArenaManager的课程,其ArenaManagerBase延伸,Arena延伸ArenaBase

当我收到ClassCastException ArenaManager的错误时,它会调用ArenaManager.getInstance()并将(ArenaManager)投射到该ArenaManager。首次启动时,我会初始化ArenaManager.init(this);

public static void init(JavaPlugin instance) {
    am = new ArenaManagerBase(); // What is returned when calling getInstance()
    am.plugin = instance;
    am.ac = new ArenaConfigBase(am.plugin);
    am.init(); // Separate initializer for any subclass that might need it.
}

然后,在init内部我调用了一些东西。

ArenaManager am = (ArenaManager) ArenaManager.getInstance();

抛出错误的部分是:

ClassLoader

所以就这一点而言,我有一个我想要强制转换的类,这是另一个类的超类。他们似乎都有相同的null。我做错了什么,我怎么能解决它?

修改:
这是堆栈跟踪。这很奇怪,因为在顶部它显示CastClassException然后在底部它会考虑[18:36:10 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'cast ledefend' in plugin DefendTheCastle vAlpha at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17 5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146 ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6 55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2 50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5 45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6 17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] Caused by: java.lang.ClassCastException: me.codermusgrove.arenaapi.ArenaManagerB ase cannot be cast to me.valdeon.defendthecastle.arena.ArenaManager at me.valdeon.defendthecastle.cmd.CmdCreateArena.onCommand(CmdCreateAren a.java:27) ~[?:?] at me.valdeon.defendthecastle.cmd.BaseCommand.onCommand(BaseCommand.java :30) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] ... 13 more

{{1}}

1 个答案:

答案 0 :(得分:2)

可以使用父类型变量引用子类的实例。但你无法做到相反。虽然,您可以通过使用类似 - (ArenaManager) ArenaManager.getInstance()的显式转换来绕过编译器错误检查,但它最终会在运行时使用CCE失败。

ArenaManagerBase是父级,ArenaManager是您的孩子。您拥有的实例是父实例(即new ArenaManagerBase()),并且您正试图将其转换为子实例。