I need to get parameters of method declaration of java file. I am using JavaBaseListener interface and those method :
@Override
public Object visitMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
TokenStream tokens = parser.getTokenStream();
String type = "void";
if(ctx.type() != null) {
type = tokens.getText(ctx.type().getSourceInterval());
}
String args = tokens.getText(ctx.formalParameters());
System.out.println("\t" + type + " " + ctx.Identifier() + args + ";");
return super.visitMethodDeclaration(ctx);
}
The problem is, that there are no white spaces between method name and method classname. Input : private void addLoan(Loan loan)
Output : void addLoan(Loanloan);
I tried to change java.g4 grammar file, and added whitespace there
formalParameter : variableModifier* type " " variableDeclaratorId ;
But now i have a lot of errors such as :
line 1:6 no viable alternative at input 'public ' line 1:12 extraneous input ' ' expecting Identifier line 1:20 extraneous input ' ' expecting {'extends', 'implements', '{', '<'} line 2:5 no viable alternative at input 'List ' ...
What is the best solution for my problem and how can i handle it? Thanks in forward