I think the two methods may be interfering with each other ? The first one works without the second.
The second method changes the name of the Java class.
Thanks,
Mohan
@Override
public void enterClassBody(JavaParser.ClassBodyContext ctx) {
String field = "\n\tpublic static final long serialVersionUID = 1L;";
rewriter.insertAfter(ctx.start, field);
}
@Override
public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
System.out.println( ctx.getChild(1) + "[" + ctx.start.getText() + "][" + ctx.stop.getText() + "]");
if( ctx.getChild(1).getText().contains( "Action )) {
//rewriter.replace( ctx.start,
//ctx.stop,
//"Controller");
}
}
This is what is printed.
Action[class][}]
The exception if I uncomment the code is this.
Exception in thread "main" java.lang.IllegalArgumentException: insert op <InsertBeforeOp@[@270,1725:1731='private',<33>,48:1]:"
public static final long serialVersionUID = 1L;"> within boundaries of previous <ReplaceOp@[@265,1686:1690='class',<9>,47:7]..[@1800,11379:11379='}',<60>,343:0]:"Controller">
at org.antlr.v4.runtime.TokenStreamRewriter.reduceToSingleOperationPerIndex(TokenStreamRewriter.java:566)
at org.antlr.v4.runtime.TokenStreamRewriter.getText(TokenStreamRewriter.java:409)
at org.antlr.v4.runtime.TokenStreamRewriter.getText(TokenStreamRewriter.java:371)
at com.transformer.ActionVisitor.printTree(ActionVisitor.java:43)
at com.transformer.ActionVisitor.main(ActionVisitor.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)