Change the name of Java class

19 views
Skip to first unread message

Mohan Radhakrishnan

unread,
Jun 20, 2016, 5:10:59 AM6/20/16
to antlr-di...@googlegroups.com
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)

Mohan Radhakrishnan

unread,
Jun 20, 2016, 6:54:06 AM6/20/16
to antlr-discussion
Replying to my mail.

The solution is a visitor because the 'getChild(1)' returns a ParseTree. Not it works.

pt.accept(new ParseTreeVisitor<Object>() {

@Override
public Object visitChildren(RuleNode ruleNode) {
System.out.println(ruleNode.getText());
return null;
}

@Override
public Object visitErrorNode(ErrorNode errorNode) {
return null;
}

@Override
public Object visit(ParseTree parseTree) {
return null;
}

@Override
public Object visitTerminal(TerminalNode terminalNode) {
System.out.println(terminalNode.getText());
rewriter.replace( terminalNode.getSymbol(),"Controller");
return null;
}
});

Thanks,
Mohan
Reply all
Reply to author
Forward
0 new messages