Skip to content

Commit

Permalink
Merge pull request #29 from LoiNguyenCS/delete-comments
Browse files Browse the repository at this point in the history
removing comments from input codes
  • Loading branch information
kelloggm authored Oct 6, 2023
2 parents a0f6175 + fe77e9d commit 04cc94e
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 13 deletions.
14 changes: 5 additions & 9 deletions src/main/java/org/checkerframework/specimin/SpeciminRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, CompilationUnit> target : parsedTargetFiles.entrySet()) {
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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()"});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.example;

public class Maternal {

public String maternalLastName = null;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.example;

public class Paternal {

public String paternalLastName = null;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package an.old.library;

public class Book {

public Book(int parameter0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import an.old.library.Book;

class Simple {
// Target method.

int test() {
Book bookOfTheYear = new Book(2023);
return bookOfTheYear.getRates().length();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example;

class Simple {
// Target method.

void bar() {
Object obj = new Object();
obj = baz(obj);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
19 changes: 19 additions & 0 deletions src/test/resources/removingcomments/input/com/example/Simple.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 04cc94e

Please sign in to comment.