代码之家  ›  专栏  ›  技术社区  ›  Jed Schneider

JRuBy:Java类接受IRB中的参数,但当从类调用时返回错误

  •  0
  • Jed Schneider  · 技术社区  · 14 年前

    我使用的类接受 重写的方法 我指的是可选的论点签名 (不确定这是否重要,但可能)

    当我从IRB调用它时,它按预期工作,例如,它接受参数

    (在需要保密的地方使用[筛选]筛选名称空间和密码,让我的公司满意)

    jruby-1.5.0 > require 'java'
     => true 
    
    jruby-1.5.0 > Dir.glob('lib/java/*.jar').each{|jar| require jar}
     => ["lib/java/[filtered].jar", "lib/java/[filtered].jar", "lib/java/[filtered].jar"] 
    
    jruby-1.5.0 > import "[filtered].His351n1"
     => Java::[filtered]::His351n1 
    
    jruby-1.5.0 > broker = [filtered].Broker.new('[filtered]', '[filtered]')
     => #<Java::[filtered]::Broker:0x4c4936f3> 
    
    jruby-1.5.0 > rpc = "[filtered]"
     => "[filtered]" 
    
    jruby-1.5.0 > his = His351n1.new(broker, rpc)
     => #<Java::[filtered]::His351n1:0x7fb6a1c4> 
    

    这是我的规格和匹配代码

    before(:each) do 
      @base = Legacy::Base.new
    end
    
    it "should create a valid his351n1 object" do
      his = @base.create_his351n1
      puts his.inpsect
    end  
    
    # from within Legacy::Base
    def create_his351n1
      his = His351n1.new(build_broker, rpc)
    end
    

    最后,调用his351n1.new失败的错误

    1)
    ArgumentError in 'Legacy::Base should create a valid his351n1 object'
    wrong # of arguments(2 for 0)
    

    使事情复杂化的是,在IRB上,这显然也是有效的:

    jruby-1.5.0 > his = His351n1.new
     => #<Java::[filtered]::His351n1:0x5ad3c69c> 
    

    另外,这里是被重写的Java方法

    public His351n1() {
        super();
    }
    
    public His351n1(Broker broker) {
        this(broker, DEFAULT_SERVER);
    }
    
    public His351n1(BrokerService bs) {
        this(bs.getBroker(), bs.toString());
    }
    
    public His351n1(Broker broker, String serverAddr) {
        super(broker, serverAddr, "string", true);
    }
    
    public His351n1(final Broker broker, final String serverAddr, final String library)
    {
        super(broker, serverAddr, library, true);
    }
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Jed Schneider    14 年前

    似乎您需要在对象的实例化中使用整个名称空间,也就是:

    his = Java::[filtered_namesapce]::His351n1.new(build_broker, rpc)