Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top