每个人的早上好, 我试图用两个具有相同名称的方法扩展一个抽象的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”时,我的第一个版本,我收到以下错误: “对于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”类。 在此先感谢您的帮助。
答案 0 :(得分: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>