Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JRuby limitations when working with Java Classes

Tags:

java

ruby

jruby

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?
like image 527
Jakub Arnold Avatar asked Oct 19 '09 15:10

Jakub Arnold


1 Answers

  • 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.

like image 107
Pesto Avatar answered Sep 28 '22 02:09

Pesto