简体   繁体   English

是否可以将匿名类从Java转换为JRuby?

[英]Is it possible to convert an anonymous class from Java to JRuby?

I am working on implementing a JRuby wrapper around the SNMP4J-Agent library, found here: 我正在围绕SNMP4J-Agent库实现JRuby包装器,可在此处找到:

Website: http://www.snmp4j.org/ 网址: http//www.snmp4j.org/
JavaDocs: http://www.snmp4j.org/agent/doc/ JavaDocs: http ://www.snmp4j.org/agent/doc/


It's been pretty fun so far, but I am having trouble deciphering the following piece of Java code to implement in JRuby: 到目前为止,这非常有趣,但是我很难理解下面的Java代码以在JRuby中实现:

server = new DefaultMOServer();
vacmMIB = new VacmMIB(new MOServer[] { server });

The problem is that - as far as I can tell - casting new with MOServer[] (which is an interface) creates an anonymous function that is passed a server object, and I can't seem to find the right way to express that with JRuby. 问题是-据我可以告诉-铸造newMOServer[]这是一个接口)创建传递服务对象的匿名函数,我似乎无法找到表达与正道JRuby的。 I've included information about the Java classes: 我提供了有关Java类的信息:


DefaultMOServer is defined as DefaultMOServer定义为

public class DefaultMOServer implements MOServer {
    public DefaultMOServer() {
       ...
    }
    ...
}

JavaDoc: http://www.snmp4j.org/agent/doc/org/snmp4j/agent/DefaultMOServer.html JavaDoc: http : //www.snmp4j.org/agent/doc/org/snmp4j/agent/DefaultMOServer.html


VacmMIB is defined as VacmMIB定义为

public class VacmMIB implements MOGroup, MutableVACM {
    public VacmMIB(MOServer[] server) {
       this.server = server;
       ...
    }
    ...
}

JavaDoc: http://www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/VacmMIB.html JavaDoc: http : //www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/VacmMIB.html


And finally, MOServer is defined as 最后, MOServer被定义为

public interface MOServer {
   ...
}

JavaDoc: http://www.snmp4j.org/agent/doc/org/snmp4j/agent/MOServer.html JavaDoc: http : //www.snmp4j.org/agent/doc/org/snmp4j/agent/MOServer.html



Here is roughly what I am doing in JRuby: 这大致就是我在JRuby中所做的事情:

require 'java'
require 'snmp4jruby'
require 'lib/snmp4j-agent-1.4.3.jar'

module Agent
    include_package 'org.snmp4j.agent'

    module MO
       include_package 'org.snmp4j.agent.mo'

       module SNMP
          include_package 'org.snmp4j.agent.mo.snmp'
       end
    end
end

class SnmpAgent < Agent::BaseAgent

    # Setup the agent
    def init
       ... everything works fine up here ...

       # Server is created early on without issue
       self.server = Agent::DefaultMOServer.new

       # Having trouble here
       _server = Agent::MOServer.new { self.server }
       self.vacmMIB = Agent::MO::SNMP::VacmMIB.new(_server)
    end

end

Running the above code gives me the following error for the line where I set self.vacmMIB = ... : 运行上面的代码给我设置self.vacmMIB = ...的行出现以下错误:

TypeError: failed to coerce org.jruby.gen.InterfaceImpl792882806 to [Lorg.snmp4j.agent.MOServer;



Any direction on this would be greatly appreciated! 任何方向对此将不胜感激!

Your issue seems to be that VacmMIB constructor takes array of MOServer as an argument and you're passing an instance of MOServer . 您的问题似乎是, VacmMIB构造函数采用的阵列MOServer作为参数,要传递的一个实例MOServer

Try this: 尝试这个:

_server = Agent::MOServer.new { self.server }
# your code
# self.vacmMIB = Agent::MO::SNMP::VacmMIB.new(_server) 
# updated code
self.vacmMIB = Agent::MO::SNMP::VacmMIB.new([_server]) 

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

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