Skip to content

Commit

Permalink
Support Move Code refactoring in tracking of blocks, comments, annota…
Browse files Browse the repository at this point in the history
…tions, methods
  • Loading branch information
tsantalis committed Oct 17, 2024
1 parent 10344c4 commit 2f393a6
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/codetracker/AnnotationTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gr.uom.java.xmi.diff.ModifyAttributeAnnotationRefactoring;
import gr.uom.java.xmi.diff.MoveAndRenameAttributeRefactoring;
import gr.uom.java.xmi.diff.MoveAttributeRefactoring;
import gr.uom.java.xmi.diff.MoveCodeRefactoring;
import gr.uom.java.xmi.diff.MoveOperationRefactoring;
import gr.uom.java.xmi.diff.PullUpAttributeRefactoring;
import gr.uom.java.xmi.diff.PullUpOperationRefactoring;
Expand Down Expand Up @@ -140,6 +141,24 @@ public boolean checkForExtractionOrInline(Version currentVersion, Version parent
}
break;
}
case MOVE_CODE: {
MoveCodeRefactoring moveCodeRefactoring = (MoveCodeRefactoring) refactoring;
Method extractedMethod = Method.of(moveCodeRefactoring.getTargetContainer(), currentVersion);
if (equalMethod.test(extractedMethod)) {
Annotation annotationBefore;
if (rightAnnotation.getOperation().isPresent())
annotationBefore = Annotation.of(rightAnnotation.getAnnotation(), rightAnnotation.getOperation().get(), parentVersion);
else
annotationBefore = Annotation.of(rightAnnotation.getAnnotation(), rightAnnotation.getClazz().get(), parentVersion);
annotationChangeHistory.handleAdd(annotationBefore, rightAnnotation, moveCodeRefactoring.toString());
if(extractMatches == 0) {
elements.addFirst(annotationBefore);
}
annotationChangeHistory.connectRelatedNodes();
extractMatches++;
}
break;
}
case MERGE_OPERATION: {
MergeOperationRefactoring mergeOperationRefactoring = (MergeOperationRefactoring) refactoring;
Method methodAfter = Method.of(mergeOperationRefactoring.getNewMethodAfterMerge(), currentVersion);
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/codetracker/BlockTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import gr.uom.java.xmi.diff.ExtractOperationRefactoring;
import gr.uom.java.xmi.diff.InlineOperationRefactoring;
import gr.uom.java.xmi.diff.MergeOperationRefactoring;
import gr.uom.java.xmi.diff.MoveCodeRefactoring;
import gr.uom.java.xmi.diff.MoveOperationRefactoring;
import gr.uom.java.xmi.diff.PullUpOperationRefactoring;
import gr.uom.java.xmi.diff.PushDownOperationRefactoring;
Expand Down Expand Up @@ -327,6 +328,68 @@ else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof Sta
}
break;
}
case MOVE_CODE: {
MoveCodeRefactoring moveCodeRefactoring = (MoveCodeRefactoring) refactoring;
Method extractedMethod = Method.of(moveCodeRefactoring.getTargetContainer(), currentVersion);
if (equalMethod.test(extractedMethod)) {
AbstractCodeFragment matchedBlockFromSourceMethod = null;
UMLOperationBodyMapper bodyMapper = moveCodeRefactoring.getBodyMapper();
for (AbstractCodeMapping mapping : bodyMapper.getMappings()) {
if (mapping instanceof CompositeStatementObjectMapping) {
Block matchedBlockInsideExtractedMethodBody = Block.of((CompositeStatementObject) mapping.getFragment2(), bodyMapper.getContainer2(), currentVersion);
if (matchedBlockInsideExtractedMethodBody.equalIdentifierIgnoringVersion(rightBlock)) {
matchedBlockFromSourceMethod = (CompositeStatementObject) mapping.getFragment1();
Block blockBefore = Block.of((CompositeStatementObject) mapping.getFragment1(), bodyMapper.getContainer1(), parentVersion);
List<String> stringRepresentationBefore = blockBefore.getComposite().stringRepresentation();
List<String> stringRepresentationAfter = matchedBlockInsideExtractedMethodBody.getComposite().stringRepresentation();
if (!stringRepresentationBefore.equals(stringRepresentationAfter)) {
if (!stringRepresentationBefore.get(0).equals(stringRepresentationAfter.get(0))) {
blockChangeHistory.addChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.EXPRESSION_CHANGE));
}
List<String> stringRepresentationBodyBefore = stringRepresentationBefore.subList(1, stringRepresentationBefore.size());
List<String> stringRepresentationBodyAfter = stringRepresentationAfter.subList(1, stringRepresentationAfter.size());
if (!stringRepresentationBodyBefore.equals(stringRepresentationBodyAfter)) {
blockChangeHistory.addChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.BODY_CHANGE));
}
}
break;
}
}
else if (mapping instanceof LeafMapping && mapping.getFragment1() instanceof StatementObject && mapping.getFragment2() instanceof StatementObject) {
Block matchedBlockInsideExtractedMethodBody = Block.of((StatementObject) mapping.getFragment2(), bodyMapper.getContainer2(), currentVersion);
if (matchedBlockInsideExtractedMethodBody.equalIdentifierIgnoringVersion(rightBlock)) {
matchedBlockFromSourceMethod = mapping.getFragment1();
Block blockBefore = Block.of((StatementObject) mapping.getFragment1(), bodyMapper.getContainer1(), parentVersion);
List<String> stringRepresentationBefore = blockBefore.getComposite().stringRepresentation();
List<String> stringRepresentationAfter = matchedBlockInsideExtractedMethodBody.getComposite().stringRepresentation();
if (!stringRepresentationBefore.equals(stringRepresentationAfter)) {
//blockChangeHistory.addChange(blockBefore, matchedBlockInsideExtractedMethodBody, ChangeFactory.forBlock(Change.Type.BODY_CHANGE));
addStatementChange(blockBefore, matchedBlockInsideExtractedMethodBody);
}
break;
}
}
}
Block blockBefore = Block.of(rightBlock.getComposite(), rightBlock.getOperation(), parentVersion);
if (matchedBlockFromSourceMethod == null) {
blockChangeHistory.handleAdd(blockBefore, rightBlock, moveCodeRefactoring.toString());
if(extractMatches == 0) {
elements.add(blockBefore);
}
}
else {
VariableDeclarationContainer sourceOperation = moveCodeRefactoring.getSourceContainer();
Method sourceMethod = Method.of(sourceOperation, parentVersion);
Block leftBlock = Block.of(matchedBlockFromSourceMethod instanceof StatementObject ? (StatementObject) matchedBlockFromSourceMethod : (CompositeStatementObject) matchedBlockFromSourceMethod, sourceMethod);
if(extractMatches == 0) {
elements.add(leftBlock);
}
}
blockChangeHistory.connectRelatedNodes();
extractMatches++;
}
break;
}
case MOVE_AND_INLINE_OPERATION:
case INLINE_OPERATION: {
InlineOperationRefactoring inlineOperationRefactoring = (InlineOperationRefactoring) refactoring;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/codetracker/CommentTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import gr.uom.java.xmi.diff.ModifyAttributeAnnotationRefactoring;
import gr.uom.java.xmi.diff.MoveAndRenameAttributeRefactoring;
import gr.uom.java.xmi.diff.MoveAttributeRefactoring;
import gr.uom.java.xmi.diff.MoveCodeRefactoring;
import gr.uom.java.xmi.diff.MoveOperationRefactoring;
import gr.uom.java.xmi.diff.PullUpAttributeRefactoring;
import gr.uom.java.xmi.diff.PullUpOperationRefactoring;
Expand Down Expand Up @@ -286,6 +287,50 @@ public boolean checkForExtractionOrInline(Version currentVersion, Version parent
}
break;
}
case MOVE_CODE: {
MoveCodeRefactoring moveCodeRefactoring = (MoveCodeRefactoring) refactoring;
Method extractedMethod = Method.of(moveCodeRefactoring.getTargetContainer(), currentVersion);
if (equalMethod.test(extractedMethod)) {
UMLComment matchedCommentFromSourceMethod = null;
UMLOperationBodyMapper bodyMapper = moveCodeRefactoring.getBodyMapper();
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())) {
processChange(commentBefore, matchedCommentInsideExtractedMethodBody);
}
break;
}
}
}
Comment commentBefore;
if (rightComment.getOperation().isPresent())
commentBefore = Comment.of(rightComment.getComment(), rightComment.getOperation().get(), parentVersion);
else
commentBefore = Comment.of(rightComment.getComment(), rightComment.getClazz().get(), parentVersion);
if (matchedCommentFromSourceMethod == null) {
commentChangeHistory.handleAdd(commentBefore, rightComment, moveCodeRefactoring.toString());
if(extractMatches == 0) {
elements.addFirst(commentBefore);
}
}
else {
VariableDeclarationContainer sourceOperation = moveCodeRefactoring.getSourceContainer();
Method sourceMethod = Method.of(sourceOperation, parentVersion);
Comment leftComment = Comment.of(matchedCommentFromSourceMethod, sourceMethod);
if(extractMatches == 0) {
elements.addFirst(leftComment);
}
}
commentChangeHistory.connectRelatedNodes();
extractMatches++;
}
break;
}
case MOVE_AND_INLINE_OPERATION:
case INLINE_OPERATION: {
InlineOperationRefactoring inlineOperationRefactoring = (InlineOperationRefactoring) refactoring;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/codetracker/MethodTrackerChangeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import gr.uom.java.xmi.diff.ModifyVariableAnnotationRefactoring;
import gr.uom.java.xmi.diff.MoveAndRenameClassRefactoring;
import gr.uom.java.xmi.diff.MoveClassRefactoring;
import gr.uom.java.xmi.diff.MoveCodeRefactoring;
import gr.uom.java.xmi.diff.MoveOperationRefactoring;
import gr.uom.java.xmi.diff.MoveSourceFolderRefactoring;
import gr.uom.java.xmi.diff.MovedClassToAnotherSourceFolder;
Expand Down Expand Up @@ -424,6 +425,24 @@ public Set<Method> analyseMethodRefactorings(Collection<Refactoring> refactoring
}
break;
}
case MOVE_CODE: {
MoveCodeRefactoring moveCodeRefactoring = (MoveCodeRefactoring) refactoring;
operationBefore = moveCodeRefactoring.getSourceContainer();
VariableDeclarationContainer extractedOperation = moveCodeRefactoring.getTargetContainer();
Method extractedOperationAfter = Method.of(extractedOperation, currentVersion);
if (equalOperator.test(extractedOperationAfter) && moveCodeRefactoring.getMoveType().equals(MoveCodeRefactoring.Type.MOVE_TO_ADDED)) {
Method extractedOperationBefore = Method.of(extractedOperation, parentVersion);
extractedOperationBefore.setAdded(true);
methodChangeHistory.addChange(extractedOperationBefore, extractedOperationAfter, ChangeFactory.forMethod(Change.Type.INTRODUCED)
.refactoring(moveCodeRefactoring).codeElement(extractedOperationAfter).hookedElement(Method.of(operationBefore, parentVersion)));
methodChangeHistory.connectRelatedNodes();
leftMethodSet.add(extractedOperationBefore);
Method sourceOperationBefore = Method.of(operationBefore, parentVersion);
setSourceOperation(sourceOperationBefore);
return leftMethodSet;
}
break;
}
}

addMethodChange(currentVersion, parentVersion, equalOperator, leftMethodSet, refactoring, operationBefore, operationAfter, changeType);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/codetracker/change/ChangeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ else if (isAttribute())
change = new AttributeSignatureFormatChange();
else if (isClass())
change = new ClassSignatureFormatChange();
else if (isBlock())
change = new BlockSignatureFormatChange();
break;
}
default:
Expand All @@ -362,6 +364,10 @@ else if (isClass())
return change;
}

private boolean isBlock() {
return "block".equals(elementType);
}

private boolean isMethod() {
return "method".equals(elementType);
}
Expand Down
Loading

0 comments on commit 2f393a6

Please sign in to comment.