Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LexicalPreservingPrinter will remove comments when using ASTTransforms.wrapIntoResource #338

Open
andrecsilva opened this issue Apr 9, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@andrecsilva
Copy link
Contributor

andrecsilva commented Apr 9, 2024

Consider the following snippet:

    String code =
        """
    import java.nio.file.Files;
    import java.io.File;
    import java.io.BufferedWriter;
    class A{
	    void f(File f){
		    // first comment
		    BufferedWriter writer = Files.newBufferedWriter(f.toPath());
		    // second comment
		    System.out.println(1);
		    // third comment
		    System.out.println(2);
	    }
    }
    """;
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    parserConfiguration.setLanguageLevel(ParserConfiguration.LanguageLevel.BLEEDING_EDGE);
    parserConfiguration.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
    var parser = new JavaParser(parserConfiguration);

    CompilationUnit cu = parser.parse(code).getResult().get();
    LexicalPreservingPrinter.setup(cu);

    var node = cu.findAll(VariableDeclarator.class).get(0);
    var lvd = LocalVariableDeclaration.fromVariableDeclarator(node).get();
    ASTTransforms.wrapIntoResource(lvd.getStatement().asExpressionStmt(), lvd.getVariableDeclarationExpr(), lvd.getScope());

    System.out.println(cu);
    System.out.println("------------------");
    System.out.println(LexicalPreservingPrinter.print(cu));

This will output:

    import java.nio.file.Files;
    import java.io.File;
    import java.io.BufferedWriter;

    class A {

        void f(File f) {
            // first comment
            try (BufferedWriter writer = Files.newBufferedWriter(f.toPath())) {
                // second comment
                System.out.println(1);
                // third comment
                System.out.println(2);
            }
        }
    }

    ------------------
    import java.nio.file.Files;
    import java.io.File;
    import java.io.BufferedWriter;
    class A{
     void f(File f){
  
      try (BufferedWriter writer = Files.newBufferedWriter(f.toPath())) {
          System.out.println(1);
          System.out.println(2);
      }


     }
    }

Notice how the comments disappears if we print it with the LexicalPreservingPrinter, despite being there if we chose not to use it.

This affects all codemods that uses this transform, mainly those using the ResourceLeakFixer transform:
codeql:java/input-resource-leak, codeql:java/output-resource-leak, codeql:java/database-resource-leak, pixee:java/prevent-filewriter-leak-with-nio, pixee:java/resource-leak.

@nahsra
Copy link
Contributor

nahsra commented Apr 9, 2024

👀

@andrecsilva andrecsilva added the bug Something isn't working label Apr 9, 2024
andrecsilva added a commit that referenced this issue Apr 12, 2024
Added a new resource leak transform and general codemod

- Removed CodeQL dependency for ResourceLeakFixer and left it available
as a general transform.
- Added new resource leak codemod independent of CodeQL. This may
conflict with the resource leak codemods if both are available,
producing more changes than necessary.
- Added ResourceLeakFixer transform to
PreventFileWriterLeakWithFilesCodemod. Closes #304.

You may notice the changes to the PreventFileWriterLeakWithFilesCodemod
test files. This is due to a newly discovered issue. Refer to #338.
@andrecsilva
Copy link
Contributor Author

Can't find a workaround for this, at least one that doesn't involve ditching LexicalPreservingPrinter. I've created an Issue for this in JavaParser's github after narrowing it down a bit more.
javaparser/javaparser#4376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants