Forum: JRuby Overriding Java methods defined with the same name

0f91e71b5b55019a0197620bc7e9d066?d=identicon&s=25 Manish Sa (manish_s)
on 2010-09-09 20:34
Hi,

I'm trying to replicate the example from
http://code.google.com/p/javaparser/wiki/UsingThis...
in Jruby.

In the original example, the abstract class VoidVisitorAdapter defines
various 'visit' methods that accept different inputs
(AnnotationDeclaration, CompilationUnit, MethodDeclaration, etc.)

Check the source of VoidVisitorAdapter here for reference:
http://code.google.com/p/javaparser/source/browse/...


My code in JRuby is:
----------------------------
require 'java'
require 'javaparser-1.0.8.jar'

java_import Java::japa.parser.JavaParser
java_import Java::japa.parser.ast.CompilationUnit
java_import Java::japa.parser.ast.body.MethodDeclaration
java_import Java::japa.parser.ast.visitor.VoidVisitorAdapter
java_import java.io.FileInputStream


class MethodVisitor < VoidVisitorAdapter
    # I'm trying to Override the method with the following signature:
    # public void visit(MethodDeclaration n, Object arg)
    # How can it be done in JRuby ??
    def visit(n, arg)
        puts(n.get_name)
    end
end

fis = FileInputStream.new(ARGV[0])

begin
  cu = JavaParser.parse(fis)
rescue Exception => err
  puts err
ensure
  fis.close
end

mv = MethodVisitor.new
mv.java_send :visit, [CompilationUnit, java.lang.Object], cu, nil

----------------------------

The trouble I'm facing is overriding the 'visit' method that accepts
MethodDeclaration. How can this be achieved in JRuby when there are
multiple definitions of methods with same name?

-- Manish
13313ac2ec7ba7c43b1b952db034ff3b?d=identicon&s=25 Thomas E Enebo (Guest)
on 2010-09-09 21:49
(Received via mailing list)
On Thu, Sep 9, 2010 at 1:34 PM, Manish Ss <lists@ruby-forum.com> wrote:
> Check the source of VoidVisitorAdapter here for reference:
> java_import Java::japa.parser.ast.body.MethodDeclaration
>    end
> end
>
> mv = MethodVisitor.new
> mv.java_send :visit, [CompilationUnit, java.lang.Object], cu, nil
>
> ----------------------------
>
> The trouble I'm facing is overriding the 'visit' method that accepts
> MethodDeclaration. How can this be achieved in JRuby when there are
> multiple definitions of methods with same name?

Your 'def visit' above will override all visit overloads because Ruby
has no way of using types to specify a particular overload.  So your
visit method will need to look at the arguments provided and then use
java_send to delegate it to the proper base class version when you
don't actually want to handle that particular version of the visit
method.  So some plumbing...this does seem like an interesting
enhancement to JI though.   The ability to override some overloads of
the same method name.  Anyone have any ideas on what this could look
like?

Java.override [CompilationUnit, :object]
def visit(a, b)
end

????

-Tom

>
--
blog: http://blog.enebo.com       twitter: tom_enebo
mail: tom.enebo@gmail.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
0f91e71b5b55019a0197620bc7e9d066?d=identicon&s=25 Manish Sa (manish_s)
on 2010-09-09 22:41
Thomas E Enebo wrote:
> On Thu, Sep 9, 2010 at 1:34 PM, Manish Ss <lists@ruby-forum.com> wrote:
>> Check the source of VoidVisitorAdapter here for reference:
>> java_import Java::japa.parser.ast.body.MethodDeclaration
>> � �end
>> end
>>
>> mv = MethodVisitor.new
>> mv.java_send :visit, [CompilationUnit, java.lang.Object], cu, nil
>>
>> ----------------------------
>>
>> The trouble I'm facing is overriding the 'visit' method that accepts
>> MethodDeclaration. How can this be achieved in JRuby when there are
>> multiple definitions of methods with same name?
>
> Your 'def visit' above will override all visit overloads because Ruby
> has no way of using types to specify a particular overload.  So your
> visit method will need to look at the arguments provided and then use
> java_send to delegate it to the proper base class version when you
> don't actually want to handle that particular version of the visit
> method.

Ahh I get it (damn!... silly me).
Thanks for the reply Tom. Here is my modified MethodVisitor class:

class MethodVisitor < VoidVisitorAdapter
    # This method will now check for MethodDeclaration
    # and process it
    def visit(n, arg)
      super
      klass = n.class

      if klass.eql?(Java::JapaParserAstBody::MethodDeclaration)
        self.java_send :visit, [MethodDeclaration, java.lang.Object], n,
arg
        puts("Method: " + n.get_name)
      end
    end
end

This works nicely :) ... thanks.
But is this a good way to do it? Is there a better way ??


> So some plumbing...this does seem like an interesting
> enhancement to JI though.   The ability to override some overloads of
> the same method name.  Anyone have any ideas on what this could look
> like?
>
> Java.override [CompilationUnit, :object]
> def visit(a, b)
> end
>
> ????


Something similar to java_alias, java_send, java_anotation,
java_signature, etc. would be nice:

class MethodVisitor

  java_override :visit, [MethodDeclaration, :object]
  def visit(n, arg)
    # some code ....
  end
end


> -Tom
>
>>
> --
> blog: http://blog.enebo.com� � �� twitter: tom_enebo
> mail: tom.enebo@gmail.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.