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
on 2010-09-09 20:34
on 2010-09-09 21:49
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
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
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.