Skip to content

Commit

Permalink
bump to spoon 11 / gumtree 4 (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored May 30, 2024
1 parent 9a8d3c8 commit dfa0a3f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>fr.inria.gforge.spoon.labs </groupId>
<artifactId>gumtree-spoon-ast-diff</artifactId>
<version>1.70</version>
<version>1.73</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import gumtree.spoon.diff.operations.UpdateOperation;
import spoon.reflect.declaration.CtElement;

import static fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer.getSafeStringRepr;

/**
*
* @author Matias Martinez
Expand Down Expand Up @@ -451,7 +453,7 @@ private List<MatchingEntity> matchElements(Operation affectedOperation, PatternE
// Scale the parent hierarchy and check types.
while (currentNodeFromAction != null && i_levels <= parentLevel) {
String typeOfNode = EntityTypesInfoResolver.getNodeLabelFromCtElement(currentNodeFromAction);
String valueOfNode = currentNodeFromAction.toString();
String valueOfNode = getSafeStringRepr(currentNodeFromAction);
String roleInParent = (currentNodeFromAction.getRoleInParent() != null)
? currentNodeFromAction.getRoleInParent().toString().toLowerCase()
: "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import spoon.reflect.visitor.filter.LineFilter;
import spoon.reflect.visitor.filter.TypeFilter;

import static fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer.getStringRepr;
import static fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer.getSafeStringRepr;

public abstract class AbstractCodeAnalyzer {

Expand Down Expand Up @@ -297,8 +297,8 @@ public boolean checkNormalGuardCondition(CtExpression condition) {
if (binOp != null && binOp.size() > 0) {

for (CtBinaryOperator ctBinaryOperator : binOp) {
if (getStringRepr(ctBinaryOperator.getRightHandOperand()).equals("null")
|| getStringRepr(ctBinaryOperator.getLeftHandOperand()).equals("null")) {
if (getSafeStringRepr(ctBinaryOperator.getRightHandOperand()).equals("null")
|| getSafeStringRepr(ctBinaryOperator.getLeftHandOperand()).equals("null")) {

return false;
}
Expand Down Expand Up @@ -351,8 +351,8 @@ public boolean checkNullCheckGuardCondition(CtExpression condition) {
if (binOp != null && binOp.size() > 0) {

for (CtBinaryOperator ctBinaryOperator : binOp) {
if (!getStringRepr(ctBinaryOperator.getRightHandOperand()).equals("null")
&& !getStringRepr(ctBinaryOperator.getLeftHandOperand()).equals("null")) {
if (!getSafeStringRepr(ctBinaryOperator.getRightHandOperand()).equals("null")
&& !getSafeStringRepr(ctBinaryOperator.getLeftHandOperand()).equals("null")) {

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void analyzeBinaryWhetehrMathRoot (CtBinaryOperator operatorunderstudy,
whethermathroot =false;
}

writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O5_IS_MATH_ROOT,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ getSafeStringRepr(operatorunderstudy), CodeFeatures.O5_IS_MATH_ROOT,
whethermathroot, "FEATURES_BINARYOPERATOR");
}

Expand Down Expand Up @@ -122,7 +122,7 @@ private void analyzeBinaryLogicalOperator(CtBinaryOperator operatorunderstudy, i
}
}

writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O2_LOGICAL_CONTAIN_NOT,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ getSafeStringRepr(operatorunderstudy), CodeFeatures.O2_LOGICAL_CONTAIN_NOT,
whethercontainnotoperator, "FEATURES_BINARYOPERATOR");

}
Expand Down Expand Up @@ -167,7 +167,7 @@ private void analyzeBinaryOperatorKind(CtBinaryOperator operatorunderstudy, int
for(int index=0; index<binoperatortype.size(); index++) {
CodeFeatures cerainfeature = binoperatortype.get(index);

final String operatorunderstudyStr = getStringRepr(operatorunderstudy);
final String operatorunderstudyStr = getSafeStringRepr(operatorunderstudy);
if(cerainfeature.toString().endsWith(operatorstring.toUpperCase()))
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ operatorunderstudyStr, cerainfeature,
true, "FEATURES_BINARYOPERATOR");
Expand All @@ -176,7 +176,7 @@ else writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ operatorund
}
}

public static String getStringRepr(CtElement operatorunderstudy) {
public static String getSafeStringRepr(CtElement element) {
// workaround for
// at spoon.support.reflect.reference.CtTypeReferenceImpl.getAccessType(CtTypeReferenceImpl.java:774)
// at spoon.reflect.visitor.ImportAnalyzer$ScannerListener.enter(ImportAnalyzer.java:135)
Expand Down Expand Up @@ -214,7 +214,7 @@ public static String getStringRepr(CtElement operatorunderstudy) {
// at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)

try {
return operatorunderstudy.toString();
return element.toString();
} catch (Exception e) {
// fake string, please open an issue if this is a problem
return "FIXME_oefa";
Expand All @@ -229,12 +229,12 @@ private void analyzeBinaryOperatorInvolveNull(CtBinaryOperator operatorunderstud
CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
CtExpression rightexpression = operatorunderstudy.getRightHandOperand();

final String leftStr = getStringRepr(leftexpression);
final String rightStr = getStringRepr(rightexpression);
final String leftStr = getSafeStringRepr(leftexpression);
final String rightStr = getSafeStringRepr(rightexpression);
if(leftStr.trim().equals("null") || rightStr.trim().equals("null"))
whethercontainnull = true;

writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O3_CONTAIN_NULL,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ getSafeStringRepr(operatorunderstudy), CodeFeatures.O3_CONTAIN_NULL,
whethercontainnull, "FEATURES_BINARYOPERATOR");

}
Expand All @@ -248,16 +248,16 @@ private void analyzeBinaryOperatorInvolve01 (CtBinaryOperator operatorunderstudy
CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
CtExpression rightexpression = operatorunderstudy.getRightHandOperand();

final String leftStr = getStringRepr(leftexpression);
final String rightStr = getStringRepr(rightexpression);
final String leftStr = getSafeStringRepr(leftexpression);
final String rightStr = getSafeStringRepr(rightexpression);
if(leftStr.trim().equals("0") || leftStr.trim().equals("0.0") ||
leftStr.trim().equals("1.0") || leftStr.trim().equals("1")
|| rightStr.trim().equals("0") || rightStr.trim().equals("0.0") ||
rightStr.trim().equals("1.0") || rightStr.trim().equals("1")
|| leftStr.trim().endsWith("1") || rightStr.trim().endsWith("1"))
whethercontain01 = true;

writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O3_CONTAIN_01,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ getSafeStringRepr(operatorunderstudy), CodeFeatures.O3_CONTAIN_01,
whethercontain01, "FEATURES_BINARYOPERATOR");
}

Expand All @@ -274,7 +274,7 @@ private void analyzeBinaryOperatorCompareInCondition (CtElement wholeoriginal, C
whethercompareincondition = true;
}

writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O4_COMPARE_IN_CONDITION,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ getSafeStringRepr(operatorunderstudy), CodeFeatures.O4_COMPARE_IN_CONDITION,
whethercompareincondition, "FEATURES_BINARYOPERATOR");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -24,9 +22,8 @@
import fr.inria.coming.main.ComingProperties;
import fr.inria.coming.repairability.models.InstanceStats;
import gumtree.spoon.diff.operations.Operation;
import org.json.simple.JSONObject;

import static fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer.getStringRepr;
import static fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer.getSafeStringRepr;

public class JSonPatternInstanceOutput implements IOutput {

Expand Down Expand Up @@ -154,9 +151,9 @@ public static JsonObject getJSONFromOperator(Operation operation) {
: "null");

op.addProperty("src_parent",
(operation.getSrcNode() != null) ? getStringRepr(operation.getSrcNode().getParent()) : "null");
(operation.getSrcNode() != null) ? getSafeStringRepr(operation.getSrcNode().getParent()) : "null");
op.addProperty("dst_parent",
(operation.getDstNode() != null) ? getStringRepr(operation.getDstNode().getParent()) : "null");
(operation.getDstNode() != null) ? getSafeStringRepr(operation.getDstNode().getParent()) : "null");

return op;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.reference.CtTypeReference;

import static fr.inria.coming.codefeatures.codeanalyze.BinaryOperatorAnalyzer.getSafeStringRepr;

/**
* Arja tries to correct the code by inserting statements that are in the source
* file either in a direct approach or a type matching approach.
Expand Down Expand Up @@ -218,7 +220,7 @@ private boolean checkSrcIncludesDstTemplate(CtElement srcNode, CtElement dstNode
continue;
}

String srcAsString = currentSrcElement.toString();
String srcAsString = getSafeStringRepr(currentSrcElement);
Set<CtElement> elementsInSubtree = new HashSet<>();
elementsInSubtree.add(currentSrcElement);
for (int j = 0; j == 0 || (i + j < allSrcElements.size()
Expand Down
21 changes: 7 additions & 14 deletions src/test/java/fr/inria/prophet4j/GumtreeDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,10 @@ public void testExplicitConversion() throws Exception {
assertEquals("1", operations.get(0).getDstNode().toString());
}

/**
* This test shows there is bug/limitation in the gumtree diff. Diff exists but
* not found.
*
* @throws Exception
*/

@Test
public void DiffNotFoundTest() throws Exception {
public void diffFoundTest() throws Exception {
// contract: we can find a diff with cast difference

/*
* SRC: x0 = 0.5 * (x0 + x1 - FastMath.max(rtol * FastMath.abs(x1), atol));
* TARGET: x0 = 0.5 * ((int)x0 + x1 - FastMath.max(rtol * FastMath.abs(x1),
Expand All @@ -127,12 +122,10 @@ public void DiffNotFoundTest() throws Exception {
List<Operation> operations = diff.getRootOperations();
System.out.println(operations);

// Delete VariableRead at org.apache.commons.math.analysis.solvers.BaseSecantSolver:188
// x0
//, Insert VariableRead at org.apache.commons.math.analysis.solvers.BaseSecantSolver:188
// ((int) (x0))

assertEquals(2, operations.size());
// [Insert TypeReference at org.apache.commons.math.analysis.solvers.BaseSecantSolver:188
// int
//]
assertEquals(1, operations.size());

/*
* SRC: n1n2prod * (n1 + n2 + 1) / 12.0; TARGET: (double) ((double) n1n2prod *
Expand Down

0 comments on commit dfa0a3f

Please sign in to comment.