Direct embedding: calling java methods on object passed indirectly to ruby

Dear Gurus,

I have a little java-ruby integration issue that you might be able to
answer straight away:

  • am using the old direct embedding API
    (http://kenai.com/projects/jruby/pages/DirectJRubyEmbedding) with
    Jruby 1.2 (I know - I should upgrade and will, as soon as 1.5 comes
    out)
  • the embedded .rb code calls a (static) java method and passes a
    RubyArray as a java List argument
  • that method creates java objects, adds them to the list
  • back in ruby, I find those objects in the list, but they are
    ‘opaque’; can’t see or call any of the java methods on them.

I even had difficulty in see the java class of these,
o.class says JavaJavaObject,
o.java_class says #Java::JavaClass:0x4eefb4 , but
o.java_class.name gives me the right answer(the class I was
instantiating in the method). Btw:
o.java_proxy? says true

Should I wrap/unwrap the java object somehow? I could not find any of
the java methods on the objects (o.methods).
Was also looking wiki pages and JavaUtilities , could not found
anything useful for this case.

Thanks for any hints,

Gergo


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

It sounds like the Java objects are coming back not as their “proxied”
type (the nice Ruby-friendly wrapper you want to see) but instead as
the JavaObject “internal” wrapper. I’m not sure of the best way to get
at the proxied version. Do you have any code we could use to reproduce
this?

On Tue, Mar 30, 2010 at 8:47 AM, Gergely N. [email protected] wrote:

 * that method creates java objects, adds them to the list
Should I wrap/unwrap the java object somehow? I could not find any of

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks for your reply, Charlie.

2010/4/3 Charles Oliver N. [email protected]:

It sounds like the Java objects are coming back not as their “proxied”
type (the nice Ruby-friendly wrapper you want to see) but instead as
the JavaObject “internal” wrapper. I’m not sure of the best way to get
at the proxied version. Do you have any code we could use to reproduce
this?

I just distilled it to the snippets here: http://gist.github.com/355817

As mentioned there, this seems working since 1.4. This suggests to me
that my code was correct, and there is no need for any extra wrapping
code even with the ‘direct embedding’ case.
Actually the embedding mechanism doesn’t matter.

So, I wish I could upgrade… but a few things hold me off that, eg. an
exception like this:
“RuntimeError: Java wrapper with no contents”
when passing a block to a method taking a java.lang.Runnable.
Before I start debugging into this (and/or filing bugs for it): would
you have any ideas? in which cases would this happen?
On JRuby 1.2, the same code works fine, but it could still be
something in my code/setup/classpath/etc…

Cheers, Gergo

  • the embedded .rb code calls a (static) java method and passes a
    o.java_proxy? says true

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Sun, Apr 4, 2010 at 7:28 PM, Gergely N. [email protected] wrote:

I just distilled it to the snippets here: http://gist.github.com/355817

Confirmed working on 1.5 and broken on 1.2, so that’s something:

~/projects/jruby âž” jruby -rjava array_bug.rb
Got [email protected] inspect: [email protected] class:Java::bug::Food
javaclass:bug.Food
Digesting 3 of apple
Got [email protected] inspect: [email protected] class:Java::bug::Food
javaclass:bug.Food
Digesting 999 of spam

~/projects/jruby âž” …/jruby-1.2.0/bin/jruby -rjava array_bug.rb
Got [email protected] inspect: #Java::JavaObject:0x11d2066
class:Java::JavaObject javaclass:bug.Food
1.2.0 can’t digest this, is it a bug?
Got [email protected] inspect: #Java::JavaObject:0x7616ad
class:Java::JavaObject javaclass:bug.Food
1.2.0 can’t digest this, is it a bug?

As mentioned there, this seems working since 1.4. This suggests to me
that my code was correct, and there is no need for any extra wrapping
code even with the ‘direct embedding’ case.
Actually the embedding mechanism doesn’t matter.

Yes, your code was probably right. I think I remember some bugs fixed
relating to getting elements out of Java arrays.

Here’s a workaround that might help you survive a bit longer:

require ‘jruby’
def feed
foods = []
Java::bug::Food.feed(foods)
foods.each do |f|
puts “Got #{f} inspect: #{f.inspect} class:#{f.class}
javaclass:#{f.java_class}”
# JRuby.reference gives you a reference to the actual JavaObject
instance
# dataGetStruct gets the real object out of it, wrapping it
correctly
f = JRuby.reference(f).data_get_struct if Java::JavaObject === f
begin
f.digest
rescue NoMethodError
puts “#{JRUBY_VERSION} can’t digest this, is it a bug?”
end
end
end

I wouldn’t expect this to be very fast though. If you can, another
workaround is to use a real Java collection rather than a Ruby array:

def feed
food = java.util.ArrayList.new

Looks like the bug was in using a Ruby Array as a List, and things not
coercing right going into or coming out of it.

So, I wish I could upgrade… but a few things hold me off that, eg. an
exception like this:
“RuntimeError: Java wrapper with no contents”
when passing a block to a method taking a java.lang.Runnable.
Before I start debugging into this (and/or filing bugs for it): would
you have any ideas? in which cases would this happen?

This is on 1.4 I assume? It certainly should be working now. Have a
reproduction?

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Wed, Apr 7, 2010 at 7:58 PM, Gergely N. [email protected] wrote:

It certainly should be working now. Have a reproduction?

Actually, either my memory was at fault and/or things got fixed in the
meantime… but now the simple block->Runnable conversion seems fine:)

It may have been related to some new interface impl code I added that
changed how Java methods are selected. See
http://jira.codehaus.org/browse/JRUBY-4680 for example and the related
fix.

The only thing broken now seems to be:
 - if you make up an AbstractRunnable (implements Runnable) in Java
 - with an abstract doRun method, then
 - try to override this method in Ruby to call a block…
then you’ll get error mentioned above.
I know it’s a pretty roundabout way to do it anyway - it was just one
of my earlier workarounds when I couldn’t the simpler ways working…
Nevertheless, it should work I guess as it does with 1.5.
Just raised a bug for this with some distilled code to reproduce it:
http://jira.codehaus.org/browse/JRUBY-4704

Great, thank you! This may or may not be a regression, may or may not
get into 1.5, but I’m glad you got the bug report in (with a
reproducible case, no less).

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Charles,

2010/4/5 Charles Oliver N. [email protected]:

On Sun, Apr 4, 2010 at 7:28 PM, Gergely N. [email protected] wrote:

I just distilled it to the snippets here: http://gist.github.com/355817

Confirmed working on 1.5 and broken on 1.2, so that’s something:
Great!

As mentioned there, this seems working since 1.4. This suggests to me
that my code was correct, and there is no need for any extra wrapping
code even with the ‘direct embedding’ case.
Actually the embedding mechanism doesn’t matter.

Yes, your code was probably right. I think I remember some bugs fixed
relating to getting elements out of Java arrays.

Here’s a workaround that might help you survive a bit longer:

I wouldn’t expect this to be very fast though. If you can, another
workaround is to use a real Java collection rather than a Ruby array:

Indeed, that java.util.ArrayList makes this work like a charm :slight_smile:
Thanks for that - I should have tried it myself earlier…

“RuntimeError: Java wrapper with no contents”
when passing a block to a method taking a java.lang.Runnable.
Before I start debugging into this (and/or filing bugs for it): would
you have any ideas? in which cases would this happen?

This is on 1.4 I assume?
No, it’s 1.5.

It certainly should be working now. Have a reproduction?

Actually, either my memory was at fault and/or things got fixed in the
meantime… but now the simple block->Runnable conversion seems fine:)
The only thing broken now seems to be:

  • if you make up an AbstractRunnable (implements Runnable) in Java
  • with an abstract doRun method, then
  • try to override this method in Ruby to call a block…
    then you’ll get error mentioned above.
    I know it’s a pretty roundabout way to do it anyway - it was just one
    of my earlier workarounds when I couldn’t the simpler ways working…
    Nevertheless, it should work I guess as it does with 1.5.
    Just raised a bug for this with some distilled code to reproduce it:
    http://jira.codehaus.org/browse/JRUBY-4704

Thanks for your help,
Gergo


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Charlie,

2010/4/8 Charles Oliver N. [email protected]:

On Wed, Apr 7, 2010 at 7:58 PM, Gergely N. [email protected] wrote:
Actually, either my memory was at fault and/or things got fixed in the
meantime… but now the simple block->Runnable conversion seems fine:)

It may have been related to some new interface impl code I added that
changed how Java methods are selected. See
http://jira.codehaus.org/browse/JRUBY-4680 for example and the related
fix.
Cheers for that!

Nevertheless, it should work I guess as it does with 1.5.

Oops this was a typo, of course I meant:
“Nevertheless, it should work I guess as it does with 1.2 … 1.4”

Just raised a bug for this with some distilled code to reproduce it:
http://jira.codehaus.org/browse/JRUBY-4704

Great, thank you! This may or may not be a regression, may or may not

You’re right, the fact that it works with 1.4 does not necessarily
mean it’s a regression per se.
However if 1.5 is stricter/pickier, it should give a clearer error
message.

get into 1.5, but I’m glad you got the bug report in (with a
reproducible case, no less).

I’m glad you found it useful - I played a bit more with this, going to
talk about that on JIRA.

Thinking about next time - what’s the best way to report these
JRuby-specific bugs?
E.g. should I -a newbie- try to create specs or tests for these kind
of problems? Attach them to JIRA and/or share via git?
I can’t remember seeing much info on this yet.
Thanks again, Gergo


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Thu, Apr 8, 2010 at 7:12 PM, Gergely N. [email protected] wrote:

Thinking about next time - what’s the best way to report these
JRuby-specific bugs?
E.g. should I -a newbie- try to create specs or tests for these kind
of problems? Attach them to JIRA and/or share via git?
I can’t remember seeing much info on this yet.

Reporting here on the mailing list is good, but eventually all bugs or
features should go through JIRA. A combination of reporting it in JIRA
and giving us a heads-up here usually works best.

And of course, it would be very helpful if folks like you watched how
we end up fixing your bugs (or similar bugs) so that you’ll start to
get a feel for how JRuby works. Then, perhaps, you’ll be able to
submit patches with bug reports in the future :slight_smile:

  • Cahrlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

2010/4/9 Charles Oliver N. [email protected]:

On Thu, Apr 8, 2010 at 7:12 PM, Gergely N. [email protected] wrote:

Thinking about next time - what’s the best way to report these JRuby-specific bugs?

Reporting here on the mailing list is good, but eventually all bugs or
features should go through JIRA. A combination of reporting it in JIRA
and giving us a heads-up here usually works best.

Of course, it’s clear that real bugs should be reported in JIRA …
and the list is great for clarifying things when ‘jiring’ looks like a
waste of time (as my other case with 1.2 that got fixed since, still I
needed a workaround).

And of course, it would be very helpful if folks like you watched how
we end up fixing your bugs (or similar bugs) so that you’ll start to
get a feel for how JRuby works. Then, perhaps, you’ll be able to
submit patches with bug reports in the future :slight_smile:

Point taken, so ad-hoc test snippets are fine for now - won’t bother
with proper unit tests… until a bit later :slight_smile:

Cheers, Gergo


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs