JRuby的标注方法不获取调用 [英] jruby annotation method not getting called

查看:173
本文介绍了JRuby的标注方法不获取调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为JAR的 Test.jar的其中包含以下Java类:

I have a jar named test.jar which contains following java classes:

Test.java

package test;
import java.lang.reflect.Method;
public class Test {

public static void yoyo(Object o) {

    Method[] methods = o.getClass().getMethods();

    for (Method method : methods) {
        CanRun annos = method.getAnnotation(CanRun.class);
        if (annos != null) {
            try {
                method.invoke(o);            
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
}

和接口的 CanRun.java

package test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface CanRun {
} 

和我的红宝石文件的 main.rb的

require 'java'
require 'test.jar'
java_import Java::test.Test

class Main
  def run
    Test.yoyo(Main.new)
  end

  java_annotation('CanRun')
  def onHi()
    puts "inside on Hi"
  end
end

app = Main.new
app.run

和我通过以下命令运行它:
JRuby的main.rb的

and I am running it by the following command: jruby Main.rb

,但没有输出

基本上,我试图做的是,从main.rb的调用YOYO()Test.java,并在YOYO(传递主要的对象)。然后YOYO()在Test.java将分析主要的所有功能.RB有注释CanRun如果发现会打电话给他们,在我们的例子onHi应该叫。
我检查了一下,我在我的YOYO通过()的对象不是空。我现在面临的问题是,onHi是没有得到调用。

Basically,what I am trying to do is, calling yoyo() of Test.java from Main.rb and passing object of Main in yoyo().then yoyo() in Test.java will analyze all the functions of Main.rb having annotation CanRun and if found will call them, in our case onHi should be called. I checked that the object which I am passing in yoyo() is not null. The problem I am facing is that onHi is not getting called.

我试过了很多东西,但没有任何帮助。

I tried a lot of things but nothing helped.

我使用的标注正确的RB文件或请建议一些其他的方式。

Am I using annotation properly in rb file or please suggest some other way.

推荐答案

在注释是不是在默认包(也不是进口的)​​,从而代替:

you annotation is not in the default package (nor is imported) thus instead :

java_annotation'test.CanRun

java_import "test.CanRun"
java_annotation :CanRun

这篇关于JRuby的标注方法不获取调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆