2012-04-24 124 views
2

早上好,每个主体, 我想用两个具有相同名称的方法扩展抽象Java类。但只需要表达其中一个。JRuby:扩展具有相同名称的两种方法的Java类

这里是我的Java代码:

public abstract class ClientHandler extends Handler implements SOAPHandler<SOAPMessageContext> { 
    protected boolean disable = true; 
    protected X509Certificate signatureCertificate; 
    protected PrivateKey privateKey; 
    protected Certificate[] signatureCertificationChain; 

    //Constructeur 
    protected ClientHandler(boolean disable) { 
     this.disable = disable; 
    } 

    private boolean handleMessage(VIHF vihf) { 
     //This is the beginning of the method i am trying to redefine 
     X509Certificate certificate = this.signatureCertificate 
     String issuer = certificate.getSubjectDN().toString(); 
     //some code 
     ... 
    } 

    public boolean handleMessage(SOAPMessageContext smc) { 
     //some code 
    } 
    } 

这里是我的JRuby的代码

#To make parent attributes accessible from subclasses 
class ClientHandler 
    field_accessor :disable, :signatureCertificate, :privateKey, :signatureCertificationChain 
end 

#first version referring to that post: <http://stackoverflow.com/questions/6238734/jruby-calls- the-wrong-method> 

module RubyClientHandler 
    class RClientHandler < ClientHandler 
    handleMessage = ClientHandler.java_method :handleMessage, java.lang.Class.for_name(Java::ComSubFolder::VIHF)] 

    def handleMessage(vihf) 
     super(self.disableSignature) #is this line ok or note? 

     certificate = self.signatureCertificate #do i need to use the "self" or the "@" ? 
     issuer = certificate.getSubjectDN().toString() 
     ... 
    end 
    end 
end 

我的第一个版本时,我尝试使用“java_method”,我收到以下错误: “没有方法'forName'在Java :: JavaLang :: Class上的参数(org.jruby.RubyModule)”

然后,我已经尝试过RClientHandler类的第二个版本

第二个版本:指该职位:http://www.ruby-forum.com/topic/216636

module RubyClientHandler 
    class RClientHandler < ClientHandler 
    def handleMessage(vihf) 
     super   #line causing the error 
     klass = vihf.class 

     if(klass.eql?(Java::ComSubFolder::VIHF)) 
      self.java_send :handleMessage,[VIHF], vihf 

      certificate = self.signatureCertificate 
      issuer = certificate.getSubjectDN().toString() 
      ... 
     end 
    end 
    end 
end 

有了这个第二个版本我得到的是指向了“的handleMessage”的第一线以下异常

HANDLER_RAISED_RUNTIME_EXCEPTION 
org.jruby.exceptions.RaiseException: Native Exception: 'class java.lang.StackOverflowError';  Message: null; StackTrace: java.lang.StackOverflowError 
at org.jruby.proxy.com.sub.folder.ClientHandler$Proxy0.__super$handleMessage(Unknown Source) 

这是用“超级”来调用父类构造函数或父类“handleMessage”方法? 根据父类的构造函数在这里调用“super”时是否需要一些参数?

任何帮助将不胜感激告诉我如何通过重写JRuby中的一个(或两个)“handleMessage”方法来扩展此“ClientHandler”类。 在此先感谢您的帮助。

回答

1

在第一个版本,试图通过Java类的名字作为一个字符串:

java.lang.Class.for_name('Java::ComSubFolder::VIHF') 

更新:其实,这是行不通的,因为这是JRuby的命名空间,对不对?您需要该类的Java路径。

例子:

> java.lang.Class.for_name('java.lang.StringBuffer') 
=> #<Java::JavaLang::Class:0x628d2280> 
> java.lang.Class.for_name('java.util.ArrayList') 
=> #<Java::JavaLang::Class:0x5057f57f> 
+0

我试图用我的类作为字符串的名称,但我收到以下错误 NativeException(抛出java.lang.ClassNotFoundException:COM /子/文件夹/ VIHF) : java/lang/Class.java:-2:in'forName0' java/lang/Class.java:169:in'forName' – user1310635 2012-04-24 14:43:42

+0

您的Java代码如何提供给JRuby?在一个jar文件中?如果是这样,你的班级路上有那个罐子吗? – 2012-04-24 15:02:18

相关问题