Thank you @dtonhofer and @parrt I was able to make it work using the grammars from
https://github.com/antlr/grammars-v4/tree/master/java/java
You need to generate the Java files in order: Lexer first and Parser last
antlr4 JavaLexer.g4
antlr4 JavaParser.g4
And finally this is the code that I have used for the ExtractInterfaceListener.java file:
import org.antlr.v4.runtime.TokenStream;
public class ExtractInterfaceListener extends JavaParserBaseListener {
JavaParser parser;
public ExtractInterfaceListener(JavaParser parser) {this.parser = parser;}
@Override
public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
// Errata Suggestion: previous implementation
// System.out.println("interface I" + ctx.Identifier() + " {");
// Errata Suggestion: fix
System.out.println("interface I" + ctx.identifier().getText() + " {");
}
@Override
public void exitClassDeclaration(JavaParser.ClassDeclarationContext classDeclarationContext) {
System.out.println("}");
}
@Override
public void enterMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
// need parser to get tokens
TokenStream tokens = parser.getTokenStream();
/*
// Errata Suggestion: previous implementation
String type = "void";
if ( ctx.type()!=null ) {
type = tokens.getText(ctx.type());
}
*/
// Errata Suggestion: fix
String type = ctx.typeTypeOrVoid().getText();
String args = tokens.getText(ctx.formalParameters());
// Errata Suggestion: previous implementation
// System.out.println("\t" + type + " " + ctx.Identifier() + args + ";");
// Errata Suggestion: fix
System.out.println("\t" + type + " " + ctx.identifier().getText() + args + ";");
}
}
I hope it will help someone.