简体   繁体   English

从Ruby调用Java时,JRuby无法将类型强制转换为自身

[英]JRuby failing to coerce a type to itself when calling java from ruby

I'm trying to interface with java classes in a server to use them with ruby, and I need to provide callback classes. 我正在尝试与服务器中的Java类进行接口以将它们与ruby一起使用,并且我需要提供回调类。 Unfortunately, JRuby seems to be trying to coerce a type to itself, and I don't understand why it's doing this and failing trying to do so. 不幸的是,JRuby似乎试图将一种类型强制给自己,而我不明白为什么它这样做并且没有尝试这样做。

I have the following method in ruby 我在Ruby中有以下方法

def registerEvent(id, type, priority = :normal, ignoreCancelled = false, &handler)
  fullClassName = "org.bukkit.event." + type.join(".")
  fullType = java.lang.Class.forName fullClassName

  eventHandler = net.connorcpu.plugscript.EventHandler.new

  eventHandler.setHandlerId id
  eventHandler.setPriority matchToEnum(priority.to_s, org.bukkit.event.EventPriority)
  eventHandler.setEventType fullType
  eventHandler.setIgnoreCancelled ignoreCancelled

  handlerWrapper = EventExecutorWrapper.new(self, handler)
  eventHandler.setExecutor handlerWrapper

  puts eventHandler.java_class.to_s

  $context.registerEvent eventHandler # line 44 from the stacktrace
  return eventHandler
end

Being used like this 这样被使用

registerEvent(
  "edit the quit message because reasons",
  [:player, :PlayerQuitEvent]
) {|event|
  puts 'test message'
}

And when I try to use it, I'm getting the following stacktrace 当我尝试使用它时,我得到以下堆栈跟踪

[21:34:05 WARN]: TypeError: failed to coerce net.connorcpu.plugscript.EventHandler to net.connorcpu.plugscript.EventHandler
  registerEvent at <script>:44
         (root) at <script>:1
            run at SourceFile:617
[21:34:05 ERROR]: [PlugScript] [PlugScript] An exception occurred initializing the script file .\plugins\dev.rb
javax.script.ScriptException: org.jruby.embed.EvalFailedException: (TypeError) failed to coerce net.connorcpu.plugscript.EventHandler to net.connorcpu.plugscript.EventHandler
        at org.jruby.embed.jsr223.JRubyEngine.wrapException(JRubyEngine.java:104) ~[jruby.jar:?]
        at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:121) ~[jruby.jar:?]
        at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:146) ~[jruby.jar:?]
        at net.connorcpu.plugscript.ScriptedPlugin.reloadScript(ScriptedPlugin.java:46) [PlugScript.jar:?]
        at net.connorcpu.plugscript.PlugScript.loadRubyEngines(PlugScript.java:149) [PlugScript.jar:?]
        at net.connorcpu.plugscript.PlugScript.onEnable(PlugScript.java:47) [PlugScript.jar:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:218) [spigot.jar:git-Spigot-1207]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457) [spigot.jar:git-Spigot-1207]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:385) [spigot.jar:git-Spigot-1207]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:302) [spigot.jar:git-Spigot-1207]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:284) [spigot.jar:git-Spigot-1207]
        at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:348) [spigot.jar:git-Spigot-1207]
        at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:325) [spigot.jar:git-Spigot-1207]
        at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:281) [spigot.jar:git-Spigot-1207]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:184) [spigot.jar:git-Spigot-1207]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:430) [spigot.jar:git-Spigot-1207]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [spigot.jar:git-Spigot-1207]
Caused by: org.jruby.embed.EvalFailedException: (TypeError) failed to coerce net.connorcpu.plugscript.EventHandler to net.connorcpu.plugscript.EventHandler
        at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:133) ~[jruby.jar:?]
        at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:118) ~[jruby.jar:?]
        ... 15 more
Caused by: org.jruby.exceptions.RaiseException: (TypeError) failed to coerce net.connorcpu.plugscript.EventHandler to net.connorcpu.plugscript.EventHandler
        at RUBY.registerEvent(<script>:44) ~[?:?]
        at RUBY.(root)(<script>:1) ~[?:?]
        ... 1 more

Thank you for any help on this, I've been stumped on this for a while. 谢谢您的帮助,有一段时间我对此感到困惑。

It turns out, this was an issue relating to classloaders. 原来,这是一个与类加载器有关的问题。 The same class was being used from two unique classloaders in each place of the code. 在代码的每个位置,两个唯一的类加载器正在使用同一类。 The solution was to initialize the JRuby scripting engine with the same classloader as the code creating the scripting environment. 解决方案是使用与创建脚本环境的代码相同的类加载器初始化JRuby脚本引擎。

engine = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.PERSISTENT);
engine.setCompileMode(RubyInstanceConfig.CompileMode.JIT);
engine.setCompatVersion(rubyCompat());
engine.setClassLoader(PlugScript.class.getClassLoader());

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

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