Type mismatch when calling Java method with a class (using .java_class)


#1

Hi,

While porting some existing Java code to JRuby I’m running into an issue
probably caused by my lack of knowlegde here. If someone could shed some
light on this, that would be most helpful.

I’ve reduced the problem to a miminal example. I’m running into this
error when running ‘jruby test.rb’:

test.rb:6: for method foo expected [#<Java::JavaClass:0x5fBottom line
is that a13338>];
got: [org.jruby.RubyModule]; error: argument type mismatch (TypeError)

Now test.rb is:

require ‘java’
require ‘test.jar’
include_class “test.MyTest”

my_test = MyTest.new
my_test.foo(Java::MyTest::someValue.java_class)

and test/MyTest.java is:

package test;

public class MyTest {

static String someValue;

public void foo(java.lang.Class klass) {
}

}

and test.jar simply contains test/MyTest.class.

Any hints how I can accomplish passing the Java class?

Thanks,

Menno


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#2

Hi,

Menno Jonkers wrote:

[about an argument type mismatch error I’m running into]

I have to decide how to proceed with this port tomorrow and would be
very grateful if someone could give me a pointer on how to move on.

(I’ve done a fair share of Googling and reading the wiki, but fail to
see where I err…).

Thanks!

Menno


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

Though I’m much more of a Java programmer than a Ruby programmer, have
you tried calling
my_test.foo(Java::MyTest::someValue.java_class.to_java)? Your issue is
that .java_class is putting out a Ruby module, as you would want for
working with it in Ruby, and the Java VM is a bit picky about its object
types.

Menno Jonkers wrote:

(I’ve done a fair share of Googling and reading the wiki, but fail to
http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#4

Hi Jacob, Charlie,

Charles Oliver N. wrote:

Jacob K. wrote:

Though I’m much more of a Java programmer than a Ruby programmer, have
you tried calling
my_test.foo(Java::MyTest::someValue.java_class.to_java)?
[…]
Actually just passing .java_class should work…

That’s what I thought, but the call

my_test.foo(Java::MyTest::someValue.java_class)

still throws the

test.rb:6: for method foo expected [#Java::JavaClass:0x5fa13338];
got: [org.jruby.RubyModule]; error: argument type mismatch (TypeError)

leaving me puzzled :-/ Any chance that something broken in my
environment/setup causes this?

Cheers,

Menno

----- example code -----

Test.rb is:

require ‘java’
require ‘test.jar’
include_class “test.MyTest”

my_test = MyTest.new
my_test.foo(Java::MyTest::someValue.java_class)

and test/MyTest.java is:

package test;

public class MyTest {

static String someValue;

public void foo(java.lang.Class klass) {
}

}

and test.jar simply contains test/MyTest.class.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#5

Charles Oliver N. wrote:

Can you check what that java_class call is actually returning?

p Java::MyTest::someValue.java_class

returns

Java::MyTestSomeValueJava_class

Cheers,

Menno


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#6

Menno Jonkers wrote:

That’s what I thought, but the call

my_test.foo(Java::MyTest::someValue.java_class)

still throws the

test.rb:6: for method foo expected [#Java::JavaClass:0x5fa13338];
got: [org.jruby.RubyModule]; error: argument type mismatch (TypeError)

leaving me puzzled :-/ Any chance that something broken in my
environment/setup causes this?

Can you check what that java_class call is actually returning?

This may also be a problem with our logging of dispatch mismatches
that’s fixed on master. If you build from master you may get a better
error that says what’s actually happening.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#7

Jacob K. wrote:

Though I’m much more of a Java programmer than a Ruby programmer, have
you tried calling
my_test.foo(Java::MyTest::someValue.java_class.to_java)? Your issue is
that .java_class is putting out a Ruby module, as you would want for
working with it in Ruby, and the Java VM is a bit picky about its object
types.

Actually just passing .java_class should work…

âž” jruby -rjava -e “p
java.lang.Compiler.compile_class(org.jruby.RubyString.java_class)”
false

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#8

Charles Oliver N. wrote:

Does the class you’re trying to pass in actually work? I suspect
it’s not loading it at all.

That simple question seems to have pushed me in the right direction.
Making some progress now. Not able to spend much time on it today and
haven’t fully seen the light yet, but things are moving again. Thanks!

Menno


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#9

Menno Jonkers wrote:

Charles Oliver N. wrote:

Can you check what that java_class call is actually returning?

p Java::MyTest::someValue.java_class

returns

Java::MyTestSomeValueJava_class

Ahh ok, you’re not actually getting the class here…it’s trying to turn
your sequence of stuff into one long java package…

Does the class you’re trying to pass in actually work? I suspect it’s
not loading it at all.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email