Skip to content

Commit

Permalink
fix #264
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored May 1, 2024
1 parent 4d1317f commit 8e49c8c
Show file tree
Hide file tree
Showing 14 changed files with 2,240 additions and 38 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.62</version>
<version>1.70</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import spoon.reflect.visitor.filter.LineFilter;
import spoon.reflect.visitor.filter.TypeFilter;

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

public abstract class AbstractCodeAnalyzer {

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

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

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

for (CtBinaryOperator ctBinaryOperator : binOp) {
if (!ctBinaryOperator.getRightHandOperand().toString().equals("null")
&& !ctBinaryOperator.getLeftHandOperand().toString().equals("null")) {
if (!getStringRepr(ctBinaryOperator.getRightHandOperand()).equals("null")
&& !getStringRepr(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)+"_"+operatorunderstudy, CodeFeatures.O5_IS_MATH_ROOT,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(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)+"_"+operatorunderstudy, CodeFeatures.O2_LOGICAL_CONTAIN_NOT,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O2_LOGICAL_CONTAIN_NOT,
whethercontainnotoperator, "FEATURES_BINARYOPERATOR");

}
Expand Down Expand Up @@ -166,27 +166,75 @@ private void analyzeBinaryOperatorKind(CtBinaryOperator operatorunderstudy, int

for(int index=0; index<binoperatortype.size(); index++) {
CodeFeatures cerainfeature = binoperatortype.get(index);


final String operatorunderstudyStr = getStringRepr(operatorunderstudy);
if(cerainfeature.toString().endsWith(operatorstring.toUpperCase()))
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, cerainfeature,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ operatorunderstudyStr, cerainfeature,
true, "FEATURES_BINARYOPERATOR");
else writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, cerainfeature,
else writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+ operatorunderstudyStr, cerainfeature,
false, "FEATURES_BINARYOPERATOR");
}
}


public static String getStringRepr(CtElement operatorunderstudy) {
// workaround for
// at spoon.support.reflect.reference.CtTypeReferenceImpl.getAccessType(CtTypeReferenceImpl.java:774)
// at spoon.reflect.visitor.ImportAnalyzer$ScannerListener.enter(ImportAnalyzer.java:135)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:124)
// at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:184)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:106)
// at spoon.reflect.visitor.CtScanner.visitCtTypeReference(CtScanner.java:813)
// at spoon.support.reflect.reference.CtTypeReferenceImpl.accept(CtTypeReferenceImpl.java:79)
// at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:145)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:127)
// at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:184)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:106)
// at spoon.reflect.visitor.CtScanner.visitCtTypeAccess(CtScanner.java:825)
// at spoon.support.reflect.code.CtTypeAccessImpl.accept(CtTypeAccessImpl.java:28)
// at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:145)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:127)
// at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:184)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:106)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:83)
// at spoon.reflect.visitor.CtScanner.visitCtInvocation(CtScanner.java:528)
// at spoon.support.reflect.code.CtInvocationImpl.accept(CtInvocationImpl.java:46)
// at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:145)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:127)
// at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:184)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:106)
// at spoon.reflect.visitor.CtScanner.visitCtBinaryOperator(CtScanner.java:312)
// at spoon.support.reflect.code.CtBinaryOperatorImpl.accept(CtBinaryOperatorImpl.java:34)
// at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:145)
// at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:127)
// at spoon.reflect.visitor.ImportAnalyzer.process(ImportAnalyzer.java:48)
// at spoon.reflect.visitor.ForceFullyQualifiedProcessor.process(ForceFullyQualifiedProcessor.java:28)
// at spoon.reflect.visitor.DefaultJavaPrettyPrinter.applyPreProcessors(DefaultJavaPrettyPrinter.java:2136)
// at spoon.reflect.visitor.DefaultJavaPrettyPrinter.printElement(DefaultJavaPrettyPrinter.java:281)
// at spoon.support.reflect.declaration.CtElementImpl.toString(CtElementImpl.java:295)
// at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)

try {
return operatorunderstudy.toString();
} catch (Exception e) {
// fake string, please open an issue if this is a problem
return "FIXME_oefa";
}
}

//icse15dataset: 1367617, 267239, 597123, 1348493, 614068, 306964, 902094, 410960, 1456710, 1458106
private void analyzeBinaryOperatorInvolveNull(CtBinaryOperator operatorunderstudy, int operatorindex, Cntx<Object> context) {

boolean whethercontainnull = false;

CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
CtExpression rightexpression = operatorunderstudy.getRightHandOperand();

if(leftexpression.toString().trim().equals("null") || rightexpression.toString().trim().equals("null"))

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

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

}
Expand All @@ -199,15 +247,17 @@ private void analyzeBinaryOperatorInvolve01 (CtBinaryOperator operatorunderstudy

CtExpression leftexpression = operatorunderstudy.getLeftHandOperand();
CtExpression rightexpression = operatorunderstudy.getRightHandOperand();

if(leftexpression.toString().trim().equals("0") || leftexpression.toString().trim().equals("0.0") ||
leftexpression.toString().trim().equals("1.0") || leftexpression.toString().trim().equals("1")
|| rightexpression.toString().trim().equals("0") || rightexpression.toString().trim().equals("0.0") ||
rightexpression.toString().trim().equals("1.0") || rightexpression.toString().trim().equals("1")
|| leftexpression.toString().trim().endsWith("1") || rightexpression.toString().trim().endsWith("1"))

final String leftStr = getStringRepr(leftexpression);
final String rightStr = getStringRepr(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)+"_"+operatorunderstudy, CodeFeatures.O3_CONTAIN_01,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O3_CONTAIN_01,
whethercontain01, "FEATURES_BINARYOPERATOR");
}

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

writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+operatorunderstudy, CodeFeatures.O4_COMPARE_IN_CONDITION,
writeGroupedInfo(context, Integer.toString(operatorindex)+"_"+getStringRepr(operatorunderstudy), CodeFeatures.O4_COMPARE_IN_CONDITION,
whethercompareincondition, "FEATURES_BINARYOPERATOR");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ private int[] argumentDiffMethod(List<CtElement> argumentsoriginal, List<CtEleme
CtElement original = argumentsoriginal.get(index);
CtElement other = argumentsother.get(index);

if(original.equals(other) || original.toString().equals(other.toString())) {
if(original.equals(other)) {
// same
} else {
numberdiffargument+=1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ private int[] argumentDiff(List<CtElement> argumentsoriginal, List<CtElement> ar
CtElement original = argumentsoriginal.get(index);
CtElement other = argumentsother.get(index);

if(original.equals(other) || original.toString().equals(other.toString())) {
if(original.equals(other)) {
// same
} else {
numberdiffargument+=1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import gumtree.spoon.diff.operations.Operation;
import org.json.simple.JSONObject;

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

public class JSonPatternInstanceOutput implements IOutput {

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

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

return op;
}
Expand Down
Loading

0 comments on commit 8e49c8c

Please sign in to comment.