开发者

JRuby limitations when working with Java Classes

As far as I understand JRuby, it is perfectly possible to use Jav开发者_开发知识库a 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?


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


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.


JRuby does now support Annotations via java_annotation but it has some limitations. One serious one in my experience so far is that it requires use of become_java! (which is undocumented). This prevents you from using Annotations on Ruby classes that were subclassed from Java superclasses because of issue 2359.

This is pretty problematic in web programming, where many frameworks use Annotations for their routing system. It effectively means you can't create parent classes for controllers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜