Tuesday, March 15, 2011

Casting Java Objects From JRuby

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?

From stackoverflow
  • 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

0 comments:

Post a Comment