大家早上好,我正在尝试使用两个具有相同名称的方法来扩展一个抽象 Java 类。但只需要verride其中一个。
这是我的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”时,我的第一个版本出现以下错误:“Java::JavaLang::Class 上的参数(org.jruby.RubyModule)没有方法'forName'”
然后我尝试了 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)
这一行是“super”调用父类构造函数还是父“handleMessage”方法?根据父类构造函数在这里调用“super”时是否需要一些参数?
任何帮助将不胜感激,告诉我如何通过在 JRuby 中仅覆盖一个(或两个)“handleMessage”方法来扩展这个“ClientHandler”类。在此先感谢您的帮助。