From baad546e4d5afb12149b7613618dba13d2424fde Mon Sep 17 00:00:00 2001 From: tsantalis Date: Fri, 4 Oct 2024 05:33:42 -0400 Subject: [PATCH] Improved isMoved() to handle anonymous methods whose parent container is moved --- .../java/org/codetracker/FileTrackerImpl.java | 15 +++++++++++---- .../FileTrackerWithLocalFilesImpl.java | 15 +++++++++++---- .../resources/blame/blameTestWithLocalRepo15.txt | 6 +++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/codetracker/FileTrackerImpl.java b/src/main/java/org/codetracker/FileTrackerImpl.java index c2eb3c690de..69c6ff53fef 100644 --- a/src/main/java/org/codetracker/FileTrackerImpl.java +++ b/src/main/java/org/codetracker/FileTrackerImpl.java @@ -42,6 +42,7 @@ import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl; import gr.uom.java.xmi.LocationInfo.CodeElementType; +import gr.uom.java.xmi.UMLAnonymousClass; import gr.uom.java.xmi.UMLAttribute; import gr.uom.java.xmi.decomposition.UMLOperationBodyMapper; import gr.uom.java.xmi.diff.ExtractClassRefactoring; @@ -967,10 +968,16 @@ else if (key2 instanceof Annotation) { } private boolean isMoved(Method rightMethod, List refactorings) { + Set parentContainers = new LinkedHashSet<>(); + parentContainers.add(rightMethod.getUmlOperation()); + if (rightMethod.getUmlOperation().getAnonymousClassContainer() != null && rightMethod.getUmlOperation().getAnonymousClassContainer().isPresent()) { + UMLAnonymousClass anonymous = rightMethod.getUmlOperation().getAnonymousClassContainer().get(); + parentContainers.addAll(anonymous.getParentContainers()); + } for (Refactoring r : refactorings) { if (r instanceof MoveOperationRefactoring) { MoveOperationRefactoring move = (MoveOperationRefactoring) r; - if (move.getMovedOperation().equals(rightMethod.getUmlOperation())) + if (parentContainers.contains(move.getMovedOperation())) return true; } } @@ -998,12 +1005,12 @@ private void processLocallyRefactoredMethods(Map leftSideMethods = startMethodChangeHistory.analyseMethodRefactorings(refactorings, currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion); Set methodContainerChanged = startMethodChangeHistory.isMethodContainerChanged(umlModelDiff, refactorings, currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, getClassMoveDiffList(umlModelDiff)); leftSideMethods.addAll(methodContainerChanged); boolean refactored = !leftSideMethods.isEmpty(); - if (refactored) { - boolean moved = isMoved(rightMethod, refactorings); + if (refactored || moved) { leftSideMethods.forEach(startMethodChangeHistory::addFirst); if (leftSideMethods.size() == 1) { startMethodChangeHistory.setCurrent(leftSideMethods.iterator().next()); @@ -1116,7 +1123,7 @@ else if (key2 instanceof Annotation) { } } } - if (startMethodChangeHistory.peek().isAdded() && startMethodChangeHistory.getSourceOperation() != null) { + if (startMethodChangeHistory.peek() != null && startMethodChangeHistory.peek().isAdded() && startMethodChangeHistory.getSourceOperation() != null) { Method codeElement = startMethodChangeHistory.getSourceOperation(); boolean found = false; for (CodeElement key2 : programElementMap.keySet()) { diff --git a/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java b/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java index 1407c8650ae..64005aa7aed 100644 --- a/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java +++ b/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java @@ -47,6 +47,7 @@ import gr.uom.java.xmi.UMLType; import gr.uom.java.xmi.VariableDeclarationContainer; import gr.uom.java.xmi.LocationInfo.CodeElementType; +import gr.uom.java.xmi.UMLAnonymousClass; import gr.uom.java.xmi.decomposition.UMLOperationBodyMapper; import gr.uom.java.xmi.diff.ExtractClassRefactoring; import gr.uom.java.xmi.diff.ExtractSuperclassRefactoring; @@ -974,10 +975,16 @@ else if (key2 instanceof Annotation) { } private boolean isMoved(Method rightMethod, List refactorings) { + Set parentContainers = new LinkedHashSet<>(); + parentContainers.add(rightMethod.getUmlOperation()); + if (rightMethod.getUmlOperation().getAnonymousClassContainer() != null && rightMethod.getUmlOperation().getAnonymousClassContainer().isPresent()) { + UMLAnonymousClass anonymous = rightMethod.getUmlOperation().getAnonymousClassContainer().get(); + parentContainers.addAll(anonymous.getParentContainers()); + } for (Refactoring r : refactorings) { if (r instanceof MoveOperationRefactoring) { MoveOperationRefactoring move = (MoveOperationRefactoring) r; - if (move.getMovedOperation().equals(rightMethod.getUmlOperation())) + if (parentContainers.contains(move.getMovedOperation())) return true; } } @@ -1005,12 +1012,12 @@ private void processLocallyRefactoredMethods(Map leftSideMethods = startMethodChangeHistory.analyseMethodRefactorings(refactorings, currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion); Set methodContainerChanged = startMethodChangeHistory.isMethodContainerChanged(umlModelDiff, refactorings, currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, getClassMoveDiffList(umlModelDiff)); leftSideMethods.addAll(methodContainerChanged); boolean refactored = !leftSideMethods.isEmpty(); - if (refactored) { - boolean moved = isMoved(rightMethod, refactorings); + if (refactored || moved) { leftSideMethods.forEach(startMethodChangeHistory::addFirst); if (leftSideMethods.size() == 1) { startMethodChangeHistory.setCurrent(leftSideMethods.iterator().next()); @@ -1123,7 +1130,7 @@ else if (key2 instanceof Annotation) { } } } - if (startMethodChangeHistory.peek().isAdded() && startMethodChangeHistory.getSourceOperation() != null) { + if (startMethodChangeHistory.peek() != null && startMethodChangeHistory.peek().isAdded() && startMethodChangeHistory.getSourceOperation() != null) { Method codeElement = startMethodChangeHistory.getSourceOperation(); boolean found = false; for (CodeElement key2 : programElementMap.keySet()) { diff --git a/src/test/resources/blame/blameTestWithLocalRepo15.txt b/src/test/resources/blame/blameTestWithLocalRepo15.txt index 1dcbad99a55..b2a9d7dfdbb 100644 --- a/src/test/resources/blame/blameTestWithLocalRepo15.txt +++ b/src/test/resources/blame/blameTestWithLocalRepo15.txt @@ -304,10 +304,10 @@ bd5b90f75 src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java ( 078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 304) Object test; 078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 305) try { bd5b90f75 src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java (Peter Wright 2014-12-07 14:18:17 +0000 306) test = new ReflectiveCallable() { - 307) @Override - 308) protected Object runReflectiveCall() throws Throwable { +078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 307) @Override +078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 308) protected Object runReflectiveCall() throws Throwable { bd5b90f75 src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java (Peter Wright 2014-12-07 14:18:17 +0000 309) return createTest(method); - 310) } +078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 310) } 078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 311) }.run(); 078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 312) } catch (Throwable e) { 078e45bd3 src/main/java/org/junit/internal/runners/BlockJUnit4ClassRunner.java (kbeck 2008-06-16 17:52:02 +0000 313) return new Fail(e);