简体   繁体   English

使用Java类时JRuby的局限性

[英]JRuby limitations when working with Java Classes

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. 据我了解JRuby,完全可以在JRuby代码中使用Java类,反之亦然,但是,我仍然不了解一些东西。

  • can JRuby work with Java Annotations? JRuby可以使用Java Annotations吗?
  • is it possible to use reflection from JRuby on Java class? 是否有可能在Java类上使用来自JRuby的反射?
  • is it possible to use reflection from Java on JRuby class? 是否有可能在JRuby类上使用Java的反射?
  • do I have executable classes in JRuby? 在JRuby中有可执行类吗?
  • is it possible to redefine Java class inside of JRuby script? 是否有可能在JRuby脚本中重新定义Java类? (the same way as I can redefine eg. Integer in C Ruby) (就像我可以重新定义一样。例如.Cub Ruby中的Integer)
  • are there any other limitations, that prevent using JRuby as part of any Java application? 是否有任何其他限制,阻止使用JRuby作为任何Java应用程序的一部分?
  • can JRuby work with Java Annotations? JRuby可以使用Java Annotations吗?

No. Ruby doesn't have annotations. 不,Ruby没有注释。 The ruby2java compiler will allow you to add annotations that are used when compiling to a class file, though. 但是, ruby2java编译器将允许您添加在编译为类文件时使用的注释。

  • is it possible to use reflection from JRuby on Java class? 是否有可能在Java类上使用来自JRuby的反射?

Yes: 是:

java.util.Vector.methods.include? '[]'  #  => true
  • is it possible to use reflection from Java on JRuby class? 是否有可能在JRuby类上使用Java的反射?

When embedding JRuby using BSF or JSR223? 使用BSF或JSR223嵌入JRuby时? Only to the extent that those technologies allow it. 仅在这些技术允许的范围内。 When using ruby2java? 使用ruby2java时? Yes. 是。 It generates normal Java .class files. 它生成普通的Java .class文件。

  • do I have executable classes in JRuby? 在JRuby中有可执行类吗?

I'm not exactly sure what you're asking. 我不确定你在问什么。

  • is it possible to redefine Java class inside of JRuby script? 是否有可能在JRuby脚本中重新定义Java类? (the same way as I can redefine eg. Integer in C Ruby) (就像我可以重新定义一样。例如.Cub Ruby中的Integer)

Yes, you can monkey patch in JRuby, but the changes aren't visible from the Java side, just JRuby: 是的,您可以在JRuby中修补补丁,但是从Java端看不到更改,只有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? 是否有任何其他限制,阻止使用JRuby作为任何Java应用程序的一部分?

Plenty of little gotchas abound when using Java from JRuby. 从JRuby使用Java时,有很多小问题 ruby2java is still in its infancy, and I'm not sure it's ready for a production environment yet. ruby2java还处于起步阶段,我不确定它是否已经为生产环境做好了准备。 Other than that, the focus has been more on scripting with BSF and JSR223, which may or may not suit your purposes. 除此之外,重点更多的是使用BSF和JSR223编写脚本,这可能适合您的目的,也可能不适合您的目的。

JRuby 1.4 does contain some support for annotations on Ruby classes, but only at runtime. JRuby 1.4确实包含对Ruby类注释的一些支持,但仅限于运行时。 Look at http://github.com/nicksieger/ruby-jersey for an example of using the new runtime class generation to interop with annotation-based frameworks. 请查看http://github.com/nicksieger/ruby-jersey ,了解使用新的运行时类生成与基于注释的框架互操作的示例。

The rest of Pesto's answers are pretty much right. 佩斯托其余的答案非常正确。

JRuby does now support Annotations via java_annotation but it has some limitations. JRuby现在通过java_annotation支持Annotations,但它有一些限制。 One serious one in my experience so far is that it requires use of become_java! 到目前为止,我的经验中最严重的一点是它需要使用become_java! (which is undocumented). (没有证件)。 This prevents you from using Annotations on Ruby classes that were subclassed from Java superclasses because of issue 2359 . 这可以防止您因为问题2359而对从Java超类子类化的Ruby类使用Annotations。

This is pretty problematic in web programming, where many frameworks use Annotations for their routing system. 这在Web编程中是非常有问题的,其中许多框架使用Annotations作为其路由系统。 It effectively means you can't create parent classes for controllers. 它实际上意味着您无法为控制器创建父类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM