I'm trying to comment all calls to a custom method inside a java file using Javaparser. My first approach is to use a ModifierVisitor:
ModifierVisitor<Object> visitante = new ModifierVisitor<Object>() {
@Override
public Visitable visit(MethodCallExpr n, Object arg) {
if (!"MYMETHOD".equalsIgnoreCase(n.getNameAsString())) {
return super.visit(n, arg);
}
Node comment = new BlockComment(n.toString());
return comment;
}
};
visitante.visit(this.ficheroCompilado, null);
... the code finds the method calls to "MYMETHOD" correctly, but when I try to replace it with a BlockComment, an exception is thrown:
java.lang.ClassCastException: com.github.javaparser.ast.comments.LineComment cannot be cast to com.github.javaparser.ast.expr.Expression
at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:477)
at com.github.javaparser.ast.visitor.ModifierVisitor.visit(ModifierVisitor.java:51)
at com.github.javaparser.ast.stmt.ExpressionStmt.accept(ExpressionStmt.java:71)
...
It seems I can only replace an Expression by another Expression (because the change is inside an ExpressionStmt).
How could I about it and convert the method call to a comment?
Thank you all
Updated: Finally, I've made a hack: changing the name of the method to "//" + method name, so it ends up commented.
Finally, I've made a hack: changing the name of the method to "//" + method name, so it ends up commented.