gpt4 book ai didi

java - Jruby "RuntimeHelpers"类已弃用

转载 作者:行者123 更新时间:2023-12-01 09:50:05 25 4
gpt4 key购买 nike

我有一个基于JRuby on Rails的项目,现在我正在将该项目迁移到jruby-9.0.5.0版本。

在代码中,从Java文件调用ruby,如下所示:

    String source = new StringBuilder("require 'java'\n" +
"\n" +
"java_package 'jrubysource'\n" +

"class SystemFilePath\n" +
" attr_accessor :topDirCmd, :topDirNames\n" +
" java_signature 'SystemFilePath(String topDirCmd)'\n" +
" def initialize(topDirCmd)\n" +
" @topDirCmd = topDirCmd\n" +
" setTopDirectoryNames()\n" +
" end\n" +
"\n" +
" java_signature 'void setTopDirectoryNames()'\n" +
" def setTopDirectoryNames\n" +
" Open3.popen3(@topDirCmd) { |stdin, stdout, stderr, wait_thread|\n" +
" out = stdout.read\n" +
" err = stderr.read\n" +
" if err.size > 0 || out.size == 0\n" +
" @topDirNames = nil\n" +
" return\n" +
" end\n" +
" out = out.split(/\\n/).select{|a| a.match(/system_1/) && !a.match(/system_1\\/sim/)}.join(\"\\n\") + \"\\n\";\n" +
" topDirNames = out.split(/\\//).select{|a| a.match(/\\n$/) }.map(&:chomp)\n" +
" if topDirNames.size == 0 then\n" +
" @topDirNames = nil\n" +
" return\n" +
" end\n" +
" @topDirNames = topDirNames.sort.reverse\n" +
" }\n" +
" rescue Exception => e\n" +
" @topDirNames = nil\n" +
" end\n" +
"\n" +
"end\n" +
"").toString();
__ruby__.executeScript(source, "system_file_path.rb");
RubyClass metaclass = __ruby__.getClass("SystemFilePath");
metaclass.setRubyStaticAllocator(SystemFilePath.class);
if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: SystemFilePath");
__metaclass__ = metaclass;
}


public void setTopDirectoryNames() {
@SuppressWarnings("unused")
IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "setTopDirectoryNames"); //Here i am calling ruby code from java file.
return;
}

现在,根据 JRuby API document “RuntimeHelpers”类已弃用。

该文档没有提供替代 API。请引导我找到替代方案,因为这对我来说是一个障碍。

最佳答案

它是一个内部 JRuby API ...您可以为每个 IRubyObject 使用类似的 API:

而不是

RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "setTopDirectoryNames")

尝试:

this.callMethod(__ruby__.getCurrentContext(), "setTopDirectoryNames")

关于java - Jruby "RuntimeHelpers"类已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com