从JRuby铸造Java对象 [英] Casting Java Objects From JRuby
问题描述
我在JRuby使用Java库。我从文件中读取一个对象,我需要将它作为一个不同的对象类型传递给第二个构造函数:
@hmm_model = ObjectInputStream.new(FileInputStream.new(LINGPIPE_MODEL_PATH))
@tagger = HmmDecoder.new(@hmm_model)
@hmm_model是ObjectInputStream类型,需要转换为(HiddenMarkovModel)。显然,这很容易在Java中,它只是:
@tagger = HmmDecoder.new((HiddenMarkovModel)@ hmm_model)
但是,这当然不适用于JRuby。实际上有没有什么方法来显式地将@hmm_model转换成正确的类型?
亮。 JRuby JVM接口足够聪明,可以自我调用,我正在调用构造函数不正确。实际调用是:
@tagger = HmmDecoder.new(@ hmm_model.readObject())
$
JRuby:c $ c>
并且JRuby正确处理类型转换为HiddenMarkovModel。 1
me:0
I'm working with a Java library in JRuby. I'm reading an object from a file, and I need to pass it as a different object type to a second constructor:
@hmm_model = ObjectInputStream.new(FileInputStream.new(LINGPIPE_MODEL_PATH))
@tagger = HmmDecoder.new(@hmm_model)
@hmm_model is of type ObjectInputStream, and needs to be cast to (HiddenMarkovModel). Obviously, that'd be easy in Java, it would just be:
@tagger = HmmDecoder.new((HiddenMarkovModel)@hmm_model)
But, of course, that doesn't work in JRuby. Is there actually any way to explicitly cast the @hmm_model to be of the correct type?
解决方案 So, I'm not very bright. The JRuby JVM interface is smart enough to cast itself, I was making the call to the constructor incorrectly. The actual call is:
@tagger = HmmDecoder.new(@hmm_model.readObject())
and JRuby correctly handles the type conversion to a HiddenMarkovModel.
JRuby: 1
me: 0
这篇关于从JRuby铸造Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!