views:

530

answers:

2

As far as I understand JRuby, it is perfectly possible to use Java class inside JRuby code and vice versa, however, I still don't understand few things.

  • can JRuby work with Java Annotations?
  • is it possible to use reflection from JRuby on Java class?
  • is it possible to use reflection from Java on JRuby class?
  • do I have executable classes in JRuby?
  • is it possible to redefine Java class inside of JRuby script? (the same way as I can redefine eg. Integer in C Ruby)
  • are there any other limitations, that prevent using JRuby as part of any Java application?
+6  A: 
  • can JRuby work with Java Annotations?

No. Ruby doesn't have annotations. The ruby2java compiler will allow you to add annotations that are used when compiling to a class file, though.

  • is it possible to use reflection from JRuby on Java class?

Yes:

java.util.Vector.methods.include? '[]'  #  => true
  • is it possible to use reflection from Java on JRuby class?

When embedding JRuby using BSF or JSR223? Only to the extent that those technologies allow it. When using ruby2java? Yes. It generates normal Java .class files.

  • do I have executable classes in JRuby?

I'm not exactly sure what you're asking.

  • is it possible to redefine Java class inside of JRuby script? (the same way as I can redefine eg. Integer in C Ruby)

Yes, you can monkey patch in JRuby, but the changes aren't visible from the Java side, just JRuby:

import java.util.Vector
class Vector
  def foo
    "foo!"
  end
end
v = java.util.Vector.new
v.foo                       #  => "foo!"
  • are there any other limitations, that prevent using JRuby as part of any Java application?

Plenty of little gotchas abound when using Java from JRuby. ruby2java is still in its infancy, and I'm not sure it's ready for a production environment yet. Other than that, the focus has been more on scripting with BSF and JSR223, which may or may not suit your purposes.

Pesto
executable classes means that class definition is executable code, eg. http://yehudakatz.com/2009/06/04/the-importance-of-executable-class-bodies/ or http://split-s.blogspot.com/2005/12/rubys-executable-class-definitions.html
Darth
In that case, yes, JRuby classes are exactly like Ruby classes.
Pesto
+2  A: 

JRuby 1.4 does contain some support for annotations on Ruby classes, but only at runtime. Look at http://github.com/nicksieger/ruby-jersey for an example of using the new runtime class generation to interop with annotation-based frameworks.

The rest of Pesto's answers are pretty much right.

Charles Oliver Nutter