Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > JRuby: How does one keep Java objects as Java objects so they can be used in method calls?

Reply
Thread Tools

JRuby: How does one keep Java objects as Java objects so they can be used in method calls?

 
 
Steve Drach
Guest
Posts: n/a
 
      06-19-2004
I'm trying to use JDOM and JRuby (yes I know about RexML). In the following
program, I get an error when I pass f to sb.build because f is a RubyFile and
not a Java File object. The error I get is:

no method 'build' with argument types matching [org.jruby.RubyFile] (NameError)

What is the correct way to deal with this, I believe, common issue?

Thanks,
Steve

------
require 'java'

class Foo
include_package 'java.io'
include_package 'org.jdom'
include_package 'org.jdom.input'

attr_reader :d

def initialize
sb = SAXBuilder.new
f = File.new "/home/drach/schema/modeler/build.xml"
@d = sb.build f
end
end

foo=Foo.new
 
Reply With Quote
 
 
 
 
Thomas E Enebo
Guest
Posts: n/a
 
      06-19-2004
On Sat, 19 Jun 2004, Steve Drach defenestrated me:
>
> I'm trying to use JDOM and JRuby (yes I know about RexML). In the following
> program, I get an error when I pass f to sb.build because f is a RubyFile and
> not a Java File object. The error I get is:
>
> no method 'build' with argument types matching [org.jruby.RubyFile]
> (NameError)
>
> What is the correct way to deal with this, I believe, common issue?


Well one way is to create a 'java_alias' ala:

java_alias :JFile, :File

then use 'JFile.new "/etc/blah"'

I actually think that your original code should have worked, but
it doesn't. In ruby:

module One
class File
def initialize(a, b)
end
end
end

Class Two
include One

def foo
p File.new("/etc/motd", "r")
end
end

Will see One::File instead of File. At the surface I would think
that include_package should work similiarly. I will likely change this
behavior before next release unless someone can explain why doing so
would be a bad idea.

-Tom

--
+ http://www.tc.umn.edu/~enebo +---- (E-Mail Removed) ----+
| Thomas E Enebo, Protagonist | "A word is worth a thousand |
| | pictures" -Bruce Tognazzini |


 
Reply With Quote
 
 
 
 
Steve Drach
Guest
Posts: n/a
 
      06-19-2004
> Well one way is to create a 'java_alias' ala:
>
>java_alias :JFile, :File
>
> then use 'JFile.new "/etc/blah"'


Ok, I think I understand. It's because the constant File was already
defined to be the org.jruby.RubyFile class. Is that correct? And
java_alias just deals with Java constants? Is that why alias wouldn't
work, since it would have aliased the RubyFile to JFile? It's all
a little subtle for me.

In any case, it seems to work, and both File and SAXBuilder are
the correctly scoped type (symbol?) now. However it now fails
mysteriously, not telling me which method is missing:

jruby-0.7.0/bin/jruby.sh test.rb
Foo::SAXBuilder
Foo::JFile
Exception in thread "main" java.lang.NoSuchMethodError
at org.jruby.javasupport.JavaSupport.createRaiseExcep tion(JavaSupport.java:83)

The new code is:

require 'java'

class Foo
include_package 'java.io'
include_package 'org.jdom'
include_package 'org.jdom.input'

java_alias :JFile, :File

attr_reader :d

def initialize
sb = SAXBuilder.new
p sb.class
f = JFile.new "/home/drach/schema/modeler/build.xml"
p f.class
@d = sb.build f
end
end

foo = Foo.new
 
Reply With Quote
 
Thomas E Enebo
Guest
Posts: n/a
 
      06-19-2004
On Sun, 20 Jun 2004, Steve Drach defenestrated me:
> >
> > Well one way is to create a 'java_alias' ala:
> >
> >java_alias :JFile, :File
> >
> > then use 'JFile.new "/etc/blah"'

>
> Ok, I think I understand. It's because the constant File was already
> defined to be the org.jruby.RubyFile class. Is that correct? And
> java_alias just deals with Java constants? Is that why alias wouldn't
> work, since it would have aliased the RubyFile to JFile? It's all
> a little subtle for me.


yep. I was not the designer of the java integration so I can only
make an educated guess, but I think a decision was made to lazily
load classes from an imported package when they are actually used.
It implements this via const_missing. Obviously, for File, const_missing
is never fired, so I think they made java_alias as a way around this
problem. I sent some more thoughts on this to jruby-dev last night.
Any continued conversation about this should probably continue there.

> In any case, it seems to work, and both File and SAXBuilder are
> the correctly scoped type (symbol?) now. However it now fails
> mysteriously, not telling me which method is missing:


In the current CVS tree I think this problem is fixed (along
with many others). I tried running your script (using another
xml file) and I did not get your error (no news is good news? ).

A problem was corrected where it was not properly finding the
correct overloaded method in a java class. SaxBuiler.build has
a about thousand versions, so I am guessing it has tripped over that
bug.

-Tom

PS- Could you send any other jruby questions to jruby-user or
jruby-dev (hosted by sourceforge)? It will make jruby not look
so dead

--
+ http://www.tc.umn.edu/~enebo +---- (E-Mail Removed) ----+
| Thomas E Enebo, Protagonist | "A word is worth a thousand |
| | pictures" -Bruce Tognazzini |


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can Groovy be used in an applet and/or can it generate the Java bytecodes that then can be used in an applet? Casey Hawthorne Java 1 03-18-2009 12:56 AM
Email my List of 50,000 Genuine Opportunity Seekers for $17 One-TimePayment and They are All Yours To Keep Forever RG HTML 0 09-05-2008 03:17 PM
Does one "call a method" when polymorphism is used? Stefan Ram Java 13 01-05-2008 08:24 AM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments