jruby 注释方法没有被调用 [英] jruby annotation method not getting called

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

问题描述

我有一个名为 test.jar 的 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 {
} 

我的 ruby​​ 文件是 Main.rb

and my ruby file is 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调用Test.java的yoyo()并在yoyo()中传递Main的对象.然后Test.java中的yoyo()会分析Main的所有函数.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天全站免登陆