Skip to content

Commit

Permalink
Add source operation of extracted methods in the tracked code elements
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Aug 4, 2024
1 parent 9fe41b9 commit a77ae76
Show file tree
Hide file tree
Showing 4 changed files with 2,434 additions and 2,352 deletions.
56 changes: 56 additions & 0 deletions src/main/java/org/codetracker/FileTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ else if (annotation.getClazz().isPresent()) {
return null;
}

private Set<CodeElement> remainingBlames(Method startMethod) {
Set<CodeElement> remaining = new LinkedHashSet<CodeElement>();
for (CodeElement key2 : programElementMap.keySet()) {
if (key2 instanceof Block) {
Block startBlock = (Block)key2;
BlockTrackerChangeHistory startBlockChangeHistory = (BlockTrackerChangeHistory) programElementMap.get(startBlock);
if (startBlock.getOperation().equals(startMethod.getUmlOperation())) {
HistoryInfo<Block> historyInfo = startBlockChangeHistory.blameReturn(startBlock);
if (historyInfo == null) {
remaining.add(startBlock);
}
}
}
else if (key2 instanceof Comment) {
Comment startComment = (Comment)key2;
CommentTrackerChangeHistory startCommentChangeHistory = (CommentTrackerChangeHistory) programElementMap.get(startComment);
if (startComment.getOperation().isPresent() && startComment.getOperation().get().equals(startMethod.getUmlOperation())) {
HistoryInfo<Comment> historyInfo = startCommentChangeHistory.blameReturn();
if (historyInfo == null) {
remaining.add(startComment);
}
}
}
}
return remaining;
}

public void blame() throws Exception {
try (Git git = new Git(repository); RevWalk walk = new RevWalk(repository)) {
Version startVersion = gitRepository.getVersion(startCommitId);
Expand Down Expand Up @@ -772,6 +799,35 @@ else if (key2 instanceof Annotation) {
}
}
}
if (startMethodChangeHistory.peek().isAdded() && startMethodChangeHistory.getSourceOperation() != null) {
Method codeElement = startMethodChangeHistory.getSourceOperation();
boolean found = false;
for (CodeElement key2 : programElementMap.keySet()) {
if (key2 instanceof Method) {
Method method = (Method)key2;
MethodTrackerChangeHistory methodChangeHistory = (MethodTrackerChangeHistory) programElementMap.get(method);
if (codeElement.getUmlOperation().equals(method.getUmlOperation())) {
found = true;
break;
}
if (codeElement.getUmlOperation() instanceof UMLOperation && method.getUmlOperation() instanceof UMLOperation &&
((UMLOperation)codeElement.getUmlOperation()).equalSignature((UMLOperation)method.getUmlOperation())) {
found = true;
break;
}
if (!methodChangeHistory.isEmpty() && codeElement.getUmlOperation().equals(methodChangeHistory.peek().getUmlOperation())) {
found = true;
break;
}
}
}
if (!found && remainingBlames(startMethod).size() > 0) {
AbstractChangeHistory<BaseCodeElement> changeHistory = (AbstractChangeHistory<BaseCodeElement>) factory(codeElement);
changeHistory.addFirst((BaseCodeElement) codeElement);
changeHistory.get().addNode((BaseCodeElement) codeElement);
programElementMap.put(codeElement, changeHistory);
}
}
}
else if(startMethodChangeHistory.isMethodAdded(umlModelDiff, rightMethod.getUmlOperation().getClassName(), currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, getAllClassesDiff(umlModelDiff))) {
for (CodeElement key2 : programElementMap.keySet()) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/codetracker/MethodTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ public class MethodTrackerChangeHistory extends AbstractChangeHistory<Method> {
private final ChangeHistory<Method> methodChangeHistory = new ChangeHistory<>();
private final String methodName;
private final int methodDeclarationLineNumber;
private Method sourceOperation;

public MethodTrackerChangeHistory(String methodName, int methodDeclarationLineNumber) {
this.methodName = methodName;
this.methodDeclarationLineNumber = methodDeclarationLineNumber;
}

public Method getSourceOperation() {
return sourceOperation;
}

public void setSourceOperation(Method sourceOperation) {
this.sourceOperation = sourceOperation;
}

public ChangeHistory<Method> get() {
return methodChangeHistory;
}
Expand Down Expand Up @@ -368,6 +377,8 @@ public Set<Method> analyseMethodRefactorings(Collection<Refactoring> refactoring
.refactoring(extractOperationRefactoring).codeElement(extractedOperationAfter).hookedElement(Method.of(operationBefore, parentVersion)));
methodChangeHistory.connectRelatedNodes();
leftMethodSet.add(extractedOperationBefore);
Method sourceOperationBefore = Method.of(operationBefore, parentVersion);
setSourceOperation(sourceOperationBefore);
return leftMethodSet;
}
UMLOperationBodyMapper mapper = extractOperationRefactoring.getBodyMapper();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ public void testBlameWithLocalRepoUsingFileTracker2() throws Exception {
assertEqualWithFile(expectedFilePath, actual);
}

@Test
public void testBlameWithLocalRepoUsingFileTrackerUntilCommitZero() throws Exception {
String url = "https://github.com/eclipse/jgit/commit/bd1a82502680b5de5bf86f6c4470185fd1602386";
String filePath = "org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java";
String expectedFilePath = System.getProperty("user.dir") + "/src/test/resources/blame/blameTestUntilCommitZero.txt";
String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getOwner(url) + "/" + getProject(url), URLHelper.getRepo(url));
FileTrackerImpl fileTracker = new FileTrackerImpl(repository, commitId, filePath);
fileTracker.blame();
BlameFormatter blameFormatter = new BlameFormatter(fileTracker.getLines());
List<String[]> results = blameFormatter.make(fileTracker.getBlameInfo());
String actual = TabularPrint.make(results);
assertEqualWithFile(expectedFilePath, actual);
}

@Test
public void testBlameWithLocalRepo() throws Exception {
String url = "https://github.com/pouryafard75/DiffBenchmark/commit/5b33dc6f8cfcf8c0e31966c035b0406eca97ec76";
Expand Down
Loading

0 comments on commit a77ae76

Please sign in to comment.