Skip to content

Commit

Permalink
Process properly extracted and added methods in FileTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 24, 2024
1 parent cd7c55d commit 54430a3
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 70 deletions.
71 changes: 71 additions & 0 deletions src/main/java/org/codetracker/AbstractTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.codetracker.api.Version;
import org.codetracker.element.Attribute;
import org.codetracker.element.BaseCodeElement;
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Method;
import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl;

Expand Down Expand Up @@ -487,9 +489,78 @@ protected static BaseCodeElement getCodeElement(UMLModel umlModel, Version versi
if (current instanceof Attribute) {
return getAttribute(umlModel, version, current::equalIdentifierIgnoringVersion);
}
else if (current instanceof Method) {
return getMethod(umlModel, version, current::equalIdentifierIgnoringVersion);
}
else if (current instanceof Block) {
return getBlock(umlModel, version, current::equalIdentifierIgnoringVersion);
}
else if (current instanceof Comment) {
return getComment(umlModel, version, current::equalIdentifierIgnoringVersion);
}
return current;
}

protected static Block getBlock(UMLModel umlModel, Version version, Predicate<Block> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
for (UMLOperation operation : umlClass.getOperations()) {
Method method = Method.of(operation, version);
Block block = method.findBlock(predicate);
if (block != null) {
return block;
}
}
for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) {
for (UMLOperation operation : anonymousClass.getOperations()) {
Method method = Method.of(operation, version);
Block block = method.findBlock(predicate);
if (block != null) {
return block;
}
}
}
}
return null;
}

protected static Comment getComment(UMLModel umlModel, Version version, Predicate<Comment> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
for (UMLOperation operation : umlClass.getOperations()) {
Method method = Method.of(operation, version);
Comment comment = method.findComment(predicate);
if (comment != null) {
return comment;
}
}
for (UMLAttribute umlAttribute : umlClass.getAttributes()) {
Attribute attribute = Attribute.of(umlAttribute, version);
Comment comment = attribute.findComment(predicate);
if (comment != null) {
return comment;
}
}
for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) {
for (UMLOperation operation : anonymousClass.getOperations()) {
Method method = Method.of(operation, version);
Comment comment = method.findComment(predicate);
if (comment != null) {
return comment;
}
}
for (UMLAttribute umlAttribute : anonymousClass.getAttributes()) {
Attribute attribute = Attribute.of(umlAttribute, version);
Comment comment = attribute.findComment(predicate);
if (comment != null) {
return comment;
}
}
}
}
return null;
}

protected static Method getMethod(UMLModel umlModel, Version version, Predicate<Method> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/codetracker/BlockTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
return false;
}

public void addedMethod(Method rightMethod, Block rightBlock, Version parentVersion) {
Block blockBefore = Block.of(rightBlock.getComposite(), rightMethod.getUmlOperation(), parentVersion);
blockChangeHistory.handleAdd(blockBefore, rightBlock, "added with method");
elements.add(blockBefore);
blockChangeHistory.connectRelatedNodes();
}

private boolean isAdded(UMLOperationBodyMapper umlOperationBodyMapper, Version currentVersion, Version parentVersion, Predicate<Block> equalOperator) {
for (CompositeStatementObject composite : umlOperationBodyMapper.getNonMappedInnerNodesT2()) {
Block blockAfter = Block.of(composite, umlOperationBodyMapper.getContainer2(), currentVersion);
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/org/codetracker/CommentTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,19 @@ public boolean checkForExtractionOrInline(Version currentVersion, Version parent
if (equalMethod.test(extractedMethod)) {
UMLComment matchedCommentFromSourceMethod = null;
UMLOperationBodyMapper bodyMapper = extractOperationRefactoring.getBodyMapper();
for (Pair<UMLComment, UMLComment> mapping : bodyMapper.getCommentListDiff().getCommonComments()) {
Comment matchedCommentInsideExtractedMethodBody = Comment.of(mapping.getRight(), bodyMapper.getContainer2(), currentVersion);
if (matchedCommentInsideExtractedMethodBody.equalIdentifierIgnoringVersion(rightComment)) {
matchedCommentFromSourceMethod = mapping.getLeft();
Comment commentBefore = Comment.of(mapping.getLeft(), bodyMapper.getContainer1(), parentVersion);
if (!commentBefore.getComment().getText().equals(matchedCommentInsideExtractedMethodBody.getComment().getText())) {
commentChangeHistory.addChange(commentBefore, matchedCommentInsideExtractedMethodBody, ChangeFactory.forComment(Change.Type.BODY_CHANGE));
}
break;
}
UMLCommentListDiff commentListDiff = bodyMapper.getCommentListDiff();
if (commentListDiff != null) {
for (Pair<UMLComment, UMLComment> mapping : commentListDiff.getCommonComments()) {
Comment matchedCommentInsideExtractedMethodBody = Comment.of(mapping.getRight(), bodyMapper.getContainer2(), currentVersion);
if (matchedCommentInsideExtractedMethodBody.equalIdentifierIgnoringVersion(rightComment)) {
matchedCommentFromSourceMethod = mapping.getLeft();
Comment commentBefore = Comment.of(mapping.getLeft(), bodyMapper.getContainer1(), parentVersion);
if (!commentBefore.getComment().getText().equals(matchedCommentInsideExtractedMethodBody.getComment().getText())) {
commentChangeHistory.addChange(commentBefore, matchedCommentInsideExtractedMethodBody, ChangeFactory.forComment(Change.Type.BODY_CHANGE));
}
break;
}
}
}
Comment commentBefore;
if (rightComment.getOperation().isPresent())
Expand Down Expand Up @@ -351,9 +354,16 @@ public boolean isMatched(UMLOperationBodyMapper umlOperationBodyMapper, Version
return false;
}

public void addedMethod(Method rightMethod, Comment rightComment, Version parentVersion) {
Comment commentBefore = Comment.of(rightComment.getComment(), rightMethod.getUmlOperation(), parentVersion);
commentChangeHistory.handleAdd(commentBefore, rightComment, "added with method");
elements.add(commentBefore);
commentChangeHistory.connectRelatedNodes();
}

public void addedAttribute(Attribute rightAttribute, Comment rightComment, Version parentVersion) {
Comment commentBefore = Comment.of(rightComment.getComment(), rightAttribute.getUmlAttribute(), parentVersion);
commentChangeHistory.handleAdd(commentBefore, rightComment, "new comment");
commentChangeHistory.handleAdd(commentBefore, rightComment, "added with attribute");
elements.add(commentBefore);
commentChangeHistory.connectRelatedNodes();
}
Expand Down
154 changes: 95 additions & 59 deletions src/main/java/org/codetracker/FileTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ private void processLocallyRefactoredMethods(Map<Method, MethodTrackerChangeHist
for (CodeElement key2 : programElementMap.keySet()) {
if (key2 instanceof Block) {
Block startBlock = (Block)key2;
if (startBlock.getOperation().equals(startMethod.getUmlOperation())) {
BlockTrackerChangeHistory startBlockChangeHistory = (BlockTrackerChangeHistory) programElementMap.get(startBlock);
BlockTrackerChangeHistory startBlockChangeHistory = (BlockTrackerChangeHistory) programElementMap.get(startBlock);
if (startBlock.getOperation().equals(startMethod.getUmlOperation()) ||
(!startBlockChangeHistory.isEmpty() && rightMethod.getUmlOperation().equals(startBlockChangeHistory.peek().getOperation()))) {
Block currentBlock = startBlockChangeHistory.poll();
if (currentBlock == null) {
continue;
Expand All @@ -623,8 +624,9 @@ private void processLocallyRefactoredMethods(Map<Method, MethodTrackerChangeHist
}
else if (key2 instanceof Comment) {
Comment startComment = (Comment)key2;
if (startComment.getOperation().isPresent() && startComment.getOperation().get().equals(startMethod.getUmlOperation())) {
CommentTrackerChangeHistory startCommentChangeHistory = (CommentTrackerChangeHistory) programElementMap.get(startComment);
CommentTrackerChangeHistory startCommentChangeHistory = (CommentTrackerChangeHistory) programElementMap.get(startComment);
if ((startComment.getOperation().isPresent() && startComment.getOperation().get().equals(startMethod.getUmlOperation())) ||
(!startCommentChangeHistory.isEmpty() && startCommentChangeHistory.peek().getOperation().isPresent() && rightMethod.getUmlOperation().equals(startCommentChangeHistory.peek().getOperation().get()))) {
Comment currentComment = startCommentChangeHistory.poll();
if (currentComment == null) {
continue;
Expand All @@ -649,8 +651,41 @@ else if (key2 instanceof Comment) {
}
}
}
else {
startMethodChangeHistory.isMethodAdded(umlModelDiff, rightMethod.getUmlOperation().getClassName(), currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, getAllClassesDiff(umlModelDiff));
else if(startMethodChangeHistory.isMethodAdded(umlModelDiff, rightMethod.getUmlOperation().getClassName(), currentVersion, parentVersion, rightMethod::equalIdentifierIgnoringVersion, getAllClassesDiff(umlModelDiff))) {
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()) ||
(!startBlockChangeHistory.isEmpty() && rightMethod.getUmlOperation().equals(startBlockChangeHistory.peek().getOperation()))) {
Block currentBlock = startBlockChangeHistory.poll();
if (currentBlock == null) {
continue;
}
Block rightBlock = rightMethod.findBlock(currentBlock::equalIdentifierIgnoringVersion);
if (rightBlock == null) {
continue;
}
startBlockChangeHistory.addedMethod(rightMethod, rightBlock, parentVersion);
}
}
else if (key2 instanceof Comment) {
Comment startComment = (Comment)key2;
CommentTrackerChangeHistory startCommentChangeHistory = (CommentTrackerChangeHistory) programElementMap.get(startComment);
if ((startComment.getOperation().isPresent() && startComment.getOperation().get().equals(rightMethod.getUmlOperation())) ||
(!startCommentChangeHistory.isEmpty() && startCommentChangeHistory.peek().getOperation().isPresent() && rightMethod.getUmlOperation().equals(startCommentChangeHistory.peek().getOperation().get()))) {
Comment currentComment = startCommentChangeHistory.poll();
if (currentComment == null) {
continue;
}
Comment rightComment = rightMethod.findComment(currentComment::equalIdentifierIgnoringVersion);
if (rightComment == null) {
continue;
}
startCommentChangeHistory.addedMethod(rightMethod, rightComment, parentVersion);
}
}
}
}
}
}
Expand Down Expand Up @@ -750,64 +785,65 @@ private Map<Method, MethodTrackerChangeHistory> processMethodsWithSameSignature(
startMethodChangeHistory.get().addChange(leftMethod, rightMethod, ChangeFactory.forMethod(Change.Type.DOCUMENTATION_CHANGE));
startMethodChangeHistory.get().connectRelatedNodes();
startMethodChangeHistory.setCurrent(leftMethod);
VariableDeclarationContainer leftOperation = leftMethod.getUmlOperation();
VariableDeclarationContainer rightOperation = rightMethod.getUmlOperation();
UMLOperationBodyMapper bodyMapper = null;
if (leftOperation instanceof UMLOperation && rightOperation instanceof UMLOperation) {
UMLClassBaseDiff lightweightClassDiff = lightweightClassDiff(leftModel, rightModel, leftOperation, rightOperation);
bodyMapper = new UMLOperationBodyMapper((UMLOperation) leftOperation, (UMLOperation) rightOperation, lightweightClassDiff);
if (containsCallToExtractedMethod(bodyMapper, bodyMapper.getClassDiff())) {
bodyMapper = null;
}
processNestedStatementsAndComments(rightModel, currentVersion, leftModel, parentVersion,
startMethod, rightMethod, leftMethod);
}
}
}
return notFoundMethods;
}

private void processNestedStatementsAndComments(UMLModel rightModel, Version currentVersion, UMLModel leftModel,
Version parentVersion, Method startMethod, Method rightMethod, Method leftMethod)
throws RefactoringMinerTimedOutException {
VariableDeclarationContainer leftOperation = leftMethod.getUmlOperation();
VariableDeclarationContainer rightOperation = rightMethod.getUmlOperation();
UMLOperationBodyMapper bodyMapper = null;
if (leftOperation instanceof UMLOperation && rightOperation instanceof UMLOperation) {
UMLClassBaseDiff lightweightClassDiff = lightweightClassDiff(leftModel, rightModel, leftOperation, rightOperation);
bodyMapper = new UMLOperationBodyMapper((UMLOperation) leftOperation, (UMLOperation) rightOperation, lightweightClassDiff);
}
else if (leftOperation instanceof UMLInitializer && rightOperation instanceof UMLInitializer) {
UMLClassBaseDiff lightweightClassDiff = lightweightClassDiff(leftModel, rightModel, leftOperation, rightOperation);
bodyMapper = new UMLOperationBodyMapper((UMLInitializer) leftOperation, (UMLInitializer) rightOperation, lightweightClassDiff);
}
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()) ||
(!startBlockChangeHistory.isEmpty() && rightMethod.getUmlOperation().equals(startBlockChangeHistory.peek().getOperation()))) {
Block currentBlock = startBlockChangeHistory.poll();
if (currentBlock == null) {
continue;
}
else if (leftOperation instanceof UMLInitializer && rightOperation instanceof UMLInitializer) {
UMLClassBaseDiff lightweightClassDiff = lightweightClassDiff(leftModel, rightModel, leftOperation, rightOperation);
bodyMapper = new UMLOperationBodyMapper((UMLInitializer) leftOperation, (UMLInitializer) rightOperation, lightweightClassDiff);
if (containsCallToExtractedMethod(bodyMapper, bodyMapper.getClassDiff())) {
bodyMapper = null;
}
Block rightBlock = rightMethod.findBlock(currentBlock::equalIdentifierIgnoringVersion);
if (rightBlock == null) {
continue;
}
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()) ||
(!startBlockChangeHistory.isEmpty() && rightMethod.getUmlOperation().equals(startBlockChangeHistory.peek().getOperation()))) {
Block currentBlock = startBlockChangeHistory.poll();
if (currentBlock == null) {
continue;
}
Block rightBlock = rightMethod.findBlock(currentBlock::equalIdentifierIgnoringVersion);
if (rightBlock == null) {
continue;
}
if (startBlockChangeHistory.checkBodyOfMatchedOperations(currentVersion, parentVersion, rightBlock::equalIdentifierIgnoringVersion, bodyMapper)) {
continue;
}
}
}
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())) ||
(!startCommentChangeHistory.isEmpty() && startCommentChangeHistory.peek().getOperation().isPresent() && rightMethod.getUmlOperation().equals(startCommentChangeHistory.peek().getOperation().get()))) {
Comment currentComment = startCommentChangeHistory.poll();
if (currentComment == null) {
continue;
}
Comment rightComment = rightMethod.findComment(currentComment::equalIdentifierIgnoringVersion);
if (rightComment == null) {
continue;
}
if (startCommentChangeHistory.checkBodyOfMatchedOperations(currentVersion, parentVersion, rightComment::equalIdentifierIgnoringVersion, bodyMapper)) {
continue;
}
}
}
if (startBlockChangeHistory.checkBodyOfMatchedOperations(currentVersion, parentVersion, rightBlock::equalIdentifierIgnoringVersion, bodyMapper)) {
continue;
}
}
}
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())) ||
(!startCommentChangeHistory.isEmpty() && startCommentChangeHistory.peek().getOperation().isPresent() && rightMethod.getUmlOperation().equals(startCommentChangeHistory.peek().getOperation().get()))) {
Comment currentComment = startCommentChangeHistory.poll();
if (currentComment == null) {
continue;
}
Comment rightComment = rightMethod.findComment(currentComment::equalIdentifierIgnoringVersion);
if (rightComment == null) {
continue;
}
if (startCommentChangeHistory.checkBodyOfMatchedOperations(currentVersion, parentVersion, rightComment::equalIdentifierIgnoringVersion, bodyMapper)) {
continue;
}
}
}
}
return notFoundMethods;
}
}

0 comments on commit 54430a3

Please sign in to comment.