简体   繁体   中英

JRuby: extend Java class with two methods that have the same name

Good morning every body, I am trying to extend an abstract Java class with two methods that have the same name. But only need to verride one of them.

Here is my Java code:

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
    }
  }

Here is my JRuby code

#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

With my first version when i try to use the "java_method", i am getting the following error: "no method 'forName' for arguments (org.jruby.RubyModule) on Java::JavaLang::Class"

Then i have tried this second version of the RClientHandler class

Second version : referring to that post: 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

With this second version i am getting the following Exception that is pointing the first line of the "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)

Is this line with "super" calling the parent class constructor or the parent "handleMessage" method ? Do i need some argument when calling "super" here according to the parent class constructor ?

Any help will be appreciate to tell me how can i extend this "ClientHandler" class by overriding only one (or the two) "handleMessage" method in JRuby. Thanks in advance for your help.

In the first version, trying passing the name of Java class as a string:

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

Update: Actually, that won't work, since that's the JRuby namespace, right? You need the Java path to the class.

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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