diff --git a/src/main/java/org/checkerframework/specimin/SpeciminRunner.java b/src/main/java/org/checkerframework/specimin/SpeciminRunner.java index 3b14f32b..9062c68d 100644 --- a/src/main/java/org/checkerframework/specimin/SpeciminRunner.java +++ b/src/main/java/org/checkerframework/specimin/SpeciminRunner.java @@ -5,7 +5,6 @@ import com.github.javaparser.ast.Node; import com.github.javaparser.ast.PackageDeclaration; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; -import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import com.github.javaparser.symbolsolver.JavaSymbolSolver; import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; @@ -14,7 +13,7 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; @@ -175,9 +174,6 @@ public static void performMinimization( new MethodPrunerVisitor(finder.getTargetMethods(), finder.getUsedMembers()); for (CompilationUnit cu : parsedTargetFiles.values()) { - // This must happen before any modifications to each compilation unit, or - // the printer won't know about them. (It registers an observer.) - LexicalPreservingPrinter.setup(cu); cu.accept(methodPruner, null); } for (Entry target : parsedTargetFiles.entrySet()) { @@ -191,7 +187,6 @@ public static void performMinimization( continue; } } - Path targetOutputPath = Path.of(outputDirectory, target.getKey()); // Create any parts of the directory structure that don't already exist. Path dirContainingOutputFile = targetOutputPath.getParent(); @@ -201,10 +196,11 @@ public static void performMinimization( if (dirContainingOutputFile != null) { Files.createDirectories(dirContainingOutputFile); } + // Write the string representation of CompilationUnit to the file try { - Files.write( - targetOutputPath, - LexicalPreservingPrinter.print(target.getValue()).getBytes(StandardCharsets.UTF_8)); + PrintWriter writer = new PrintWriter(targetOutputPath.toFile()); + writer.print(target.getValue()); + writer.close(); } catch (IOException e) { System.out.println("failed to write output file " + targetOutputPath); System.out.println("with error: " + e); diff --git a/src/test/java/org/checkerframework/specimin/RemovingCommentsTest.java b/src/test/java/org/checkerframework/specimin/RemovingCommentsTest.java new file mode 100644 index 00000000..af9287d9 --- /dev/null +++ b/src/test/java/org/checkerframework/specimin/RemovingCommentsTest.java @@ -0,0 +1,15 @@ +package org.checkerframework.specimin; + +import java.io.IOException; +import org.junit.Test; + +/** This test checks that if Specimin will remove comments from the input code */ +public class RemovingCommentsTest { + @Test + public void runTest() throws IOException { + SpeciminTestExecutor.runTestWithoutJarPaths( + "removingcomments", + new String[] {"com/example/Simple.java"}, + new String[] {"com.example.Simple#test()"}); + } +} diff --git a/src/test/java/org/checkerframework/specimin/SpeciminTestExecutor.java b/src/test/java/org/checkerframework/specimin/SpeciminTestExecutor.java index 7df76b8a..e7a0c48d 100644 --- a/src/test/java/org/checkerframework/specimin/SpeciminTestExecutor.java +++ b/src/test/java/org/checkerframework/specimin/SpeciminTestExecutor.java @@ -90,6 +90,8 @@ public static void runTest( builder.command( "diff", "-qr", + "-w", + "-B", outputDir.toAbsolutePath().toString(), Path.of("src/test/resources/" + testName + "/expected").toAbsolutePath().toString()); } diff --git a/src/test/resources/innerclass/expected/com/example/Maternal.java b/src/test/resources/innerclass/expected/com/example/Maternal.java index 21d76a34..62d581f4 100644 --- a/src/test/resources/innerclass/expected/com/example/Maternal.java +++ b/src/test/resources/innerclass/expected/com/example/Maternal.java @@ -1,4 +1,6 @@ package com.example; + public class Maternal { + public String maternalLastName = null; } diff --git a/src/test/resources/innerclass/expected/com/example/OuterFamily.java b/src/test/resources/innerclass/expected/com/example/OuterFamily.java index 4a3e235d..5fa89315 100644 --- a/src/test/resources/innerclass/expected/com/example/OuterFamily.java +++ b/src/test/resources/innerclass/expected/com/example/OuterFamily.java @@ -1,13 +1,15 @@ package com.example; class OuterFamily extends Maternal { + class InnerFamily extends Paternal { + public String getLastName() { return paternalLastName; } } - // point A + public String getLastName() { return maternalLastName; } -} \ No newline at end of file +} diff --git a/src/test/resources/innerclass/expected/com/example/Paternal.java b/src/test/resources/innerclass/expected/com/example/Paternal.java index 985bb5c1..b1ac0153 100644 --- a/src/test/resources/innerclass/expected/com/example/Paternal.java +++ b/src/test/resources/innerclass/expected/com/example/Paternal.java @@ -1,4 +1,6 @@ package com.example; + public class Paternal { + public String paternalLastName = null; } diff --git a/src/test/resources/jarfile/expected/an/old/library/Book.java b/src/test/resources/jarfile/expected/an/old/library/Book.java index f8254949..8a4c1adb 100644 --- a/src/test/resources/jarfile/expected/an/old/library/Book.java +++ b/src/test/resources/jarfile/expected/an/old/library/Book.java @@ -1,4 +1,5 @@ package an.old.library; + public class Book { public Book(int parameter0) { diff --git a/src/test/resources/jarfile/expected/com/example/Simple.java b/src/test/resources/jarfile/expected/com/example/Simple.java index aa145f6f..ce9c6987 100644 --- a/src/test/resources/jarfile/expected/com/example/Simple.java +++ b/src/test/resources/jarfile/expected/com/example/Simple.java @@ -3,7 +3,7 @@ import an.old.library.Book; class Simple { - // Target method. + int test() { Book bookOfTheYear = new Book(2023); return bookOfTheYear.getRates().length(); diff --git a/src/test/resources/onefilesimple/expected/com/example/Simple.java b/src/test/resources/onefilesimple/expected/com/example/Simple.java index c60b702e..3a35e628 100644 --- a/src/test/resources/onefilesimple/expected/com/example/Simple.java +++ b/src/test/resources/onefilesimple/expected/com/example/Simple.java @@ -1,7 +1,7 @@ package com.example; class Simple { - // Target method. + void bar() { Object obj = new Object(); obj = baz(obj); diff --git a/src/test/resources/removingcomments/expected/com/example/Simple.java b/src/test/resources/removingcomments/expected/com/example/Simple.java new file mode 100644 index 00000000..6597d67f --- /dev/null +++ b/src/test/resources/removingcomments/expected/com/example/Simple.java @@ -0,0 +1,13 @@ +package com.example; + +class Simple { + + static int returnTen() { + throw new Error(); + } + + static void test() { + int y = returnTen(); + Simple s = new Simple(); + } +} diff --git a/src/test/resources/removingcomments/input/com/example/Simple.java b/src/test/resources/removingcomments/input/com/example/Simple.java new file mode 100644 index 00000000..64708522 --- /dev/null +++ b/src/test/resources/removingcomments/input/com/example/Simple.java @@ -0,0 +1,19 @@ +package com.example; + +class Simple { + static int returnFive() { + return 5; + } + static int returnTen() { + return returnFive() * 2; + } + + /** + * This method is used to test if the comments (including Javadoc) are all removed by Specimin + */ + static void test() { + // the comment here should be removed + int y = returnTen(); + Simple s = new Simple(); + } +}