Skip to content

Commit

Permalink
Blame support for class-level comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 10, 2024
1 parent 1e0647b commit a6ee3e5
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 249 deletions.
30 changes: 30 additions & 0 deletions src/main/java/org/codetracker/AbstractTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.apache.commons.lang3.tuple.Pair;
import org.codetracker.api.Version;
import org.codetracker.element.Class;
import org.codetracker.element.Method;
import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl;

Expand Down Expand Up @@ -47,6 +48,17 @@ protected AbstractTracker(String startCommitId, String filePath) {
this.filePath = filePath;
}

protected static UMLClassBaseDiff lightweightClassDiff(UMLAbstractClass leftClass, UMLAbstractClass rightClass) {
if (leftClass instanceof UMLClass && rightClass instanceof UMLClass) {
UMLClassDiff classDiff = new UMLClassDiff((UMLClass)leftClass, (UMLClass)rightClass, null);
return classDiff;
}
else if (leftClass instanceof UMLAnonymousClass && rightClass instanceof UMLAnonymousClass) {
//TODO
}
return null;
}

protected static UMLClassBaseDiff lightweightClassDiff(UMLModel leftModel, UMLModel rightModel, VariableDeclarationContainer leftOperation, VariableDeclarationContainer rightOperation) {
UMLClass leftClass = null;
for (UMLClass clazz : leftModel.getClassList()) {
Expand Down Expand Up @@ -422,6 +434,14 @@ protected static Set<String> getRightSideFileNames(String currentFilePath, Strin
return fileNames;
}

protected static boolean isClassAdded(UMLModelDiff modelDiff, String className) {
UMLClass addedClass = modelDiff.getAddedClass(className);
if (addedClass != null) {
return true;
}
return false;
}

protected static boolean isMethodAdded(UMLModelDiff modelDiff, String className, Predicate<Method> equalOperator, Consumer<Method> addedMethodHandler, Version currentVersion) {
List<UMLOperation> addedOperations = getAllClassesDiff(modelDiff)
.stream()
Expand Down Expand Up @@ -474,6 +494,16 @@ protected static Method getMethod(UMLModel umlModel, Version version, Predicate<
return null;
}

protected static Class getClass(UMLModel umlModel, Version version, Predicate<Class> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
Class clazz = Class.of(umlClass, version);
if (predicate.test(clazz))
return clazz;
}
return null;
}

private static Method getMethod(Version version, Predicate<Method> predicate, List<UMLOperation> operations) {
for (UMLOperation umlOperation : operations) {
Method method = Method.of(umlOperation, version);
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/codetracker/CommentTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.codetracker.change.AbstractChange;
import org.codetracker.change.Change;
import org.codetracker.change.ChangeFactory;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Method;
import org.refactoringminer.api.Refactoring;
Expand All @@ -29,6 +30,7 @@
import gr.uom.java.xmi.diff.PushDownOperationRefactoring;
import gr.uom.java.xmi.diff.RenameOperationRefactoring;
import gr.uom.java.xmi.diff.SplitOperationRefactoring;
import gr.uom.java.xmi.diff.UMLAbstractClassDiff;
import gr.uom.java.xmi.diff.UMLClassBaseDiff;

public class CommentTrackerChangeHistory {
Expand Down Expand Up @@ -84,6 +86,12 @@ public boolean isStartMethod(Method method) {
method.getUmlOperation().getLocationInfo().getEndLine() >= methodDeclarationLineNumber;
}

public boolean isStartClass(Class clazz) {
return clazz.getUmlClass().getName().equals(methodName) &&
clazz.getUmlClass().getLocationInfo().getStartLine() <= methodDeclarationLineNumber &&
clazz.getUmlClass().getLocationInfo().getEndLine() >= methodDeclarationLineNumber;
}

public boolean checkClassDiffForCommentChange(ArrayDeque<Comment> comments, Version currentVersion, Version parentVersion, Predicate<Method> equalMethod, Predicate<Comment> equalComment, UMLClassBaseDiff umlClassDiff) throws RefactoringMinerTimedOutException {
for (UMLOperationBodyMapper operationBodyMapper : umlClassDiff.getOperationBodyMapperList()) {
Method method2 = Method.of(operationBodyMapper.getContainer2(), currentVersion);
Expand Down Expand Up @@ -287,6 +295,55 @@ private boolean isAdded(UMLOperationBodyMapper umlOperationBodyMapper, Queue<Com
return false;
}

public boolean checkBodyOfMatchedClasses(Queue<Comment> comments, Version currentVersion, Version parentVersion, Predicate<Comment> equalOperator, UMLAbstractClassDiff classDiff) throws RefactoringMinerTimedOutException {
if (classDiff == null)
return false;
// check if it is in the matched
if (isMatched(classDiff, comments, currentVersion, parentVersion, equalOperator))
return true;
//Check if is added
return isAdded(classDiff, comments, currentVersion, parentVersion, equalOperator);
}

public boolean isMatched(UMLAbstractClassDiff classDiff, Queue<Comment> comments, Version currentVersion, Version parentVersion, Predicate<Comment> equalOperator) {
int matches = 0;
for (Pair<UMLComment, UMLComment> mapping : classDiff.getCommentListDiff().getCommonComments()) {
Comment commentAfter = Comment.of(mapping.getRight(), classDiff.getNextClass(), currentVersion);
if (commentAfter != null && equalOperator.test(commentAfter)) {
Comment commentBefore = Comment.of(mapping.getLeft(), classDiff.getOriginalClass(), parentVersion);
if (!commentBefore.getComment().getText().equals(commentAfter.getComment().getText())) {
commentChangeHistory.addChange(commentBefore, commentAfter, ChangeFactory.forComment(Change.Type.BODY_CHANGE));
}
else {
commentChangeHistory.addChange(commentBefore, commentAfter, ChangeFactory.of(AbstractChange.Type.NO_CHANGE));
}
if(matches == 0) {
comments.add(commentBefore);
}
commentChangeHistory.connectRelatedNodes();
matches++;
}
}
if(matches > 0) {
return true;
}
return false;
}

private boolean isAdded(UMLAbstractClassDiff classDiff, Queue<Comment> comments, Version currentVersion, Version parentVersion, Predicate<Comment> equalOperator) {
for (UMLComment composite : classDiff.getCommentListDiff().getAddedComments()) {
Comment commentAfter = Comment.of(composite, classDiff.getNextClass(), currentVersion);
if (equalOperator.test(commentAfter)) {
Comment commentBefore = Comment.of(composite, classDiff.getNextClass(), parentVersion);
commentChangeHistory.handleAdd(commentBefore, commentAfter, "new comment");
comments.add(commentBefore);
commentChangeHistory.connectRelatedNodes();
return true;
}
}
return false;
}

public boolean checkRefactoredMethod(ArrayDeque<Comment> comments, Version currentVersion, Version parentVersion, Predicate<Method> equalMethod, Comment rightComment, List<Refactoring> refactorings) throws RefactoringMinerTimedOutException {
for (Refactoring refactoring : refactorings) {
UMLOperation operationBefore = null;
Expand Down
Loading

0 comments on commit a6ee3e5

Please sign in to comment.