0

I am trying to comment a particulate statement.

  1. My first approach is to return a comment in case statement is an 'Expression Statement' and expression is a particular 'Method Call Expression'.
new ModifierVisitor<Object>() {
   public Visitable visit(ExpressionStmt expStmt, Object arg) {
      Expression exp = expStmt.getExpression();
         if (exp.isMethodCallExpr()) {

         // My other logic goes here
         
         return new LineComment(expStmt.toString());
         }
    }
}


But it failed while dumping the unit back to string.


java.lang.ClassCastException: com.github.javaparser.ast.comments.LineComment cannot be cast to com.github.javaparser.ast.stmt.Statement
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:1329)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
    at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:76)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:1220)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
    at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:104)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.printMembers(DefaultPrettyPrinterVisitor.java:190)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:419)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
    at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:98)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:325)
    at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
    at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:133)
    at com.github.javaparser.printer.DefaultPrettyPrinter.print(DefaultPrettyPrinter.java:104)
    at com.github.javaparser.ast.Node.toString(Node.java:320)


  1. As it suggests that you can replace a 'Statement' with another statement so instead I tried another approach to replace the statement and with an 'Empty Statement'. It kind of worked for me but the output does not look good as it leaves extra ';' after the commented line.

  2. At third I tried to go deeper and instead of replacing the statement I tried to replace the expression with an comment. That too failed as mentioned in SO - Javaparser comment expression.

Any idea how to fix this ?

Vinod Pahuja
  • 47
  • 12

1 Answers1

0

I tried a workaround which does not feel like a good solution, but gives me the expected result for now:

BlockStmt blockStmt = (BlockStmt) expStmt.getParentNode().get();
blockStmt.getStatement(blockStmt.getStatements().indexOf(expStmt) + 1).setLineComment(expStmt.toString());
return null;
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Vinod Pahuja
  • 47
  • 12
  • 1
    This is the right solution when you are using the ModifierVisitor. Returning null value remove the inspected node. Since you created a comment just after the node that is deleted, the result is the expected one. "This visitor can be used to save time when some specific nodes needsto be changed. To do that just extend this class and override the methods from the nodes who needs to be changed, returning the changed node. Returning null will remove the node." – jpl Oct 21 '22 at 07:15