Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

LexicalPreservingPrinter will remove comments when using ASTTransforms.wrapIntoResource #338

Open
Labels
bugSomething isn't working
@andrecsilva

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /