Hi,
I’m trying to replicate the example from
http://code.google.com/p/javaparser/wiki/UsingThisParser#Visiting_class_methods
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/trunk/JavaParser/src/japa/parser/ast/visitor/VoidVisitorAdapter.java
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