diff --git a/src/main/java/fr/inria/coming/codefeatures/P4JFeatureAnalyzer.java b/src/main/java/fr/inria/coming/codefeatures/P4JFeatureAnalyzer.java index 96666613a..25c03de50 100644 --- a/src/main/java/fr/inria/coming/codefeatures/P4JFeatureAnalyzer.java +++ b/src/main/java/fr/inria/coming/codefeatures/P4JFeatureAnalyzer.java @@ -14,6 +14,7 @@ import java.util.Map; import fr.inria.coming.core.entities.interfaces.IRevisionPair; +import fr.inria.prophet4j.feature.extended.ExtendedFeatureCross; import org.apache.log4j.Logger; import com.google.gson.JsonObject; @@ -26,7 +27,6 @@ import fr.inria.coming.main.ComingProperties; import fr.inria.prophet4j.feature.Feature; import fr.inria.prophet4j.feature.FeatureCross; -import fr.inria.prophet4j.feature.original.OriginalFeatureCross; import fr.inria.prophet4j.utility.CodeDiffer; import fr.inria.prophet4j.utility.Option; import fr.inria.prophet4j.utility.Option.FeatureOption; @@ -123,13 +123,12 @@ public JsonObject getSimleP4JJSON(Option option, File target, List simpleP4JFeatures= featureCross.getSimpleP4JFeatures(); - OriginalFeatureCross ofc = (OriginalFeatureCross) featureCross; for(Feature f: simpleP4JFeatures) { - Boolean positive = ofc.containFeature(f); + Boolean positive = featureCross.containFeature(f); if(numericalIndixator) { - jsonfile.addProperty("P4J_"+ofc.getCrossType()+"_"+f, positive?"1":"0"); + jsonfile.addProperty("P4J_"+f, positive?"1":"0"); }else { - jsonfile.addProperty("P4J_"+ofc.getCrossType()+"_"+f, positive?"true":"false"); + jsonfile.addProperty("P4J_"+f, positive?"true":"false"); } } @@ -156,7 +155,7 @@ public JsonObject genVectorsCSV(Option option, File patchedFile, List= - OP_GT_AF, // > - OP_EQ_AF, // == - OP_NE_AF, // != - UOP_INC_AF, // ++a a++ - UOP_DEC_AF, // --a a-- - // VARIABLE_AF, // variable - ASSIGN_LHS_AF, // a= - // ASSIGN_RHS_AF, // =a - ASSIGN_ZERO_AF, // zero - ASSIGN_CONST_AF, // constant - // EXCLUDE_ATOM_AF, // not include - // OPERATE_LHS_AF, // a+ a- a* a/ a% a&& a|| ... - // OPERATE_RHS_AF, // +a -a *a /a %a &&a ||a ... - CHANGED_AF, // ++a --a a++ a-- += -= *= /= = - DEREF_AF, // [] - INDEX_AF, // [] - MEMBER_ACCESS_AF, // [] * & . -> (only .) - CALLEE_AF, - CALL_ARGUMENT_AF, - ABST_V_AF, - STMT_LABEL_AF, // label - STMT_LOOP_AF, // for foreach do while - STMT_ASSIGN_AF, // = - STMT_CALL_AF, // print() - STMT_COND_AF, // if ... - STMT_CONTROL_AF, // break continue return throw - R_STMT_ASSIGN_AF, // replace version - R_STMT_CALL_AF, // replace version - R_STMT_COND_AF, // replace version - R_STMT_CONTROL_AF, // replace version -// ADDRESS_OF_AF, // Inapplicable to Java - } - - enum RepairFeature implements OriginalFeature { - /** - * inserting a potentially guarded control statement before a program point (AddControlRepair in Prophet4C) - * IfExitKind - */ - INSERT_CONTROL_RF, - /** - * adding a guard condition to an existing statement (GuardRepair in Prophet4C) - * GuardKind - * SpecialGuardKind - */ - INSERT_GUARD_RF, - /** - * inserting a non-control statement before a program point (AddStmtRepair in Prophet4C) - * AddInitKind - * AddAndReplaceKind - */ - INSERT_STMT_RF, - /** - * replacing a branch condition (CondRepair in Prophet4C) - * TightenConditionKind - * LoosenConditionKind - */ - REPLACE_COND_RF, - /** - * replacing an existing statement (ReplaceStmtRepair in Prophet4C) - * ReplaceKind - * ReplaceStringKind - */ - REPLACE_STMT_RF, - /** - * deleting a guard condition from an existing statement (GuardRepair in Prophet4C) - * RemoveGuardKind - */ - REMOVE_PARTIAL_IF, - /** - * replacing an existing statement - * RemoveSTMTKind - */ - REMOVE_STMT, - /** - * deleting a if condition and the block inside it from an existing statement (GuardRepair in Prophet4C) - * RemoveGuardBlockKind - */ - REMOVE_WHOLE_IF, - /** - * remove a whole block - */ - REMOVE_WHOLE_BLOCK, - - - } - - enum ValueFeature implements OriginalFeature { - MODIFIED_VF, - MODIFIED_SIMILAR_VF, - FUNC_ARGUMENT_VF, - MEMBER_VF, - LOCAL_VARIABLE_VF, - GLOBAL_VARIABLE_VF, - ZERO_CONST_VF, - NONZERO_CONST_VF, - STRING_LITERAL_VF, - SIZE_LITERAL_VF, -// POINTER_VF, // Inapplicable to Java -// STRUCT_POINTER_VF, // Inapplicable to Java - } -} diff --git a/src/main/java/fr/inria/prophet4j/feature/original/OriginalFeatureCross.java b/src/main/java/fr/inria/prophet4j/feature/original/OriginalFeatureCross.java deleted file mode 100644 index ccc7eba7c..000000000 --- a/src/main/java/fr/inria/prophet4j/feature/original/OriginalFeatureCross.java +++ /dev/null @@ -1,143 +0,0 @@ -package fr.inria.prophet4j.feature.original; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import fr.inria.prophet4j.feature.Feature; -import fr.inria.prophet4j.feature.FeatureCross; -import fr.inria.prophet4j.feature.original.OriginalFeature.AtomicFeature; - -import static fr.inria.prophet4j.feature.original.OriginalFeature.*; - -public class OriginalFeatureCross implements FeatureCross, Serializable { - static final long serialVersionUID = 1L; - private Integer id; - private Double degree; - private List features; - private List simpleP4Jfeatures; - private CrossType crossType; - - public OriginalFeatureCross(Integer id) { - this(id, 1.0); - } - - public OriginalFeatureCross(Integer id, Double degree) { - this.id = id; - this.degree = degree; - this.features = new ArrayList<>(); - if (id >= FEATURE_BASE_3) { - int tmp = id - FEATURE_BASE_3; - int ordinal0 = tmp / VF_SIZE; - int ordinal1 = tmp % VF_SIZE; - this.features.add(AtomicFeature.values()[ordinal0]); - this.features.add(ValueFeature.values()[ordinal1]); - } else if (id >= FEATURE_BASE_2) { - int tmp = id - FEATURE_BASE_2; - int ordinal0 = tmp / (AF_SIZE * AF_SIZE); - int ordinal1 = (tmp % (AF_SIZE * AF_SIZE)) / AF_SIZE; - int ordinal2 = (tmp % (AF_SIZE * AF_SIZE)) % AF_SIZE; - this.features.add(Position.values()[ordinal0]); - this.features.add(AtomicFeature.values()[ordinal1]); - this.features.add(AtomicFeature.values()[ordinal2]); - } else if (id >= FEATURE_BASE_1) { - int tmp = id - FEATURE_BASE_1; - int ordinal0 = tmp / (AF_SIZE * RF_SIZE); - int ordinal1 = (tmp % (AF_SIZE * RF_SIZE)) / RF_SIZE; - int ordinal2 = (tmp % (AF_SIZE * RF_SIZE)) % RF_SIZE; - this.features.add(Position.values()[ordinal0]); - this.features.add(AtomicFeature.values()[ordinal1]); - this.features.add(RepairFeature.values()[ordinal2]); - } else if (id >= FEATURE_BASE_0) { - int ordinal0 = id - FEATURE_BASE_0; - this.features.add(RepairFeature.values()[ordinal0]); - } - } - - public OriginalFeatureCross(CrossType crossType, List features) { - this(crossType, features, 1.0); - } - - - public OriginalFeatureCross(CrossType crossType, List features, Double degree) { - int ordinal0, ordinal1, ordinal2; - switch (crossType) { - case RF_CT: - assert features.size() == 1; - assert features.get(0) instanceof RepairFeature; - ordinal0 = ((RepairFeature) features.get(0)).ordinal(); - this.id = FEATURE_BASE_0 + ordinal0; - break; - case POS_AF_RF_CT: - assert features.size() == 3; - assert features.get(0) instanceof Position; - assert features.get(1) instanceof AtomicFeature; - assert features.get(2) instanceof RepairFeature; - ordinal0 = ((Position) features.get(0)).ordinal(); - ordinal1 = ((AtomicFeature) features.get(1)).ordinal(); - ordinal2 = ((RepairFeature) features.get(2)).ordinal(); - this.id = FEATURE_BASE_1 + ordinal0 * AF_SIZE * RF_SIZE + ordinal1 * RF_SIZE + ordinal2; - break; - case POS_AF_AF_CT: - assert features.size() == 3; - assert features.get(0) instanceof Position; - assert features.get(1) instanceof AtomicFeature; - assert features.get(2) instanceof AtomicFeature; - ordinal0 = ((Position) features.get(0)).ordinal(); - ordinal1 = ((AtomicFeature) features.get(1)).ordinal(); - ordinal2 = ((AtomicFeature) features.get(2)).ordinal(); - this.id = FEATURE_BASE_2 + ordinal0 * AF_SIZE * AF_SIZE + ordinal1 * AF_SIZE + ordinal2; - break; - case AF_VF_CT: - assert features.size() == 2; - assert features.get(0) instanceof AtomicFeature; - assert features.get(1) instanceof ValueFeature; - ordinal0 = ((AtomicFeature) features.get(0)).ordinal(); - ordinal1 = ((ValueFeature) features.get(1)).ordinal(); - this.id = FEATURE_BASE_3 + ordinal0 * VF_SIZE + ordinal1; - break; - } - this.degree = degree; - this.features = features; - this.crossType = crossType; - } - - public Integer getId() { - return id; - } - - public Double getDegree() { - return degree; - } - - public String getCrossType() { - return this.crossType.toString(); - } - - public List getFeatures() { - return features; - } - - public List getSimpleP4JFeatures() { - this.simpleP4Jfeatures = new ArrayList<>(); - for(int i=0; i < RepairFeature.values().length;i++) { - this.simpleP4Jfeatures.add(RepairFeature.values()[i]); - } - for(int i=0; i < AtomicFeature.values().length;i++) { - this.simpleP4Jfeatures.add(AtomicFeature.values()[i]); - } - for(int i=0; i < ValueFeature.values().length;i++) { - this.simpleP4Jfeatures.add(ValueFeature.values()[i]); - } - return this.simpleP4Jfeatures; - } - - public boolean containFeature(Feature feature) { - return features.contains(feature); - } - - @Override - public String toString() { - return "FeatureCross: " + features; - } -} diff --git a/src/main/java/fr/inria/prophet4j/feature/original/OriginalFeatureExtractor.java b/src/main/java/fr/inria/prophet4j/feature/original/OriginalFeatureExtractor.java deleted file mode 100755 index bcfb6e744..000000000 --- a/src/main/java/fr/inria/prophet4j/feature/original/OriginalFeatureExtractor.java +++ /dev/null @@ -1,503 +0,0 @@ -package fr.inria.prophet4j.feature.original; - -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import fr.inria.prophet4j.feature.Feature; -import fr.inria.prophet4j.feature.Feature.Position; -import fr.inria.prophet4j.feature.original.OriginalFeature.CrossType; -import fr.inria.prophet4j.feature.original.OriginalFeature.AtomicFeature; -import fr.inria.prophet4j.feature.original.OriginalFeature.RepairFeature; -import fr.inria.prophet4j.feature.original.OriginalFeature.ValueFeature; -import fr.inria.prophet4j.utility.Structure.FeatureVector; -import fr.inria.prophet4j.utility.Structure.Repair; -import fr.inria.prophet4j.feature.original.util.OriginalFeatureVisitor; -import fr.inria.prophet4j.feature.FeatureExtractor; -import spoon.reflect.code.CtArrayAccess; -import spoon.reflect.code.CtIf; -import spoon.reflect.code.CtLiteral; -import spoon.reflect.code.CtLocalVariable; -import spoon.reflect.code.CtStatement; -import spoon.reflect.code.CtStatementList; -import spoon.reflect.code.CtVariableAccess; -import spoon.reflect.declaration.CtElement; -import spoon.reflect.declaration.CtField; -import spoon.reflect.declaration.CtMethod; -import spoon.reflect.declaration.CtParameter; -import spoon.reflect.reference.CtExecutableReference; -import spoon.reflect.visitor.filter.TypeFilter; - -// based on FeatureExtract.cpp, RepairGenerator.cpp -public class OriginalFeatureExtractor implements FeatureExtractor { - - private Map valueExprInfo = new HashMap<>(); - - private EnumSet getRepairFeatures(Repair repair) { - EnumSet repairFeatures = EnumSet.noneOf(RepairFeature.class); - if (repair.kind == null) { - // I used null in FeatureExtractorTest.java - return repairFeatures; - } - switch (repair.kind) { - case IfExitKind: - // RepairFeature.INSERT_CONTROL_RF == AddControlRepair - repairFeatures.add(RepairFeature.INSERT_CONTROL_RF); - break; - case GuardKind: - case SpecialGuardKind: - // RepairFeature.INSERT_GUARD_RF == GuardRepair - repairFeatures.add(RepairFeature.INSERT_GUARD_RF); - break; - case AddInitKind: - case AddAndReplaceKind: - // RepairFeature.INSERT_STMT_RF == AddStmtRepair - repairFeatures.add(RepairFeature.INSERT_STMT_RF); - break; - case TightenConditionKind: - case LoosenConditionKind: - // RepairFeature.REPLACE_COND_RF == CondRepair - repairFeatures.add(RepairFeature.REPLACE_COND_RF); - break; - case ReplaceKind: - case ReplaceStringKind: - // RepairFeature.REPLACE_STMT_RF == ReplaceStmtRepair - repairFeatures.add(RepairFeature.REPLACE_STMT_RF); - break; - //Delete a guard in source file - case RemovePartialIFKind: - repairFeatures.add(RepairFeature.REMOVE_PARTIAL_IF); - break; - //Delete statements in source file - case RemoveSTMTKind: - repairFeatures.add(RepairFeature.REMOVE_STMT); - break; - case RemoveWholeIFKind: - repairFeatures.add(RepairFeature.REMOVE_WHOLE_IF); - break; - case RemoveWholeBlockKind: - repairFeatures.add(RepairFeature.REMOVE_WHOLE_BLOCK); - break; - } - return repairFeatures; - } - - private EnumSet getValueFeature(final String valueStr, final Repair repair, Map valueExprInfo) { - EnumSet valueFeatures = EnumSet.noneOf(ValueFeature.class); - if (repair.oldRExpr != null && repair.newRExpr != null) { - String oldStr = repair.oldRExpr.toString(); - String newStr = repair.newRExpr.toString(); - if (valueStr.equals(newStr)) - valueFeatures.add(ValueFeature.MODIFIED_VF); - // I can not figure out the meaning of MODIFIED_SIMILAR_VF - if (oldStr.length() > 0 && newStr.length() > 0) { - double ratio = ((double)oldStr.length()) / newStr.length(); - if (ratio > 0.5 && ratio < 2 && oldStr.length() > 3 && newStr.length() > 3) - if (oldStr.contains(newStr) || newStr.contains(oldStr)) - valueFeatures.add(ValueFeature.MODIFIED_SIMILAR_VF); - } - } - CtElement element = repair.dstElem; - if (element != null) { - CtMethod FD = element.getParent(new TypeFilter<>(CtMethod.class)); - if (FD != null) { - for (Object parameter: FD.getParameters()) { - if (parameter instanceof CtParameter) { - CtParameter VD = (CtParameter) parameter; - if (VD.getSimpleName().equals(valueStr)) - valueFeatures.add(ValueFeature.FUNC_ARGUMENT_VF); - } - } - } - } - assert(valueExprInfo.containsKey(valueStr)); - CtElement E = valueExprInfo.get(valueStr); - if (E instanceof CtVariableAccess || E instanceof CtArrayAccess || E instanceof CtLocalVariable) { - if (E instanceof CtLocalVariable) { - valueFeatures.add(ValueFeature.LOCAL_VARIABLE_VF); - } else { - valueFeatures.add(ValueFeature.GLOBAL_VARIABLE_VF); - } - } else if (E instanceof CtExecutableReference){ - // just make CALLEE_AF be meaningful - if (((CtExecutableReference) E).getParameters().size() > 0){ - valueFeatures.add(ValueFeature.LOCAL_VARIABLE_VF); - } - } else if (E instanceof CtIf){ - // just make R_STMT_COND_AF be meaningful - valueFeatures.add(ValueFeature.LOCAL_VARIABLE_VF); - } -// if (E instanceof CtVariable) { -// if (E instanceof CtLocalVariable) -// valueFeatures.add(SchemaFeature.LOCAL_VARIABLE_VF); -// else -// valueFeatures.add(SchemaFeature.GLOBAL_VARIABLE_VF); -// } else if (E instanceof CtVariableReference) { -// if (E instanceof CtLocalVariableReference) -// valueFeatures.add(SchemaFeature.LOCAL_VARIABLE_VF); -// else -// valueFeatures.add(SchemaFeature.GLOBAL_VARIABLE_VF); -// } - if (valueStr.contains("length") || valueStr.contains("size")) - valueFeatures.add(ValueFeature.SIZE_LITERAL_VF); - if (E.getElements(new TypeFilter<>(CtField.class)).size() > 0) - valueFeatures.add(ValueFeature.MEMBER_VF); - if (E instanceof CtLiteral) { - Object value = ((CtLiteral)E).getValue(); - if (value instanceof String) { - valueFeatures.add(ValueFeature.STRING_LITERAL_VF); - } else if (value instanceof Integer) { // ? - if ((Integer) value == 0) { - valueFeatures.add(ValueFeature.ZERO_CONST_VF); - } else { - valueFeatures.add(ValueFeature.NONZERO_CONST_VF); - } - } - } - return valueFeatures; - } - - // this is for CodeDiffer.java - public FeatureVector extractFeature(Repair repair, CtElement atom) { - List stmtsC = getCurrentStmts(repair); - List stmtsF = new ArrayList<>(); - List stmtsL = new ArrayList<>(); - getNearbyStmts(repair, stmtsF, stmtsL); // update values by reference - - Map> srcMapC = new HashMap<>(); - Map> srcMapF = new HashMap<>(); - Map> srcMapL = new HashMap<>(); - Map> dstMap = new OriginalFeatureVisitor(valueExprInfo).traverseRepair(repair, atom); - - for (CtElement stmt : stmtsC) { - Map> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt); - map.forEach((k, v) -> srcMapC.merge(k, v, (v1, v2) -> { - v1.addAll(v2); - return v1; - })); - } - for (CtElement stmt : stmtsF) { - Map> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt); - map.forEach((k, v) -> srcMapF.merge(k, v, (v1, v2) -> { - v1.addAll(v2); - return v1; - })); - } - for (CtElement stmt : stmtsL) { - Map> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt); - map.forEach((k, v) -> srcMapL.merge(k, v, (v1, v2) -> { - v1.addAll(v2); - return v1; - })); - } - - // this will be merged so we do not care about this value named marked - FeatureVector featureVector = new FeatureVector(); - // RepairFeatureNum = RepairFeatureNum = 5 - EnumSet repairFeatures = getRepairFeatures(repair); - // ModKind should be synonyms of RepairType - for (Feature repairFeature : repairFeatures) { - // RF_CT - List features = new ArrayList<>(); - features.add(repairFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.RF_CT, features)); - } - - // GlobalFeatureNum = 3 * AtomFeatureNum * RepairFeatureNum = 450 - for (Feature repairFeature : repairFeatures) { - if (srcMapC.containsKey("@")) { - Set atomicFeatures = srcMapC.get("@"); - for (Feature atomicFeature : atomicFeatures) { - // POS_AF_RF_CT - List globalFeatures = new ArrayList<>(); - globalFeatures.add(Position.POS_C); - globalFeatures.add(atomicFeature); - globalFeatures.add(repairFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.POS_AF_RF_CT, globalFeatures)); - } - } - if (srcMapF.containsKey("@")) { - Set atomicFeatures = srcMapF.get("@"); - for (Feature atomicFeature : atomicFeatures) { - // POS_AF_RF_CT - List globalFeatures = new ArrayList<>(); - globalFeatures.add(Position.POS_F); - globalFeatures.add(atomicFeature); - globalFeatures.add(repairFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.POS_AF_RF_CT, globalFeatures)); - } - } - if (srcMapL.containsKey("@")) { - Set atomicFeatures = srcMapL.get("@"); - for (Feature atomicFeature : atomicFeatures) { - // POS_AF_RF_CT - List globalFeatures = new ArrayList<>(); - globalFeatures.add(Position.POS_L); - globalFeatures.add(atomicFeature); - globalFeatures.add(repairFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.POS_AF_RF_CT, globalFeatures)); - } - } - } - srcMapC.remove("@"); - srcMapF.remove("@"); - srcMapL.remove("@"); - dstMap.remove("@"); - - // VarCrossFeatureNum = 3 * AtomFeatureNum * AtomFeatureNum = 2700 - for (String key : dstMap.keySet()) { - if (valueExprInfo.containsKey(key)) { - CtElement E = valueExprInfo.get(key); - if (E instanceof CtLiteral) - if (((CtLiteral)E).getValue() instanceof Integer) // ? - continue; - } - if (srcMapC.containsKey(key)) { - Set dstAtomicFeatures = dstMap.get(key); - Set srcAtomicFeatures = srcMapC.get(key); - for (Feature dstAtomicFeature : dstAtomicFeatures) { - for (Feature srcAtomicFeature : srcAtomicFeatures) { - // POS_AF_AF_CT - List varCrossFeatures = new ArrayList<>(); - varCrossFeatures.add(Position.POS_C); - varCrossFeatures.add(srcAtomicFeature); - varCrossFeatures.add(dstAtomicFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.POS_AF_AF_CT, varCrossFeatures)); - } - } - } - if (srcMapF.containsKey(key)) { - Set dstAtomicFeatures = dstMap.get(key); - Set srcAtomicFeatures = srcMapF.get(key); - for (Feature dstAtomicFeature : dstAtomicFeatures) { - for (Feature srcAtomicFeature : srcAtomicFeatures) { - // POS_AF_AF_CT - List varCrossFeatures = new ArrayList<>(); - varCrossFeatures.add(Position.POS_F); - varCrossFeatures.add(srcAtomicFeature); - varCrossFeatures.add(dstAtomicFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.POS_AF_AF_CT, varCrossFeatures)); - } - } - } - if (srcMapL.containsKey(key)) { - Set dstAtomicFeatures = dstMap.get(key); - Set srcAtomicFeatures = srcMapL.get(key); - for (Feature dstAtomicFeature : dstAtomicFeatures) { - for (Feature srcAtomicFeature : srcAtomicFeatures) { - // POS_AF_AF_CT - List varCrossFeatures = new ArrayList<>(); - varCrossFeatures.add(Position.POS_L); - varCrossFeatures.add(srcAtomicFeature); - varCrossFeatures.add(dstAtomicFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.POS_AF_AF_CT, varCrossFeatures)); - } - } - } - } - - // ValueCrossFeatureNum = AtomFeatureNum * ValueFeatureNum = 360 - for (String key : dstMap.keySet()) { - Set atomicFeatures = dstMap.get(key); - Set valueFeatures = getValueFeature(key, repair, valueExprInfo); - for (Feature atomicFeature : atomicFeatures) { - for (Feature valueFeature : valueFeatures) { - // AF_VF_CT - List valueCrossFeature = new ArrayList<>(); - valueCrossFeature.add(atomicFeature); - valueCrossFeature.add(valueFeature); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.AF_VF_CT, valueCrossFeature)); - } - } - } - return featureVector; - } - - /** - * This function returns simple P4J features - */ - public FeatureVector extractSimpleP4JFeature(Repair repair, CtElement atom) { - List stmtsC = getCurrentStmts(repair); - List stmtsF = new ArrayList<>(); - List stmtsL = new ArrayList<>(); - getNearbyStmts(repair, stmtsF, stmtsL); - - Map> srcMapC = new HashMap<>(); - Map> srcMapF = new HashMap<>(); - Map> srcMapL = new HashMap<>(); - - for (CtElement stmt : stmtsC) { - Map> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt); - map.forEach((k, v) -> srcMapC.merge(k, v, (v1, v2) -> { - v1.addAll(v2); - return v1; - })); - } - for (CtElement stmt : stmtsF) { - Map> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt); - map.forEach((k, v) -> srcMapF.merge(k, v, (v1, v2) -> { - v1.addAll(v2); - return v1; - })); - } - for (CtElement stmt : stmtsL) { - Map> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt); - map.forEach((k, v) -> srcMapL.merge(k, v, (v1, v2) -> { - v1.addAll(v2); - return v1; - })); - } - - FeatureVector featureVector = new FeatureVector(); - - List CFeatures = new ArrayList<>(); - List FFeatures = new ArrayList<>(); - List LFeatures = new ArrayList<>(); - - /** - * current position - */ - // repair features - EnumSet repairFeatures = getRepairFeatures(repair); - for (Feature repairFeature : repairFeatures) { - CFeatures.add(repairFeature); - } - // atomic and value features - for (String key : srcMapC.keySet()) { - Set atomicFeatures = srcMapC.get(key); - for (AtomicFeature af : atomicFeatures) { - if (!CFeatures.contains(af)) { - CFeatures.add(af); - } - } - if (!key.contains("@")) { - Set valueFeatures = getValueFeature(key, repair, valueExprInfo); - for (ValueFeature vf : valueFeatures) { - if (!CFeatures.contains(vf)) { - CFeatures.add(vf); - } - } - } - } - - /** - * former position - */ - - for (String key : srcMapF.keySet()) { - Set atomicFeatures = srcMapF.get(key); - for (AtomicFeature af : atomicFeatures) { - if (!FFeatures.contains(af)) { - FFeatures.add(af); - } - } - } - - /** - * later position - */ - - for (String key : srcMapL.keySet()) { - Set atomicFeatures = srcMapL.get(key); - for (AtomicFeature af : atomicFeatures) { - if (!LFeatures.contains(af)) { - LFeatures.add(af); - } - } - } - - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.SRC, CFeatures)); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.FORMER, FFeatures)); - featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.LATER, LFeatures)); - - return featureVector; - } - - - private List getCurrentStmts(Repair repair) { - List ret = new ArrayList<>(); - CtElement srcElem = repair.srcElem; - if (!repair.isReplace) { - ret.add(srcElem); - return ret; - } - else { - ret.add(srcElem); - CtStatement ElseB = null; - if (repair.dstElem instanceof CtIf) { - CtIf IFS = (CtIf) repair.dstElem; - if (IFS.getThenStatement() instanceof CtStatementList) { - CtStatementList CS = IFS.getThenStatement(); - ret.addAll(CS.getStatements()); - } - else - ret.add(IFS.getThenStatement()); - ElseB = IFS.getElseStatement(); - if (ElseB != null) { - if (ElseB instanceof CtStatementList) { - CtStatementList CS = IFS.getThenStatement(); - ret.addAll(CS.getStatements()); - } - else - ret.add(ElseB); - } - } - if (ElseB==null) { - CtElement parent = srcElem.getParent(); - if (parent instanceof CtStatementList) { - CtStatementList CS = (CtStatementList) parent; - boolean found = false; - for (CtStatement stmt : CS.getStatements()) { - if (found) { - ret.add(stmt); - break; - } - if (stmt.equals(srcElem)) - found = true; - } - } - } - return ret; - } - } - - private void getNearbyStmts(Repair repair, List stmtsF, List stmtsL) { - final int LOOKUP_DIS = 3; - CtElement srcElem = repair.srcElem; - CtElement parent = srcElem.getParent(); - if (parent instanceof CtStatementList) { - CtStatementList CS = (CtStatementList) parent; - List tmp = new ArrayList<>(); - int idx = 0; - for (CtStatement stmt: CS.getStatements()) { - if (stmt.equals(srcElem)) { - idx = tmp.size(); - } - tmp.add(stmt); - } - int s = 0; - if (idx > LOOKUP_DIS) - s = idx - LOOKUP_DIS; - int e = tmp.size(); - if (idx + LOOKUP_DIS + 1 < tmp.size()) - e = idx + LOOKUP_DIS + 1; - boolean above = true; - for (int i = s; i < e; i++) { - if (!tmp.get(i).equals(srcElem)) { - if (above) - stmtsF.add(tmp.get(i)); - else - stmtsL.add(tmp.get(i)); - } - if (tmp.get(i).equals(srcElem)) - above = false; - } - } - if (!repair.isReplace) - stmtsL.add(srcElem); - } -} \ No newline at end of file diff --git a/src/main/java/fr/inria/prophet4j/feature/original/OriginalRepairGenerator.java b/src/main/java/fr/inria/prophet4j/feature/original/OriginalRepairGenerator.java deleted file mode 100755 index bd006daf8..000000000 --- a/src/main/java/fr/inria/prophet4j/feature/original/OriginalRepairGenerator.java +++ /dev/null @@ -1,496 +0,0 @@ -package fr.inria.prophet4j.feature.original; - -import java.lang.reflect.Type; -import java.util.*; - -import fr.inria.prophet4j.utility.Structure.RepairKind; -import fr.inria.prophet4j.utility.Structure.Repair; -import fr.inria.prophet4j.utility.Structure.DiffEntry; -import fr.inria.prophet4j.feature.RepairGenerator; -import fr.inria.prophet4j.feature.original.util.OriginalRepairAnalyzer; -import spoon.Launcher; -import spoon.reflect.declaration.CtClass; -import spoon.reflect.declaration.CtElement; -import spoon.reflect.declaration.CtMethod; -import spoon.reflect.visitor.filter.TypeFilter; -import spoon.reflect.code.*; -import spoon.reflect.factory.CoreFactory; -import spoon.reflect.path.CtRole; -import spoon.reflect.visitor.CtScanner; - -// based on RepairGenerator.cpp -public class OriginalRepairGenerator implements RepairGenerator { - private Set area; // loc_map - private DiffEntry diffEntry; - private CoreFactory factory; - private List repairs = new ArrayList<>(); - private Map compound_counter = new HashMap<>(); - private OriginalRepairAnalyzer repairAnalyzer = new OriginalRepairAnalyzer(); - - public OriginalRepairGenerator(DiffEntry diffEntry) { - this.area = fuzzyLocator(diffEntry.srcNode); - this.diffEntry = diffEntry; - this.factory = new Launcher().getFactory().Core(); - this.repairs.clear(); - this.compound_counter.clear(); - } - - private boolean isTainted(CtStatement S) { - if (S == null) return false; - if (area.contains(S)) - return true; - // why Prophet does not need the second condition ? - if (S instanceof CtStatementList && compound_counter.containsKey(S)) { - CtStatementList CS = (CtStatementList) S; - return compound_counter.get(CS) >= 2 || (compound_counter.get(CS) == 1 && CS.getStatements().size() == 1); - } else { - return false; - } - } - - private void genTightCondition(CtIf n) { - CtExpression oldCondition = n.getCondition(); - CtLiteral placeholder = factory.createLiteral(); - placeholder.setValue(true); // consider the placeholder, should this be more concrete? - CtUnaryOperator tmpCondition = factory.createUnaryOperator(); - tmpCondition.setKind(UnaryOperatorKind.NOT); - tmpCondition.setOperand(placeholder); - CtBinaryOperator newCondition = factory.createBinaryOperator(); - newCondition.setKind(BinaryOperatorKind.AND); - newCondition.setLeftHandOperand(oldCondition); - newCondition.setRightHandOperand(placeholder); - - CtIf S = n.clone(); - S.setParent(n.getParent()); - S.setCondition(newCondition); - - Repair repair = new Repair(); - repair.kind = RepairKind.TightenConditionKind; - repair.isReplace = true; - repair.srcElem = n; - repair.dstElem = S; - repair.atoms.addAll(repairAnalyzer.getCondCandidateVars(n)); - repairs.add(repair); - // we do not consider the case of short-circuit evaluation at all - } - - private void genLooseCondition(CtIf n) { - CtExpression oldCondition = n.getCondition(); - CtLiteral placeholder = factory.createLiteral(); - placeholder.setValue(true); // consider the placeholder, should this be more concrete? - CtBinaryOperator newCondition = factory.createBinaryOperator(); - newCondition.setKind(BinaryOperatorKind.OR); - newCondition.setLeftHandOperand(oldCondition); - newCondition.setRightHandOperand(placeholder); - - CtIf S = n.clone(); - S.setParent(n.getParent()); - S.setCondition(newCondition); - - Repair repair = new Repair(); - repair.kind = RepairKind.LoosenConditionKind; - repair.isReplace = true; - repair.srcElem = n; - repair.dstElem = S; - repair.atoms.addAll(repairAnalyzer.getCondCandidateVars(n)); - repairs.add(repair); - // we do not consider the case of short-circuit evaluation at all - } - - private void genAddIfGuard(CtStatement n) { - CtLiteral placeholder = factory.createLiteral(); - placeholder.setValue(true); // consider the placeholder, should this be more concrete? - CtUnaryOperator guardCondition = factory.createUnaryOperator(); - guardCondition.setKind(UnaryOperatorKind.NOT); - guardCondition.setOperand(placeholder); - - CtIf guardIf = factory.createIf(); - guardIf.setParent(n.getParent()); - guardIf.setCondition(guardCondition); - guardIf.setThenStatement(n.clone()); - - Repair repair = new Repair(); - repair.kind = RepairKind.GuardKind; - repair.isReplace = true; - repair.srcElem = n; - repair.dstElem = guardIf; - repair.atoms.addAll(repairAnalyzer.getCondCandidateVars(n)); - repairs.add(repair); - // we do not consider the case of if statement as special at all - } - - private void genAddIfExit(CtStatement n) { - CtLiteral placeholder = factory.createLiteral(); - placeholder.setValue(true); // consider the placeholder, should this be more concrete? - Type returnType = void.class; - CtMethod curFD = repairAnalyzer.getCurrentFunction(n); - if (curFD != null) { - CtStatement lastStatement = curFD.getBody().getLastStatement(); -// CtReturn ctReturn = curFD.getBody().getLastStatement(); -// Type returnType = ctReturn.getClass().getGenericSuperclass(); - List ctReturns = lastStatement.getElements(new TypeFilter<>(CtReturn.class)); - if (ctReturns.size() > 0) { - returnType = ctReturns.get(0).getClass().getGenericSuperclass(); - } - } - if (returnType == void.class) { - CtLiteral returnValue = factory.createLiteral(); - returnValue.setValue(null); // is it equivalent to void ? - CtReturn RS = factory.createReturn(); - RS.setReturnedExpression(returnValue); - CtIf IFS = factory.createIf(); - IFS.setParent(n.getParent()); - IFS.setCondition(placeholder); - IFS.setThenStatement(RS); - Repair repair = new Repair(); - repair.kind = RepairKind.IfExitKind; - repair.isReplace = false; - repair.srcElem = n; - repair.dstElem = IFS; - repair.atoms = repairAnalyzer.getCondCandidateVars(n); - repairs.add(repair); - } - else { - List exprs = repairAnalyzer.getCandidateReturnExpr(n, returnType); - for (CtExpression placeholder2 : exprs) { - CtReturn RS = factory.createReturn(); - RS.setReturnedExpression(placeholder2); - CtIf IFS = factory.createIf(); - IFS.setParent(n.getParent()); - IFS.setCondition(placeholder); - IFS.setThenStatement(RS); - Repair repair = new Repair(); - repair.kind = RepairKind.IfExitKind; - repair.isReplace = false; - repair.srcElem = n; - repair.dstElem = IFS; - repair.atoms = repairAnalyzer.getCondCandidateVars(n); - repairs.add(repair); - } - } - if (repairAnalyzer.isInsideLoop(n)) { - CtBreak BS = factory.createBreak(); - CtIf IFS = factory.createIf(); - IFS.setParent(n.getParent()); - IFS.setCondition(placeholder); - IFS.setThenStatement(BS); - Repair repair = new Repair(); - repair.kind = RepairKind.IfExitKind; - repair.isReplace = false; - repair.srcElem = n; - repair.dstElem = IFS; - repair.atoms = repairAnalyzer.getCondCandidateVars(n); - repairs.add(repair); - } - } - - private void genReplaceStmt(CtStatement n) { - if (n instanceof CtExpression) { - OriginalRepairAnalyzer.AtomReplaceVisitor V = repairAnalyzer.newAtomReplaceVisitor(); - V.TraverseStmt(n); - for (CtElement it : V.getResult()) { - Repair repair = new Repair(); - repair.kind = RepairKind.ReplaceKind; - repair.isReplace = true; - repair.srcElem = n; - repair.dstElem = it; - repair.atoms.addAll(new ArrayList<>()); - repair.oldRExpr = V.getOldRExpr(it); - repair.newRExpr = V.getNewRExpr(it); - repairs.add(repair); - } - } - - // I do not know its meaning as CtLiteral is not CtStatement - if (n instanceof CtLiteral) { - if (((CtLiteral) n).getValue() instanceof String) { - CtLiteral placeholder = factory.createLiteral(); - placeholder.setValue(""); - Repair repair = new Repair(); - repair.kind = RepairKind.ReplaceStringKind; - repair.isReplace = true; - repair.srcElem = n; - repair.dstElem = placeholder; - repair.atoms.addAll(new ArrayList<>()); - repair.oldRExpr = n; - repair.newRExpr = null; - repairs.add(repair); - } - } - - if (n instanceof CtInvocation) { - for (CtInvocation it : repairAnalyzer.getCandidateCalleeFunction((CtInvocation) n)) { - Repair repair = new Repair(); - repair.kind = RepairKind.ReplaceKind; - repair.isReplace = true; - repair.srcElem = n; - repair.dstElem = it; - repair.atoms.addAll(new ArrayList<>()); - repair.oldRExpr = ((CtInvocation) n).getExecutable(); - repair.newRExpr = it; - repairs.add(repair); - } - } - } - - // isValidStmt() were commented as thought unnecessary - // also I just doubt the validity of this kind of repair - private void genAddStmt(CtStatement n) { - Set exprs = repairAnalyzer.getGlobalCandidateExprs(n); - for (CtElement it: exprs) { - OriginalRepairAnalyzer.AtomReplaceVisitor V = repairAnalyzer.newAtomReplaceVisitor(); - V.TraverseStmt(it); -// if (!repairAnalyzer.isValidStmt(it)) -// continue; - - for (CtElement it2 : V.getResult()) { -// boolean valid_after_replace = repairAnalyzer.isValidStmt(it2); -// if (!valid_after_replace) continue; - Repair repair = new Repair(); - repair.kind = RepairKind.AddAndReplaceKind; - repair.isReplace = false; - repair.srcElem = n; - repair.dstElem = it2; - repair.atoms.addAll(new ArrayList<>()); - repairs.add(repair); - } - Repair repair = new Repair(); - repair.kind = RepairKind.AddAndReplaceKind; - repair.isReplace = false; - repair.srcElem = n; - repair.dstElem = it; - repair.atoms.addAll(new ArrayList<>()); - repairs.add(repair); - } - - // insert if_stmt without atom replace if possible - Set stmts = repairAnalyzer.getGlobalCandidateIfStmts(n); - for (CtElement it : stmts) { -// boolean valid = repairAnalyzer.isValidStmt(it); -// if (!valid) continue; - Repair repair = new Repair(); - repair.kind = RepairKind.AddAndReplaceKind; - repair.isReplace = false; - repair.srcElem = n; - repair.dstElem = it; - repair.atoms.addAll(new ArrayList<>()); - repairs.add(repair); - } - } - - public Repair obtainHumanRepair() { - Repair repair = new Repair(); - repair.kind = null; // related to RepairFeature - repair.isReplace = false; - repair.srcElem = diffEntry.srcNode; - repair.dstElem = diffEntry.dstNode; - repair.atoms.addAll(new ArrayList<>()); - repair.oldRExpr = null; // related to SchemaFeature - repair.newRExpr = null; // related to SchemaFeature - - // todo improve - // based on matchCandidateWithHumanFix() - switch (diffEntry.type) { - case DeleteType: // only delete - repair.kind = RepairKind.RemoveSTMTKind; - if (diffEntry.srcNode instanceof CtIf) { - repair.kind = RepairKind.RemoveWholeIFKind; - } else if (diffEntry.srcNode instanceof CtBlock) { - repair.kind = RepairKind.RemoveWholeBlockKind; - } - break; - case PartialDeleteType: // delete + move kind - repair.kind = RepairKind.RemoveSTMTKind; - if (diffEntry.srcNode instanceof CtIf) { - repair.kind = RepairKind.RemovePartialIFKind; - } - break; - case InsertType: // kind - // IfExitKind: // INSERT_CONTROL_RF - // AddAndReplaceKind: // INSERT_STMT_RF - if (diffEntry.dstNode instanceof CtIf) { - repair.kind = RepairKind.IfExitKind; - } else { - repair.kind = RepairKind.AddAndReplaceKind; - } - // compare with others in obtainRepairCandidates() - break; - case UpdateType: // kind // oldRExpr // newRExpr - // IfExitKind: // INSERT_CONTROL_RF - // GuardKind: // INSERT_GUARD_RF - // SpecialGuardKind: // INSERT_GUARD_RF - // LoosenConditionKind: // REPLACE_COND_RF - // TightenConditionKind: // REPLACE_COND_RF - // ReplaceKind: // REPLACE_STMT_RF - // ReplaceStringKind: // REPLACE_STMT_RF - CtIf IF2; - if (diffEntry.dstNode instanceof CtIf) { - IF2 = (CtIf) diffEntry.dstNode; - } else { - IF2 = diffEntry.dstNode.getParent(new TypeFilter<>(CtIf.class)); - } - if (IF2 != null) { - CtIf IF1; - if (diffEntry.srcNode instanceof CtIf) { - IF1 = (CtIf) diffEntry.srcNode; - } else { - IF1 = diffEntry.srcNode.getParent(new TypeFilter<>(CtIf.class)); - } - if (IF1 != null) { - // make sure repair.kind would be assigned one value - repair.kind = RepairKind.SpecialGuardKind; - if (IF1.getThenStatement().equals(IF2.getThenStatement())) { - // LoosenConditionKind and TightenConditionKind are almost same as both are REPLACE_COND_RF - if (IF1.getElseStatement()!=null && IF2.getElseStatement()!=null) { - if (IF1.getElseStatement().equals(IF2.getElseStatement())) { - repair.kind = RepairKind.LoosenConditionKind; - } - } else { - repair.kind = RepairKind.LoosenConditionKind; - } - } - } else { - CtStatement S = IF2.getThenStatement(); - if (S instanceof CtCFlowBreak) { - repair.kind = RepairKind.IfExitKind; - } else { - repair.kind = RepairKind.GuardKind; - } - } - } else { - if (diffEntry.srcNode instanceof CtLiteral) { - repair.kind = RepairKind.ReplaceStringKind; - } else { - repair.kind = RepairKind.ReplaceKind; - } - } - repair.oldRExpr = diffEntry.srcNode; - repair.newRExpr = diffEntry.dstNode; -// if (repair.oldRExpr instanceof CtExpression) { -// if (!(repair.oldRExpr instanceof CtAnnotation || repair.oldRExpr instanceof CtImport)) { -// while (!(repair.oldRExpr instanceof CtStatement)){ -// repair.oldRExpr = repair.oldRExpr.getParent(); -// } -// } -// } -// if (repair.newRExpr instanceof CtExpression) { -// if (!(repair.newRExpr instanceof CtAnnotation || repair.newRExpr instanceof CtImport)) { -// while (!(repair.newRExpr instanceof CtStatement)){ -// repair.newRExpr = repair.newRExpr.getParent(); -// } -// } -// } - // compare with others in obtainRepairCandidates() - repair.isReplace = true; - break; - } - try { - List candidates = diffEntry.dstNode.getElements(new TypeFilter<>(CtElement.class)); - repair.atoms.addAll(candidates); - } catch (Exception e) { - // such as public, final, static - } - return repair; - } - - // https://people.csail.mit.edu/fanl/papers/spr-fse15.pdf <3.2 Transformation Schemas> Figure 4 - public List obtainRepairCandidates() { - CtScanner scanner = new CtScanner() { - - // https://clang.llvm.org/doxygen/classclang_1_1LabelStmt.html - private boolean isLabelStmt(CtStatement statement) { - return false; - } - - // todo check - // https://clang.llvm.org/doxygen/classclang_1_1DeclStmt.html - private boolean isDeclStmt(CtStatement statement) { - return statement instanceof CtIf || statement instanceof CtLoop || statement instanceof CtSwitch || statement instanceof CtAssignment; - } - - @Override - public void visitCtIf(CtIf n) { - // genTightCondition genLooseCondition - super.visitCtIf(n); - CtStatement ThenCS = n.getThenStatement(); - CtStatement ElseCS = n.getElseStatement(); - if (isTainted(n) || isTainted(ThenCS)) - genTightCondition(n); - if (isTainted(n) || isTainted(ElseCS)) - genLooseCondition(n); - } - - @Override - public void visitCtStatementList(CtStatementList n) { - super.visitCtStatementList(n); - compound_counter.put(n, 0); - for (CtStatement it : n) { - if (isTainted(it)) { - compound_counter.put(n, compound_counter.get(n) + 1); - } - } - } - - @Override - public void scan(CtRole role, CtElement element) { - super.scan(role, element); - if (element instanceof CtStatement && !(element instanceof CtStatementList)) { - CtStatement n = (CtStatement) element; - - if (isTainted(n)) { - // This is to compute whether Stmt n is the first - // non-decl statement in a CompoundStmt - genReplaceStmt(n); - // todo check - if (!isDeclStmt(n) && !isLabelStmt(n)) - genAddIfGuard(n); - genAddStmt(n); - genAddIfExit(n); - } - else if (n instanceof CtIf) { - CtIf IFS = (CtIf) n; - CtStatement thenBlock = IFS.getThenStatement(); - CtStatement firstS = thenBlock; - if (thenBlock instanceof CtStatementList) { - CtStatementList CS = (CtStatementList) thenBlock; - if (CS.getStatements().size() > 1) - firstS = CS.getStatements().get(0); - } - if (isTainted(thenBlock) || isTainted(firstS)) { - genAddStmt(n); - } - } - } - } - }; - // traverse (i.e. go to each node) the AST of clang::ASTContext (the top declaration context) - scanner.scan(diffEntry.srcNode); - return repairs; - } - - // based on LocationFuzzer class - private Set fuzzyLocator(CtElement statement) { - Set locations = new HashSet<>(); - if (statement instanceof CtMethod || statement instanceof CtClass || statement instanceof CtIf || statement instanceof CtStatementList) { - locations.add(statement); - } else { - // "int a;" is not CtStatement? - CtElement parent = statement.getParent(); - if (parent != null) { - List statements = parent.getElements(new TypeFilter<>(CtElement.class)); - if (parent instanceof CtStatement) { - statements = statements.subList(1, statements.size()); - } - int idx = statements.indexOf(statement); - if (idx >= 0) { - if (idx > 0) - locations.add(statements.get(idx - 1)); - locations.add(statements.get(idx)); - if (idx < statements.size() - 1) - locations.add(statements.get(idx + 1)); - } - } - } - return locations; - } -} \ No newline at end of file diff --git a/src/main/java/fr/inria/prophet4j/feature/original/util/OriginalFeatureVisitor.java b/src/main/java/fr/inria/prophet4j/feature/original/util/OriginalFeatureVisitor.java deleted file mode 100755 index ade6ba7ad..000000000 --- a/src/main/java/fr/inria/prophet4j/feature/original/util/OriginalFeatureVisitor.java +++ /dev/null @@ -1,316 +0,0 @@ -package fr.inria.prophet4j.feature.original.util; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import fr.inria.prophet4j.feature.original.OriginalFeature.AtomicFeature; -import fr.inria.prophet4j.utility.Structure.Repair; - -import spoon.reflect.code.CtArrayAccess; -import spoon.reflect.code.CtAssignment; -import spoon.reflect.code.CtBinaryOperator; -import spoon.reflect.code.CtCFlowBreak; -import spoon.reflect.code.CtExpression; -import spoon.reflect.code.CtIf; -import spoon.reflect.code.CtInvocation; -import spoon.reflect.code.CtLabelledFlowBreak; -import spoon.reflect.code.CtLiteral; -import spoon.reflect.code.CtLoop; -import spoon.reflect.code.CtOperatorAssignment; -import spoon.reflect.code.CtUnaryOperator; -import spoon.reflect.declaration.CtElement; -import spoon.reflect.declaration.CtField; -import spoon.reflect.reference.CtFieldReference; -import spoon.reflect.visitor.CtScanner; - -// based on FeatureExtract.cpp -public class OriginalFeatureVisitor { - boolean isReplace = false; - Map valueExprInfo; - Map> resMap = new HashMap<>(); - - public OriginalFeatureVisitor(Map valueExprInfo) { - this.valueExprInfo = valueExprInfo; - } - - private void putValueFeature(CtElement v, AtomicFeature af) { - if (v == null) { - if (!resMap.containsKey("@")) { - resMap.put("@", new HashSet<>()); - } - resMap.get("@").add(af); - } - else { -// CtExpression e = stripParenAndCast(v); -// std::string tmp = stmtToString(*ast, e); - String tmp = v.toString(); - // i can not know why there is one return here -// if (v instanceof CtAssignment) { -// return; -// } - // CtInvocation or CtExecutable todo check -// if (v.getElements(new TypeFilter<>(CtInvocation.class)).size() > 0 && !isAbstractStub(v)) { -// return; -// } - if (!resMap.containsKey(tmp)) { - resMap.put(tmp, new HashSet<>()); - } - resMap.get(tmp).add(af); - if (!valueExprInfo.containsKey(tmp)) { - valueExprInfo.put(tmp, v); - } - } - } - - public Map> traverseRepair(Repair repair, CtElement atom) { // traverseRC - if (atom != null) { - // for the return value of getCandidateAtoms() when null is not its only element - putValueFeature(atom, AtomicFeature.ABST_V_AF); - } - isReplace = repair.isReplace; - // meaningless todo check -// if (repair.kind == RepairKind.TightenConditionKind || -// repair.kind == RepairKind.LoosenConditionKind || -// repair.kind == RepairKind.GuardKind || -// repair.kind == RepairKind.SpecialGuardKind) { -// if (repair.actions.get(0).dstElem instanceof CtIf) { -// CtIf IFS = (CtIf) repair.actions.get(0).dstElem; -// putValueFeature(null, AtomicFeature.R_STMT_COND_AF); -// CtExpression cond = IFS.getCondition(); -// traverseStmt(cond); -// } -// } -// else { - return traverseStmt(repair.dstElem); -// } - } - - public Map> traverseStmt(CtElement S) { - CtScanner scanner = new CtScanner() { - @Override - public void scan(CtElement element) { // VisitExpr - super.scan(element); - if (element instanceof CtLoop || element instanceof CtExpression && element.getParent() instanceof CtLoop) { -// assert !isReplace; - putValueFeature(null, AtomicFeature.STMT_LOOP_AF); - putValueFeature(element, AtomicFeature.STMT_LOOP_AF); - } - if (element instanceof CtCFlowBreak || element instanceof CtExpression && element.getParent() instanceof CtCFlowBreak) { - if (element instanceof CtLabelledFlowBreak || element instanceof CtExpression && element.getParent() instanceof CtLabelledFlowBreak) { -// assert !isReplace; - putValueFeature(null, AtomicFeature.STMT_LABEL_AF); - putValueFeature(element, AtomicFeature.STMT_LABEL_AF); - } else { - putValueFeature(null, isReplace ? AtomicFeature.R_STMT_CONTROL_AF : AtomicFeature.STMT_CONTROL_AF); - putValueFeature(element, isReplace ? AtomicFeature.R_STMT_CONTROL_AF : AtomicFeature.STMT_CONTROL_AF); - } - } - if (element instanceof CtAssignment || element instanceof CtExpression && element.getParent() instanceof CtAssignment) { - putValueFeature(null, isReplace ? AtomicFeature.R_STMT_ASSIGN_AF : AtomicFeature.STMT_ASSIGN_AF); - putValueFeature(element, isReplace ? AtomicFeature.R_STMT_ASSIGN_AF : AtomicFeature.STMT_ASSIGN_AF); - } - if (element instanceof CtInvocation || element instanceof CtExpression && element.getParent() instanceof CtInvocation) { - putValueFeature(null, isReplace ? AtomicFeature.R_STMT_CALL_AF : AtomicFeature.STMT_CALL_AF); - putValueFeature(element, isReplace ? AtomicFeature.R_STMT_CALL_AF : AtomicFeature.STMT_CALL_AF); - } - if (element instanceof CtIf || element instanceof CtExpression && element.getParent() instanceof CtIf) { - putValueFeature(null, isReplace ? AtomicFeature.R_STMT_COND_AF : AtomicFeature.STMT_COND_AF); - putValueFeature(element, isReplace ? AtomicFeature.R_STMT_COND_AF : AtomicFeature.STMT_COND_AF); - } - } - - @Override - public void visitCtInvocation(CtInvocation invocation) { // VisitCallExpr - super.visitCtInvocation(invocation); - CtElement callee = invocation.getExecutable(); - putValueFeature(callee, AtomicFeature.CALLEE_AF); - for (CtExpression it : invocation.getArguments()) { - putValueFeature(it, AtomicFeature.CALL_ARGUMENT_AF); - } - } - - @Override - public void visitCtField(CtField f) { - super.visitCtField(f); -// if (f instanceof CtArrayAccess) -// putValueFeature(f, AtomicFeature.DEREF_AF); - putValueFeature(f, AtomicFeature.MEMBER_ACCESS_AF); - } - - @Override - public void visitCtFieldReference(CtFieldReference reference) { - super.visitCtFieldReference(reference); -// if (reference instanceof CtArrayAccess) -// putValueFeature(reference, AtomicFeature.DEREF_AF); - putValueFeature(reference, AtomicFeature.MEMBER_ACCESS_AF); - } - - @Override - public void visitCtAssignment(CtAssignment assignment) { - super.visitCtAssignment(assignment); - CtExpression LHS = assignment.getAssigned(); - CtExpression RHS = assignment.getAssignment(); - putValueFeature(LHS, AtomicFeature.ASSIGN_LHS_AF); - putValueFeature(LHS, AtomicFeature.CHANGED_AF); - if (RHS instanceof CtLiteral) { - Object v = ((CtLiteral)RHS).getValue(); - if (v instanceof Integer) { - if ((Integer) v == 0) { - putValueFeature(LHS, AtomicFeature.ASSIGN_ZERO_AF); - } - putValueFeature(LHS, AtomicFeature.ASSIGN_CONST_AF); - } - } - if (RHS instanceof CtArrayAccess) { - putValueFeature(LHS, AtomicFeature.DEREF_AF); - putValueFeature(RHS, AtomicFeature.INDEX_AF); - } - } - - @Override - public void visitCtOperatorAssignment(CtOperatorAssignment assignment) { - super.visitCtOperatorAssignment(assignment); - CtExpression LHS = assignment.getAssigned(); - CtExpression RHS = assignment.getAssignment(); - switch (assignment.getKind()) { - case PLUS: - putValueFeature(LHS, AtomicFeature.OP_ADD_AF); - putValueFeature(RHS, AtomicFeature.OP_ADD_AF); - break; - case MINUS: - putValueFeature(LHS, AtomicFeature.OP_SUB_AF); - putValueFeature(RHS, AtomicFeature.OP_SUB_AF); - break; - case MUL: - putValueFeature(LHS, AtomicFeature.OP_MUL_AF); - putValueFeature(RHS, AtomicFeature.OP_MUL_AF); - break; - case DIV: - putValueFeature(LHS, AtomicFeature.OP_DIV_AF); - putValueFeature(RHS, AtomicFeature.OP_DIV_AF); - break; - case MOD: - putValueFeature(LHS, AtomicFeature.OP_MOD_AF); - putValueFeature(RHS, AtomicFeature.OP_MOD_AF); - break; - } - putValueFeature(LHS, AtomicFeature.CHANGED_AF); - } - - @Override - public void visitCtBinaryOperator(CtBinaryOperator operator) { - super.visitCtBinaryOperator(operator); - CtExpression LHS = operator.getLeftHandOperand(); - CtExpression RHS = operator.getRightHandOperand(); - switch (operator.getKind()) { - case PLUS: - putValueFeature(LHS, AtomicFeature.OP_ADD_AF); - putValueFeature(RHS, AtomicFeature.OP_ADD_AF); - break; - case MINUS: - putValueFeature(LHS, AtomicFeature.OP_SUB_AF); - putValueFeature(RHS, AtomicFeature.OP_SUB_AF); - break; - case MUL: - putValueFeature(LHS, AtomicFeature.OP_MUL_AF); - putValueFeature(RHS, AtomicFeature.OP_MUL_AF); - break; - case DIV: - putValueFeature(LHS, AtomicFeature.OP_DIV_AF); - putValueFeature(RHS, AtomicFeature.OP_DIV_AF); - break; - case MOD: - putValueFeature(LHS, AtomicFeature.OP_MOD_AF); - putValueFeature(RHS, AtomicFeature.OP_MOD_AF); - break; - case LE: - putValueFeature(LHS, AtomicFeature.OP_LE_AF); - putValueFeature(RHS, AtomicFeature.OP_LE_AF); - break; - case LT: - putValueFeature(LHS, AtomicFeature.OP_LT_AF); - putValueFeature(RHS, AtomicFeature.OP_LT_AF); - break; - case GE: - putValueFeature(LHS, AtomicFeature.OP_GE_AF); - putValueFeature(RHS, AtomicFeature.OP_GE_AF); - break; - case GT: - putValueFeature(LHS, AtomicFeature.OP_GT_AF); - putValueFeature(RHS, AtomicFeature.OP_GT_AF); - break; - case EQ: - putValueFeature(LHS, AtomicFeature.OP_EQ_AF); - putValueFeature(RHS, AtomicFeature.OP_EQ_AF); - break; - case NE: - putValueFeature(LHS, AtomicFeature.OP_NE_AF); - putValueFeature(RHS, AtomicFeature.OP_NE_AF); - break; - } - } - - @Override - public void visitCtUnaryOperator(CtUnaryOperator operator) { - super.visitCtUnaryOperator(operator); - CtExpression operand = operator.getOperand(); - switch (operator.getKind()) { - case POS: - putValueFeature(operand, AtomicFeature.OP_ADD_AF); - break; - case NEG: - putValueFeature(operand, AtomicFeature.OP_SUB_AF); - break; - case PREINC: - case POSTINC: - putValueFeature(operand, AtomicFeature.UOP_INC_AF); - putValueFeature(operand, AtomicFeature.CHANGED_AF); - break; - case PREDEC: - case POSTDEC: - putValueFeature(operand, AtomicFeature.UOP_DEC_AF); - putValueFeature(operand, AtomicFeature.CHANGED_AF); - break; - } - } - }; - scanner.scan(S); - return getFeatureResult(); - } - - // i really do not know why we need to remove some atomic features - private Map> getFeatureResult() { - // meaningless todo check -// if (res.map.containsKey("@")) { -// Set tmp = res.map.get("@"); -// if (tmp.contains(AtomicFeature.STMT_LOOP_AF)) { -// tmp.remove(AtomicFeature.STMT_COND_AF); -// tmp.remove(AtomicFeature.STMT_ASSIGN_AF); -// tmp.remove(AtomicFeature.STMT_CALL_AF); -// tmp.remove(AtomicFeature.STMT_CONTROL_AF); -// tmp.remove(AtomicFeature.STMT_LABEL_AF); -// } -// else if (tmp.contains(AtomicFeature.STMT_COND_AF)) { -// tmp.remove(AtomicFeature.STMT_ASSIGN_AF); -// tmp.remove(AtomicFeature.STMT_CALL_AF); -// tmp.remove(AtomicFeature.STMT_CONTROL_AF); -// tmp.remove(AtomicFeature.STMT_LABEL_AF); -// } -// else if (tmp.contains(AtomicFeature.STMT_LABEL_AF)) { -// tmp.remove(AtomicFeature.STMT_CONTROL_AF); -// tmp.remove(AtomicFeature.STMT_ASSIGN_AF); -// tmp.remove(AtomicFeature.STMT_CALL_AF); -// } -// else if (tmp.contains(AtomicFeature.STMT_CONTROL_AF)) { -// tmp.remove(AtomicFeature.STMT_CALL_AF); -// tmp.remove(AtomicFeature.STMT_ASSIGN_AF); -// } -// else if (tmp.contains(AtomicFeature.STMT_ASSIGN_AF)) { -// tmp.remove(AtomicFeature.STMT_CALL_AF); -// } -// } - return resMap; - } -} \ No newline at end of file diff --git a/src/main/java/fr/inria/prophet4j/feature/original/util/OriginalRepairAnalyzer.java b/src/main/java/fr/inria/prophet4j/feature/original/util/OriginalRepairAnalyzer.java deleted file mode 100755 index e6d4187b1..000000000 --- a/src/main/java/fr/inria/prophet4j/feature/original/util/OriginalRepairAnalyzer.java +++ /dev/null @@ -1,177 +0,0 @@ -package fr.inria.prophet4j.feature.original.util; - -import spoon.reflect.code.*; -import spoon.reflect.declaration.*; -import spoon.reflect.visitor.filter.TypeFilter; - -import java.lang.reflect.Type; -import java.util.*; - -// based on LocalAnalyzer.cpp GlobalAnalyzer.cpp -public class OriginalRepairAnalyzer { - public List getCondCandidateVars(CtElement element) { - List ret = new ArrayList<>(); - // Global variables - CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class)); - if (ownedClass != null) { - ret.addAll(ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class))); - ret.addAll(ownedClass.getElements(new TypeFilter<>(CtArrayAccess.class))); - } - // Local variables - CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class)); - if (ownedMethod != null) { - ret.addAll(ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class))); - } - return ret; - } - - public boolean isInsideLoop(CtElement element) { - return element.getParent(new TypeFilter<>(CtLoop.class)) != null; - } - - public CtMethod getCurrentFunction(CtElement element) { - return element.getParent(new TypeFilter<>(CtMethod.class)); - } - - public List getCandidateConstantInType(CtElement element, Type type) { - List ret = new ArrayList<>(); - CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class)); - if (ownedClass != null) { - for (CtLiteral tmp : ownedClass.getElements(new TypeFilter<>(CtLiteral.class))) { - if (tmp.getClass().getGenericSuperclass() == type) { - ret.add(tmp); - } - } - for (CtVariableAccess tmp : ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class))) { - if (tmp.getClass().getGenericSuperclass() == type) { - ret.add(tmp); - } - } - } - return ret; - } - - public List getCandidateReturnExpr(CtElement element, Type type) { - List ret = new ArrayList<>(); - CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class)); - if (ownedClass != null) { - for (CtLiteral tmp : ownedClass.getElements(new TypeFilter<>(CtLiteral.class))) { - if (tmp.getClass().getGenericSuperclass() == type) { - ret.add(tmp); - } - } - for (CtVariableAccess tmp : ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class))) { - if (tmp.getClass().getGenericSuperclass() == type) { - ret.add(tmp); - } - } - } - return ret; - } - - public List getCandidateLocalVars(CtElement element, Type type) { - List ret = new ArrayList<>(); - CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class)); - if (ownedMethod != null) { - for (CtLocalVariable tmp : ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class))) { - if (tmp.getClass().getGenericSuperclass() == type) { - ret.add(tmp); - } - } - } - return ret; - } - - public Set getGlobalCandidateExprs(CtElement element) { - Set ret = new HashSet<>(); - CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class)); - if (ownedClass != null) { - ret.addAll(ownedClass.getElements(new TypeFilter<>(CtExpression.class))); - } - return ret; - } - - public Set getGlobalCandidateIfStmts(CtElement element) { - Set ret = new HashSet<>(); - CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class)); - if (ownedClass != null) { - ret.addAll(ownedClass.getElements(new TypeFilter<>(CtIf.class))); - } - return ret; - } - - public AtomReplaceVisitor newAtomReplaceVisitor() { - return new AtomReplaceVisitor(); - } - - public class AtomReplaceVisitor { // this class could be reduced as one method - Set res = new HashSet<>(); - Map> resRExpr = new HashMap<>(); - - // we implement one equivalent method instead of CtScanner - public void TraverseStmt(CtElement element) { - // PR spoon to support getting belonged CtEnum with one CtEnumValue -// List enumValues = element.getElements(new TypeFilter<>(CtEnumValue.class)); -// for (CtEnumValue enumValue : enumValues) { -// List exprs = L->getCandidateEnumConstant(enumValue); -// for (CtElement expr : exprs) { -// res.add(expr); -// resRExpr.put(expr, new HashMap.SimpleEntry<>(enumValue, expr)); -// } -// } - List binaryOperators = element.getElements(new TypeFilter<>(CtBinaryOperator.class)); - for (CtBinaryOperator binaryOperator : binaryOperators) { - CtExpression RHS = binaryOperator.getRightHandOperand(); - if (RHS instanceof CtLiteral || RHS instanceof CtVariableAccess) { - if (RHS.getClass().getGenericSuperclass().equals(Integer.class)) { - List exprs = getCandidateConstantInType(element, Integer.class); - for (CtElement expr : exprs) { - res.add(expr); - resRExpr.put(expr, new HashMap.SimpleEntry<>(RHS, expr)); - } - } - } else if (RHS instanceof CtLocalVariable) { - List exprs = getCandidateLocalVars(element, RHS.getClass().getGenericSuperclass()); - for (CtElement expr : exprs) { - res.add(expr); - resRExpr.put(expr, new HashMap.SimpleEntry<>(RHS, expr)); - } - } - } - } - - public Set getResult() { - return res; - } - - public CtElement getOldRExpr(CtElement S) { - return resRExpr.get(S).getKey(); - } - - public CtElement getNewRExpr(CtElement S) { - return resRExpr.get(S).getValue(); - } - } - - public List getCandidateCalleeFunction(CtInvocation CE) { - List ret = new ArrayList<>(); - - CtMethod ownedMethod = CE.getParent(new TypeFilter<>(CtMethod.class)); - if (ownedMethod != null) { - List invocations = ownedMethod.getElements(new TypeFilter<>(CtInvocation.class)); - for (CtInvocation invocation : invocations) { - if (CE == invocation) { - continue; - } - if (CE.getExecutable() != invocation.getExecutable()) { - continue; - } - if (CE.getArguments().size() != invocation.getArguments().size()) { - continue; - } - ret.add(invocation); - } - } - return ret; - } -} diff --git a/src/main/java/fr/inria/prophet4j/learner/FeatureLearner.java b/src/main/java/fr/inria/prophet4j/learner/FeatureLearner.java index 2d459338c..41eb06d19 100755 --- a/src/main/java/fr/inria/prophet4j/learner/FeatureLearner.java +++ b/src/main/java/fr/inria/prophet4j/learner/FeatureLearner.java @@ -6,7 +6,6 @@ import fr.inria.prophet4j.feature.S4R.S4RFeature; import fr.inria.prophet4j.feature.enhanced.EnhancedFeature; import fr.inria.prophet4j.feature.extended.ExtendedFeature; -import fr.inria.prophet4j.feature.original.OriginalFeature; import fr.inria.prophet4j.utility.Option; import fr.inria.prophet4j.utility.Support; import org.apache.logging.log4j.Level; @@ -47,9 +46,6 @@ private double[] newFeatureArray() { case EXTENDED: arraySize = ExtendedFeature.FEATURE_SIZE; break; - case ORIGINAL: - arraySize = OriginalFeature.FEATURE_SIZE; - break; case S4R: arraySize = S4RFeature.FEATURE_SIZE; break; diff --git a/src/main/java/fr/inria/prophet4j/learner/Tool.java b/src/main/java/fr/inria/prophet4j/learner/Tool.java index 3b09868fc..452810123 100644 --- a/src/main/java/fr/inria/prophet4j/learner/Tool.java +++ b/src/main/java/fr/inria/prophet4j/learner/Tool.java @@ -11,7 +11,6 @@ import fr.inria.prophet4j.utility.Option.FeatureOption; import fr.inria.prophet4j.utility.Option.RankingOption; import fr.inria.prophet4j.utility.Structure.ParameterVector; -import fr.inria.prophet4j.feature.original.OriginalFeatureCross; import fr.inria.prophet4j.utility.Support; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; @@ -50,10 +49,6 @@ private void genWeightsCSV() { for (int idx = 0; idx < parameterVector.size(); idx++) { FeatureCross featureCross; switch (option.featureOption) { - case ORIGINAL: - featureCross = new OriginalFeatureCross(idx); - header.add(featureCross.getFeatures().toString()); - break; case EXTENDED: featureCross = new ExtendedFeatureCross(idx); header.add(featureCross.getFeatures().toString()); @@ -95,10 +90,6 @@ public void genVectorsCSV(RankingOption rankingOption) { for (int idx = 0; idx < parameterVector.size(); idx++) { FeatureCross featureCross; switch (option.featureOption) { - case ORIGINAL: - featureCross = new OriginalFeatureCross(idx); - header.add(featureCross.getFeatures().toString()); - break; case EXTENDED: featureCross = new ExtendedFeatureCross(idx); header.add(featureCross.getFeatures().toString()); diff --git a/src/main/java/fr/inria/prophet4j/utility/CodeDiffer.java b/src/main/java/fr/inria/prophet4j/utility/CodeDiffer.java index dec931ad1..7840cd4f1 100755 --- a/src/main/java/fr/inria/prophet4j/utility/CodeDiffer.java +++ b/src/main/java/fr/inria/prophet4j/utility/CodeDiffer.java @@ -19,8 +19,6 @@ import fr.inria.prophet4j.feature.enhanced.EnhancedFeatureExtractor; import fr.inria.prophet4j.feature.extended.ExtendedFeatureExtractor; import fr.inria.prophet4j.feature.extended.ExtendedRepairGenerator; -import fr.inria.prophet4j.feature.original.OriginalFeatureExtractor; -import fr.inria.prophet4j.feature.original.OriginalRepairGenerator; import fr.inria.prophet4j.utility.Option.FeatureOption; import gumtree.spoon.AstComparator; import gumtree.spoon.diff.Diff; @@ -76,9 +74,6 @@ private FeatureExtractor newFeatureExtractor() { case EXTENDED: featureExtractor = new ExtendedFeatureExtractor(); break; - case ORIGINAL: - featureExtractor = new OriginalFeatureExtractor(); - break; case S4R: logger.warn("S4R should not call newFeatureExtractor"); break; @@ -96,9 +91,6 @@ private RepairGenerator newRepairGenerator(DiffEntry diffEntry) { case EXTENDED: repairGenerator = new ExtendedRepairGenerator(diffEntry); break; - case ORIGINAL: - repairGenerator = new OriginalRepairGenerator(diffEntry); - break; case S4R: throw new RuntimeException("S4R should not call newRepairGenerator"); case S4RO: diff --git a/src/main/java/fr/inria/prophet4j/utility/Structure.java b/src/main/java/fr/inria/prophet4j/utility/Structure.java index 26b3dede9..100f61aa7 100644 --- a/src/main/java/fr/inria/prophet4j/utility/Structure.java +++ b/src/main/java/fr/inria/prophet4j/utility/Structure.java @@ -6,7 +6,6 @@ import fr.inria.prophet4j.feature.S4R.S4RFeature; import fr.inria.prophet4j.feature.enhanced.EnhancedFeature; import fr.inria.prophet4j.feature.extended.ExtendedFeature; -import fr.inria.prophet4j.feature.original.OriginalFeature; import fr.inria.prophet4j.utility.Option.FeatureOption; import org.apache.commons.io.FileUtils; import spoon.reflect.declaration.CtElement; @@ -86,9 +85,6 @@ public void saveAsJson(FeatureOption featureOption) { case EXTENDED: arraySize = ExtendedFeature.FEATURE_SIZE; break; - case ORIGINAL: - arraySize = OriginalFeature.FEATURE_SIZE; - break; case S4R: arraySize = S4RFeature.FEATURE_SIZE; break; @@ -242,9 +238,6 @@ public ParameterVector(FeatureOption featureOption) { case EXTENDED: this.arraySize = ExtendedFeature.FEATURE_SIZE; break; - case ORIGINAL: - this.arraySize = OriginalFeature.FEATURE_SIZE; - break; case S4R: this.arraySize = S4RFeature.FEATURE_SIZE; break; diff --git a/src/test/java/fr/inria/prophet4j/OriginalFeatureExtractorTest.java b/src/test/java/fr/inria/prophet4j/OriginalFeatureExtractorTest.java deleted file mode 100755 index c0c62e547..000000000 --- a/src/test/java/fr/inria/prophet4j/OriginalFeatureExtractorTest.java +++ /dev/null @@ -1,386 +0,0 @@ -package fr.inria.prophet4j; - -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import fr.inria.prophet4j.utility.Option; -import fr.inria.prophet4j.utility.Option.FeatureOption; -import org.junit.Ignore; -import org.junit.Test; - -import fr.inria.prophet4j.feature.Feature; -import fr.inria.prophet4j.utility.Structure.FeatureMatrix; -import fr.inria.prophet4j.feature.original.OriginalFeature.AtomicFeature; -import fr.inria.prophet4j.feature.original.OriginalFeature.RepairFeature; -import fr.inria.prophet4j.feature.original.OriginalFeature.ValueFeature; -import fr.inria.prophet4j.utility.CodeDiffer; - -// for OriginalFeatures -public class OriginalFeatureExtractorTest { - private void test(Feature caseFeature, Feature checkFeature) { - String str0, str1; - if (caseFeature instanceof AtomicFeature) { - AtomicFeature atomicFeature = (AtomicFeature) caseFeature; - switch (atomicFeature) { - case OP_ADD_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=+1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na=1+1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na+=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_SUB_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=-1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na=1-1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na-=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_MUL_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=1*1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na*=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_DIV_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=1/1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na/=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_MOD_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=1%1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na%=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_LE_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=1<=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_LT_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=1<1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_GE_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=1>=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_GT_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=1>1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_EQ_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=1==1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case OP_NE_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=1!=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case UOP_INC_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\n++a;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na++;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case UOP_DEC_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\n--a;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nint a=1;\na--;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case ASSIGN_LHS_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=0;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case ASSIGN_ZERO_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=0;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case ASSIGN_CONST_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=0;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case CHANGED_AF: - test(AtomicFeature.OP_ADD_AF, checkFeature); - test(AtomicFeature.OP_SUB_AF, checkFeature); - test(AtomicFeature.OP_MUL_AF, checkFeature); - test(AtomicFeature.OP_DIV_AF, checkFeature); - test(AtomicFeature.OP_MOD_AF, checkFeature); - test(AtomicFeature.UOP_INC_AF, checkFeature); - test(AtomicFeature.UOP_DEC_AF, checkFeature); - test(AtomicFeature.ASSIGN_LHS_AF, checkFeature); - test(AtomicFeature.ASSIGN_ZERO_AF, checkFeature); - test(AtomicFeature.ASSIGN_CONST_AF, checkFeature); - break; - case DEREF_AF: // these is another uncompleted case of DEREF_TF - str0 = "class Foo{public void bar(){\nint[] a={1};\nint b=0;\n}}"; - str1 = "class Foo{public void bar(){\nint[] a={1};\nint b=0;\nb=a[0];\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case INDEX_AF: - str0 = "class Foo{public void bar(){\nint[] a={1};\nint b=0;\n}}"; - str1 = "class Foo{public void bar(){\nint[] a={1};\nint b=0;\nb=a[0];\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case MEMBER_ACCESS_AF: - str0 = "class Foo{public void bar(){\nFoo x=new Foo();\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nFoo x=new Foo();\nint a=x.a;\n}\nint a;}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case CALLEE_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\nMath.abs(a);\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case CALL_ARGUMENT_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\nMath.abs(a);\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case ABST_V_AF: // i do not know the meaning of this util - test(AtomicFeature.OP_LE_AF, checkFeature); - test(AtomicFeature.OP_LT_AF, checkFeature); - test(AtomicFeature.OP_GE_AF, checkFeature); - test(AtomicFeature.OP_GT_AF, checkFeature); - test(AtomicFeature.OP_EQ_AF, checkFeature); - test(AtomicFeature.OP_NE_AF, checkFeature); - test(AtomicFeature.CHANGED_AF, checkFeature); - test(AtomicFeature.DEREF_AF, checkFeature); - test(AtomicFeature.INDEX_AF, checkFeature); - test(AtomicFeature.MEMBER_ACCESS_AF, checkFeature); - test(AtomicFeature.CALLEE_AF, checkFeature); - test(AtomicFeature.CALL_ARGUMENT_AF, checkFeature); - break; - case STMT_LABEL_AF: - str1 = "class Foo{public void bar(){\nboolean a=true;\nwhile(a){a=false;}\n}}"; - str0 = "class Foo{public void bar(){\nboolean a=true;\nwhile(a){break;}}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str1 = "class Foo{public void bar(){\nboolean a=true;\nwhile(a){a=false;}\n}}"; - str0 = "class Foo{public void bar(){\nboolean a=true;\nwhile(a){continue;}}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case STMT_LOOP_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\ndo{}while(a)\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nfor(;a;){}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nwhile(a){}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case STMT_ASSIGN_AF: - str0 = "class Foo{public void bar(){\nint a=1;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=0;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case STMT_CALL_AF: - str0 = "class Foo{public void bar(){\nint a=1;\nMath.abs(a);\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\nMath.exp(a);\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case STMT_COND_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a){}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case STMT_CONTROL_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\nreturn;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=false;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case R_STMT_ASSIGN_AF: - str0 = "class Foo{public void bar(){\nint a=1;\na=1\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\na=0;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case R_STMT_CALL_AF: - str0 = "class Foo{public void bar(){\nint a=1;\nMath.abs(a);\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\nMath.exp(a);\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case R_STMT_COND_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a&&true){}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a||true){}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case R_STMT_CONTROL_AF: - str0 = "class Foo{public void bar(){\nboolean a=true;\nreturn 0;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nreturn 1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; -// case ADDRESS_OF_AF: // Inapplicable to Java -// break; - } - } - if (caseFeature instanceof RepairFeature) { - RepairFeature repairFeature = (RepairFeature) caseFeature; - switch (repairFeature) { - case INSERT_CONTROL_RF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a){}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case INSERT_GUARD_RF: - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a){return;}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a){System.exit(0)}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case INSERT_STMT_RF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\na=false;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case REPLACE_COND_RF: - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a&&true){}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a||true){}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case REPLACE_STMT_RF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=false;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case REMOVE_WHOLE_IF: - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\n int b=1;\n}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case REMOVE_PARTIAL_IF: - //only remove the if condition and keep the statements in the condition - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\nint b=1;\n}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nint b=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - //remove the if condition and part of statements inside it and keep some statements in the condition - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\nint b=1;\nint c=0;\n}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nint b=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case REMOVE_STMT: - //remove a statement - str0 = "class Foo{public void bar(){\nboolean a=true;\nboolean b=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - //remove a statement from else block - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\nint b=1;\n}\nelse{int b=2;\nint c=3;\n}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\nint b=1;\n}\nelse{int b=2;\n}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case REMOVE_WHOLE_BLOCK: - //remove the whole else block - str0 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\nint b=1;\n}\nelse{int b=2;}\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=true;\nif(a){\nint b=1;\n}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - - } - } - if (caseFeature instanceof ValueFeature) { - ValueFeature valueFeature = (ValueFeature) caseFeature; - switch (valueFeature) { - case MODIFIED_VF: - str0 = "class Foo{public void bar(){\nboolean a=true;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=false;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case MODIFIED_SIMILAR_VF: - str0 = "class Foo{public void bar(){\nboolean a=10000;\n}}"; - str1 = "class Foo{public void bar(){\nboolean a=10000*1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case FUNC_ARGUMENT_VF: - str0 = "class Foo{public void bar(int x){\nint a=0;\n}}"; - str1 = "class Foo{public void bar(int x){\nint a=x;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case MEMBER_VF: - test(AtomicFeature.MEMBER_ACCESS_AF, checkFeature); - break; - case LOCAL_VARIABLE_VF: - str0 = "class foo{public void bar(){\nint a=1;\nif(true){a=0;}\n}}"; - str1 = "class foo{public void bar(){\nint a=1;\nif(true){int b=1;}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case GLOBAL_VARIABLE_VF: - str0 = "class foo{public void bar(){\nint a=1;\nif(true){int b=1;}\n}}"; - str1 = "class foo{public void bar(){\nint a=1;\nif(true){a=0;}\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case ZERO_CONST_VF: - str0 = "class Foo{public void bar(){\nint a;\n}}"; - str1 = "class Foo{public void bar(){\nint a=0;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case NONZERO_CONST_VF: - str0 = "class Foo{public void bar(){\nint a;\n}}"; - str1 = "class Foo{public void bar(){\nint a=1;\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case STRING_LITERAL_VF: - str0 = "class Foo{public void bar(){\nString a;\n}}"; - str1 = "class Foo{public void bar(){\nString a=\"a\";\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; - case SIZE_LITERAL_VF: - str0 = "class Foo{public void bar(){\nString a=\"a\";\n}}"; - str1 = "class Foo{public void bar(){\nString a=\"a\";\nint b=a.length()\n}}"; - assertEquals(Boolean.TRUE, check(str0, str1, checkFeature)); - break; -// case POINTER_VF: // Inapplicable to Java -// break; -// case STRUCT_POINTER_VF: // Inapplicable to Java -// break; - } - } - } - - private boolean check(String str0, String str1, Feature feature) { - Option option = new Option(); - option.featureOption = FeatureOption.ORIGINAL; - CodeDiffer codeDiffer = new CodeDiffer(false, option); - List featureMatrices = codeDiffer.runByGenerator(str0, str1); - for (FeatureMatrix featureMatrix : featureMatrices) { - if (featureMatrix.containFeature(feature)) { - return true; - } - } - return false; - } - - @Test - @Ignore // Gumtree3 has changed some features - // note that we maintain ExtendedFeatureExtractor in priority, for which the test case is still executed - public void testFeatureExtractor() { - for (AtomicFeature atomicFeature : AtomicFeature.values()) { - test(atomicFeature, atomicFeature); - } - for (RepairFeature repairFeature : RepairFeature.values()) { - test(repairFeature, repairFeature); - } - for (ValueFeature valueFeature : ValueFeature.values()) { - test(valueFeature, valueFeature); - } - } -}