Skip to content

Commit

Permalink
fix regression due to changing Gumtree convention (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Apr 1, 2024
1 parent 2805ed7 commit 4d1317f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class P4JFeatureAnalyzer implements Analyzer<IRevision> {

protected static Logger log = Logger.getLogger(Thread.currentThread().getName());

protected CodeFeatureDetector cresolver = new CodeFeatureDetector();

@Override
public AnalysisResult analyze(IRevision revision, RevisionResult previousResults) {

Expand Down Expand Up @@ -76,7 +74,7 @@ public Map<String, File> fileSrcTgtPaths(IRevisionPair s) {
Map<String, File> filePaths = new HashMap<>();
final File src = new File(s.getPreviousName());
if (!src.exists()) {
throw new IllegalArgumentException("The source file not exist!");
throw new IllegalArgumentException("The source file not exist! "+src);
}
filePaths.put("src", src);
final File tgt = new File(s.getNextName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public List<IRevisionPair> getChildren() {
String previousString = new String(Files.readAllBytes(left.toPath()));
String postString = new String(Files.readAllBytes(right.toPath()));

String previousNameFile = left.getName();
String postNameFile = right.getName();
String previousNameFile = left.getAbsolutePath();
String postNameFile = right.getAbsolutePath();
FilePair fpair = new FilePair(previousString, postString, previousNameFile, postNameFile);
pairs.add(fpair);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/inria/prophet4j/utility/CodeDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private List<DiffEntry> genDiffEntries(Diff diff) throws IndexOutOfBoundsExcepti
Operation DEL = null;
for (Integer lineNum : lineNums) {
Operation operation = deleteOperations.get(lineNum);
if(operation!=null && "DEL".equals(operation.getAction().getName())) {
if(operation!=null && "delete-node".equals(operation.getAction().getName())) {
DEL = operation;
}
Operation deleteOperation = deleteOperations.get(lineNum);
Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/issue261/261_s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Scanner;

public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

double sum = num1 + num2;

System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);

scanner.close();
}
}
18 changes: 18 additions & 0 deletions src/main/resources/issue261/261_t.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Scanner;

public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

double sum = num1 + num2;

System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);

scanner.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.github.difflib.text.DiffRow;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -51,15 +53,41 @@ public void testissue261_1() throws Exception {

}

@Test
@Ignore //OutOfMemory Java heap space on CI
public void testissue261_2() throws Exception {
// https://github.com/SpoonLabs/coming/issues/261
// modes == files

fr.inria.coming.main.ComingMain.main(new String[] { "-location", "./src/main/resources", "-input", "files", "-output", "out", "-mode", "features"});

}

@Test
public void testissue261_3() throws Exception {
// https://github.com/SpoonLabs/coming/issues/261
// no P4J features in special example
// bug was in "DEL" in CodeDiffer
// change in string convention in gumtree, grrrrrr
// "DEL".equals(operation.getAction().getName()) -> "delete-node".equals(operation.getAction().getName())

fr.inria.coming.main.ComingMain.main(new String[] { "-location", "src/main/resources/issue261/261_s.java:src/main/resources/issue261/261_t.java", "-input", "filespair", "-output", "out", "-mode", "features"});

JsonObject jsonObject = new Gson().fromJson(new FileReader("out/features_261_s.java->261_t.java_FeatureAnalyzer.json"), JsonObject.class);

// jq ".files[0].features[1]"
final Set<Map.Entry<String, JsonElement>> entries = jsonObject.get("files").getAsJsonArray().get(0).getAsJsonObject().get("features").getAsJsonArray().get(1).getAsJsonObject().entrySet();

// check that we have a P4J feature
assertTrue(entries.size() > 0);
for (Map.Entry<String, JsonElement> entry : entries) {
final JsonObject value = (JsonObject) entry.getValue();
assertTrue(value.keySet().contains("P4J_AF_VF_CT_ABST_V_AF"));
}

}


@Test
public void testFeaturesOnComingEvolutionFromGit1() throws Exception {
ComingMain main = new ComingMain();
Expand Down
32 changes: 24 additions & 8 deletions src/test/java/fr/inria/prophet4j/GumtreeDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public void testChainSyntax() throws Exception {
List<Operation> operations = diff.getRootOperations();
// Three operations are expected
assertEquals(3, operations.size());
for (Operation operation : operations) {
Action action = operation.getAction();
if ("UPD".equals(action.getName())) {
// 'c' -> 'b'
assertEquals("\"c\"", operation.getSrcNode().toString());
assertEquals("\"b\"", operation.getDstNode().toString());
}
}
Operation operation = operations.get(0);
Action action = operation.getAction();
// big change in recent GT UPD -> update-node
assertEquals("update-node", action.getName());
// 'c' -> 'b'
assertEquals("\"c\"", operation.getSrcNode().toString());
assertEquals("\"b\"", operation.getDstNode().toString());

assertEquals("insert-node", operations.get(1).getAction().getName());

assertEquals("move-tree", operations.get(2).getAction().getName());

}

/*
Expand All @@ -56,6 +60,18 @@ public void testMultiLinesString() throws Exception {
*
* @throws Exception
*/
@Test
public void testDelete() throws Exception {
AstComparator comparator = new AstComparator();
String a = "class Foo{public void bar(){\ndouble b = (double) 1;\n}}";
String b = "class Foo{public void bar(){\n}}";
Diff diff = comparator.compare(a, b);
List<Operation> operations = diff.getRootOperations();
// Update Literal at Foo: 1 to ((double) (1))
assertEquals(1, operations.size());
assertEquals("delete-node", operations.get(0).getAction().getName());

}

@Test
public void testExplicitConversion() throws Exception {
Expand Down

0 comments on commit 4d1317f

Please sign in to comment.