Access to java class objects

Hi all,
occasionally I need to get hold of java class objects (e.g. int.class or
javax.swing.JLabel.class) from my jruby code. I java I would do
Class cls = javax.swing.JLabel.class;
What is the jruby equivalent? Currently I do
irb(main):002:0> Java::java.lang.Class.forName “javax.swing.JLabel”
=> class javax.swing.JLabel
which looks rather complicated. The obvious thing
irb(main):003:0> javax.swing.JLabel.class
=> Class
does not work as expected.
Any suggestions?

Many TIA,
– Jan

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Wed, Mar 17, 2010 at 5:20 AM, Jan H. [email protected]
wrote:

Any suggestions?
Generally the .java_class result should work anywhere you’d pass a
java.lang.Class, since we convert the two back and forth
automatically. If you really need the java.lang.Class object, however,
what you have above may be the best way in 1.4, and there’s a new way
in 1.5:

~/projects/jruby/tmp2/railsproblem âž” jruby -rjava -e “p
java.lang.String.to_java”
class java.lang.String

~/projects/jruby/tmp2/railsproblem âž” jruby -rjava -e “p
java.lang.String.to_java.class”
Java::JavaLang::Class

This is part of an effort to deemphasize Java::JavaClass instances and
to provide to_java consistently across all objects in the system.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Am 17.03.2010 um 22:11 schrieb Charles Oliver N.:

does not work as expected.
class java.lang.String

~/projects/jruby/tmp2/railsproblem âž” jruby -rjava -e “p
java.lang.String.to_java.class”
Java::JavaLang::Class

This is part of an effort to deemphasize Java::JavaClass instances and
to provide to_java consistently across all objects in the system.

Thanks for your answer, but your suggestions produce different results
here:

$ jruby -rjava -e “p java.lang.String.to_java”
Java::JavaLang::String
$ jruby -rjava -e “p java.lang.String.to_java.class”
Class
$ jruby --version
jruby 1.5.0.dev (ruby 1.8.7 patchlevel 174) (2010-03-09 a659b27) (Java
HotSpot™ 64-Bit Server VM 1.6.0_17) [x86_64-java]

Why is that?

– Jan


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Am 18.03.2010 um 11:16 schrieb Jan H.:

which looks rather complicated. The obvious thing

Why is that?

Sorry guys, with a more recent 1.5 this seems to work fine:

$ jruby --version
jruby 1.5.0.dev (ruby 1.8.7 patchlevel 174) (2010-03-18 0cb3e3c) (Java
HotSpot™ 64-Bit Server VM 1.6.0_17) [x86_64-java]
$ jruby -rjava -e “p java.lang.String.to_java”
class java.lang.String
$ jruby -rjava -e “p java.lang.String.to_java.class”
Java::JavaLang::Class

Sorry for the confusion.

Cheers,
– Jan


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Am 18.03.2010 um 18:06 schrieb Charles Oliver N.:

Sorry for the confusion.

Yes, it’s probably less than a week old. We move quickly!

And the recent changes include a solution for my question about class
object access. Very good you guys have set clairvoyance=true (-:

Keep up the good work!

Cheers,
– Jan


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Thu, Mar 18, 2010 at 5:34 AM, Jan H. [email protected]
wrote:

Sorry guys, with a more recent 1.5 this seems to work fine:

$ jruby --version
jruby 1.5.0.dev (ruby 1.8.7 patchlevel 174) (2010-03-18 0cb3e3c) (Java HotSpot™ 64-Bit Server VM 1.6.0_17) [x86_64-java]
$ jruby -rjava -e “p java.lang.String.to_java”
class java.lang.String
$ jruby -rjava -e “p java.lang.String.to_java.class”
Java::JavaLang::Class

Sorry for the confusion.

Yes, it’s probably less than a week old. We move quickly!

  • Charlie

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