Skip to content

Commit

Permalink
Handle matching of empty classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Dec 8, 2024
1 parent 65203a3 commit f23d1aa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/gr/uom/java/xmi/UMLModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public UMLModelDiff diff(UMLModel umlModel) throws RefactoringMinerTimedOutExcep
modelDiff.checkForMovedClasses(umlModel.repositoryDirectories, new UMLClassMatcher.RelaxedMove());
modelDiff.checkForRenamedClasses(new UMLClassMatcher.RelaxedRename());
modelDiff.inferClassRenameBasedOnFilePaths(new UMLClassMatcher.RelaxedRename());
modelDiff.inferClassRenameBasedOnReferencesInStringLiterals();
return modelDiff;
}
}
69 changes: 69 additions & 0 deletions src/main/java/gr/uom/java/xmi/diff/UMLModelDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,75 @@ public void inferClassRenameBasedOnFilePaths(UMLClassMatcher matcher) throws Ref
addedClasses.removeAll(addedClassesToBeRemoved);
}

public void inferClassRenameBasedOnReferencesInStringLiterals() throws RefactoringMinerTimedOutException {
Map<String, UMLClass> removedClassNameMap = new LinkedHashMap<>();
for(UMLClass removedClass : removedClasses) {
removedClassNameMap.put(removedClass.getNonQualifiedName(), removedClass);
}
Map<String, UMLClass> addedClassNameMap = new LinkedHashMap<>();
for(UMLClass addedClass : addedClasses) {
addedClassNameMap.put(addedClass.getNonQualifiedName(), addedClass);
}
Set<String> removedClassNames = removedClassNameMap.keySet();
Set<String> addedClassNames = addedClassNameMap.keySet();
Map<Pair<String, String>, Integer> countMap = new LinkedHashMap<>();
for(UMLClassDiff classDiff : commonClassDiffList) {
for(UMLOperationBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
for(Replacement r : mapper.getReplacements()) {
String matchingClassNameBefore = matches(r.getBefore(), removedClassNames);
String matchingClassNameAfter = matches(r.getAfter(), addedClassNames);
if(matchingClassNameBefore != null && matchingClassNameAfter != null) {
Pair<String, String> pair = Pair.of(matchingClassNameBefore, matchingClassNameAfter);
if(countMap.containsKey(pair)) {
countMap.put(pair, countMap.get(pair) + 1);
}
else {
countMap.put(pair, 1);
}
}
}
}
}
Set<UMLClassRenameDiff> diffsToBeAdded = new LinkedHashSet<UMLClassRenameDiff>();
Set<UMLClass> addedClassesToBeRemoved = new LinkedHashSet<UMLClass>();
Set<UMLClass> removedClassesToBeRemoved = new LinkedHashSet<UMLClass>();
UMLClassMatcher matcher = new UMLClassMatcher.RelaxedRename();
for(Pair<String, String> pair : countMap.keySet()) {
if(!conflictingPair(pair, countMap.keySet())) {
UMLClass removedClass = removedClassNameMap.get(pair.getLeft());
UMLClass addedClass = addedClassNameMap.get(pair.getRight());
MatchResult matchResult = matcher.match(removedClass, addedClass);
UMLClassRenameDiff newClassRenameDiff = new UMLClassRenameDiff(removedClass, addedClass, this, matchResult);
newClassRenameDiff.process();
diffsToBeAdded.add(newClassRenameDiff);
addedClassesToBeRemoved.add(addedClass);
removedClassesToBeRemoved.add(removedClass);
}
}
classRenameDiffList.addAll(diffsToBeAdded);
removedClasses.removeAll(removedClassesToBeRemoved);
addedClasses.removeAll(addedClassesToBeRemoved);
}

private static boolean conflictingPair(Pair<String, String> currentPair, Set<Pair<String, String>> allPairs) {
for(Pair<String, String> pair : allPairs) {
if(!pair.equals(currentPair)) {
if(pair.getLeft().equals(currentPair.getLeft()) || pair.getRight().equals(currentPair.getRight()))
return true;
}
}
return false;
}

private static String matches(String s, Set<String> classNames) {
for(String className : classNames) {
if(s.contains(className)) {
return className;
}
}
return null;
}

public List<UMLGeneralization> getAddedGeneralizations() {
return addedGeneralizations;
}
Expand Down

0 comments on commit f23d1aa

Please sign in to comment.