From 6aa307f655012b868d3186d81dd2e79aafda3209 Mon Sep 17 00:00:00 2001 From: khashayar etemadi Date: Thu, 24 Oct 2019 12:28:15 +0200 Subject: [PATCH] GumtreeHelper refactored --- .../DetectorChangePatternInstanceEngine.java | 6 +- .../JSONRepairabilityOutput.java | 16 +++- .../repairability/repairtools/Cardumen.java | 6 +- ...lper.java => EntityTypesInfoResolver.java} | 78 ++++++------------- 4 files changed, 43 insertions(+), 63 deletions(-) rename src/main/java/fr/inria/coming/utils/{GumtreeHelper.java => EntityTypesInfoResolver.java} (58%) diff --git a/src/main/java/fr/inria/coming/changeminer/analyzer/instancedetector/DetectorChangePatternInstanceEngine.java b/src/main/java/fr/inria/coming/changeminer/analyzer/instancedetector/DetectorChangePatternInstanceEngine.java index 8632f5dcb..76c2243b5 100644 --- a/src/main/java/fr/inria/coming/changeminer/analyzer/instancedetector/DetectorChangePatternInstanceEngine.java +++ b/src/main/java/fr/inria/coming/changeminer/analyzer/instancedetector/DetectorChangePatternInstanceEngine.java @@ -8,7 +8,7 @@ import java.util.Set; import java.util.stream.Collectors; -import fr.inria.coming.utils.GumtreeHelper; +import fr.inria.coming.utils.EntityTypesInfoResolver; import org.apache.log4j.Logger; import com.github.gumtreediff.actions.model.Action; @@ -450,7 +450,7 @@ private List matchElements(Operation affectedOperation, PatternE int i_levels = 1; // Scale the parent hierarchy and check types. while (currentNodeFromAction != null && i_levels <= parentLevel) { - String typeOfNode = GumtreeHelper.getNodeLabelFromCtElement(currentNodeFromAction); + String typeOfNode = EntityTypesInfoResolver.getNodeLabelFromCtElement(currentNodeFromAction); String valueOfNode = currentNodeFromAction.toString(); String roleInParent = (currentNodeFromAction.getRoleInParent() != null) ? currentNodeFromAction.getRoleInParent().toString().toLowerCase() @@ -461,7 +461,7 @@ private List matchElements(Operation affectedOperation, PatternE ("*".equals(parentEntity.getEntityType()) // || (typeOfNode != null && typeOfNode.equals(parentEntity.getEntityType()))) || (typeOfNode != null && - GumtreeHelper.getInstance().isAChildOf(typeOfNode, parentEntity.getEntityType()))) + EntityTypesInfoResolver.getInstance().isAChildOf(typeOfNode, parentEntity.getEntityType()))) /// && // value of element diff --git a/src/main/java/fr/inria/coming/repairability/JSONRepairabilityOutput.java b/src/main/java/fr/inria/coming/repairability/JSONRepairabilityOutput.java index 6c2d7a52c..ea58c8bf4 100644 --- a/src/main/java/fr/inria/coming/repairability/JSONRepairabilityOutput.java +++ b/src/main/java/fr/inria/coming/repairability/JSONRepairabilityOutput.java @@ -13,7 +13,6 @@ import fr.inria.coming.core.entities.output.JSonPatternInstanceOutput; import fr.inria.coming.main.ComingProperties; import fr.inria.coming.repairability.models.InstanceStats; -import fr.inria.coming.utils.GumtreeHelper; import gumtree.spoon.diff.Diff; import gumtree.spoon.diff.operations.Operation; import org.apache.log4j.Logger; @@ -100,7 +99,7 @@ public void getInstancesOfRevision(RevisionResult revisionResult, JsonArray inst } if(isRootOperation(op, diff)){ - InstanceStats instanceStats = GumtreeHelper.getStats(op); + InstanceStats instanceStats = getOperationStats(op); opjson.add("stats", getJSONFromInstanceStats(instanceStats)); } ops.add(opjson); @@ -125,4 +124,17 @@ private boolean isRootOperation(Operation op, Diff diff) { } return false; } + + private InstanceStats getOperationStats(Operation operation) { + InstanceStats stats = new InstanceStats(); + if (operation.getSrcNode() != null) { + stats.setSrcEntityTypes(operation.getSrcNode().getReferencedTypes()); + stats.setNumberOfSrcEntities(operation.getSrcNode().getElements(null).size()); + } + if (operation.getDstNode() != null) { + stats.setDstEntityTypes(operation.getDstNode().getReferencedTypes()); + stats.setNumberOfDstEntities(operation.getDstNode().getElements(null).size()); + } + return stats; + } } diff --git a/src/main/java/fr/inria/coming/repairability/repairtools/Cardumen.java b/src/main/java/fr/inria/coming/repairability/repairtools/Cardumen.java index 7935ab888..9cd8e9663 100644 --- a/src/main/java/fr/inria/coming/repairability/repairtools/Cardumen.java +++ b/src/main/java/fr/inria/coming/repairability/repairtools/Cardumen.java @@ -4,7 +4,7 @@ import fr.inria.coming.changeminer.analyzer.patternspecification.ChangePatternSpecification; import fr.inria.coming.changeminer.entity.IRevision; import fr.inria.coming.changeminer.util.PatternXMLParser; -import fr.inria.coming.utils.GumtreeHelper; +import fr.inria.coming.utils.EntityTypesInfoResolver; import gumtree.spoon.diff.Diff; import gumtree.spoon.diff.operations.Operation; import spoon.reflect.code.*; @@ -87,8 +87,8 @@ public boolean filter(ChangePatternInstance instance, IRevision revision) { for (int i = 0; i < allSrcElements.size() - allDstElements.size() + 1; i++) { CtElement currentSrcElement = allSrcElements.get(i); - String typeOfCurrentSrcElement = GumtreeHelper.getNodeLabelFromCtElement(currentSrcElement); - if (!GumtreeHelper.getInstance().isAChildOf(typeOfCurrentSrcElement, "Expression")) { + String typeOfCurrentSrcElement = EntityTypesInfoResolver.getNodeLabelFromCtElement(currentSrcElement); + if (!EntityTypesInfoResolver.getInstance().isAChildOf(typeOfCurrentSrcElement, "Expression")) { continue; } String srcAsString = currentSrcElement.toString(); diff --git a/src/main/java/fr/inria/coming/utils/GumtreeHelper.java b/src/main/java/fr/inria/coming/utils/EntityTypesInfoResolver.java similarity index 58% rename from src/main/java/fr/inria/coming/utils/GumtreeHelper.java rename to src/main/java/fr/inria/coming/utils/EntityTypesInfoResolver.java index 3c082804a..6ba3309e6 100644 --- a/src/main/java/fr/inria/coming/utils/GumtreeHelper.java +++ b/src/main/java/fr/inria/coming/utils/EntityTypesInfoResolver.java @@ -1,7 +1,5 @@ package fr.inria.coming.utils; -import fr.inria.coming.repairability.models.InstanceStats; -import gumtree.spoon.diff.operations.Operation; import org.reflections.Reflections; import spoon.reflect.declaration.CtElement; @@ -13,35 +11,34 @@ /** * Created by khesoem on 10/4/2019. */ -public class GumtreeHelper { - private static GumtreeHelper _instance = null; +public class EntityTypesInfoResolver { + private static EntityTypesInfoResolver _instance = null; private static final String CLASSES_HIERARCHY_PATH = "src/main/resources/gumtree-inheritance-relations.txt"; - private Map> childrenToParents; + private Map> childrenToParentsRelationsBetweenEntityTypes; - public static GumtreeHelper getInstance(){ - if(_instance == null) - _instance = new GumtreeHelper(); + public static EntityTypesInfoResolver getInstance() { + if (_instance == null) + _instance = new EntityTypesInfoResolver(); return _instance; } - public GumtreeHelper(){ - loadClassToParents(); + public EntityTypesInfoResolver() { + loadChildrenToParentsRelationsBetweenEntityTypes(); } - private void loadClassToParents() { - childrenToParents = new HashMap<>(); - + private void loadChildrenToParentsRelationsBetweenEntityTypes() { + childrenToParentsRelationsBetweenEntityTypes = new HashMap<>(); try { Scanner sc = new Scanner(new File(CLASSES_HIERARCHY_PATH)); - while(sc.hasNextLine()){ + while (sc.hasNextLine()) { String line = sc.nextLine(); String[] parts = line.split(":"); String child = parts[0]; String[] parents = parts[1].split(" "); - childrenToParents.put(child, new HashSet<>(Arrays.asList(parents))); + childrenToParentsRelationsBetweenEntityTypes.put(child, new HashSet<>(Arrays.asList(parents))); } sc.close(); @@ -50,11 +47,11 @@ private void loadClassToParents() { } } - // says whether parent is an ancestor of or equal to child. - public boolean isAChildOf(String child, String parent){ - if(!childrenToParents.containsKey(child)) + // checks whether parent is an ancestor of or equal to child. + public boolean isAChildOf(String childEntityTypeName, String parentEntityTypeName) { + if (!childrenToParentsRelationsBetweenEntityTypes.containsKey(childEntityTypeName)) return false; - return childrenToParents.get(child).contains(parent); + return childrenToParentsRelationsBetweenEntityTypes.get(childEntityTypeName).contains(parentEntityTypeName); } // public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException { @@ -74,8 +71,8 @@ private static void extractAndSaveCtElementsHierarchyModel(String outputPath) th // initializing childrenToParents Set toBeIgnored = new HashSet<>(); - for(Class clazz : allClasses){ - if(!clazz.getSimpleName().startsWith("Ct")) { + for (Class clazz : allClasses) { + if (!clazz.getSimpleName().startsWith("Ct")) { toBeIgnored.add(clazz); continue; } @@ -83,11 +80,11 @@ private static void extractAndSaveCtElementsHierarchyModel(String outputPath) th } allClasses.removeAll(toBeIgnored); - for(Class clazz : allClasses){ + for (Class clazz : allClasses) { String currentClassName = clazz.getSimpleName().substring(2); Set> childrenOfCurrentClass = reflections.getSubTypesOf(clazz); - for(Class childOfCurrentClass : childrenOfCurrentClass){ - if(!childOfCurrentClass.getSimpleName().startsWith("Ct")) + for (Class childOfCurrentClass : childrenOfCurrentClass) { + if (!childOfCurrentClass.getSimpleName().startsWith("Ct")) continue; String currentChildName = childOfCurrentClass.getSimpleName().substring(2); childrenToParents.get(currentChildName).add(currentClassName); @@ -95,11 +92,11 @@ private static void extractAndSaveCtElementsHierarchyModel(String outputPath) th childrenToParents.get(currentClassName).add(currentClassName); // each class is considered as an ancestor of itself } - for(Map.Entry childToParents : childrenToParents.entrySet()){ + for (Map.Entry childToParents : childrenToParents.entrySet()) { String className = childToParents.getKey(); pw.print(className + ":"); Set parents = childToParents.getValue(); - for(String parent : parents){ + for (String parent : parents) { pw.print(" " + parent); } pw.print("\n"); @@ -109,35 +106,6 @@ private static void extractAndSaveCtElementsHierarchyModel(String outputPath) th pw.close(); } - public static InstanceStats getStats(Operation operation) { - InstanceStats stats = new InstanceStats(); - if(operation.getSrcNode() != null) { - stats.setSrcEntityTypes(operation.getSrcNode().getReferencedTypes()); - try { // FIXME: exception should not be thrown - stats.setNumberOfSrcEntities(operation.getSrcNode().getElements(null).size()); - }catch (Exception e){ -// e.printStackTrace(); - } - } - if(operation.getDstNode() != null) { - stats.setDstEntityTypes(operation.getDstNode().getReferencedTypes()); - try { // FIXME: exception should not be thrown - stats.setNumberOfDstEntities(operation.getDstNode().getElements(null).size()); - }catch (Exception e){ -// e.printStackTrace(); - } - } - return stats; - } - - public Map> getChildrenToParents() { - return childrenToParents; - } - - public void setChildrenToParents(Map> childrenToParents) { - this.childrenToParents = childrenToParents; - } - /** * The label of a CtElement is the simple name of the class without the CT * prefix.