From 54430a3d3badbec5341d63c109c66cf6eadd5729 Mon Sep 17 00:00:00 2001 From: tsantalis Date: Wed, 24 Jul 2024 16:52:24 -0400 Subject: [PATCH] Process properly extracted and added methods in FileTracker --- .../java/org/codetracker/AbstractTracker.java | 71 ++++++++ .../BlockTrackerChangeHistory.java | 7 + .../CommentTrackerChangeHistory.java | 32 ++-- .../java/org/codetracker/FileTrackerImpl.java | 154 +++++++++++------- 4 files changed, 194 insertions(+), 70 deletions(-) diff --git a/src/main/java/org/codetracker/AbstractTracker.java b/src/main/java/org/codetracker/AbstractTracker.java index 6fb36117575..eab55b64979 100644 --- a/src/main/java/org/codetracker/AbstractTracker.java +++ b/src/main/java/org/codetracker/AbstractTracker.java @@ -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; @@ -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 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 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 predicate) { if (umlModel != null) for (UMLClass umlClass : umlModel.getClassList()) { diff --git a/src/main/java/org/codetracker/BlockTrackerChangeHistory.java b/src/main/java/org/codetracker/BlockTrackerChangeHistory.java index fa9ede36037..d8288dfec5c 100644 --- a/src/main/java/org/codetracker/BlockTrackerChangeHistory.java +++ b/src/main/java/org/codetracker/BlockTrackerChangeHistory.java @@ -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 equalOperator) { for (CompositeStatementObject composite : umlOperationBodyMapper.getNonMappedInnerNodesT2()) { Block blockAfter = Block.of(composite, umlOperationBodyMapper.getContainer2(), currentVersion); diff --git a/src/main/java/org/codetracker/CommentTrackerChangeHistory.java b/src/main/java/org/codetracker/CommentTrackerChangeHistory.java index 20509c85412..cec5f5da2e6 100644 --- a/src/main/java/org/codetracker/CommentTrackerChangeHistory.java +++ b/src/main/java/org/codetracker/CommentTrackerChangeHistory.java @@ -126,16 +126,19 @@ public boolean checkForExtractionOrInline(Version currentVersion, Version parent if (equalMethod.test(extractedMethod)) { UMLComment matchedCommentFromSourceMethod = null; UMLOperationBodyMapper bodyMapper = extractOperationRefactoring.getBodyMapper(); - for (Pair 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 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()) @@ -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(); } diff --git a/src/main/java/org/codetracker/FileTrackerImpl.java b/src/main/java/org/codetracker/FileTrackerImpl.java index 6099c23ba4a..bf3e704d027 100644 --- a/src/main/java/org/codetracker/FileTrackerImpl.java +++ b/src/main/java/org/codetracker/FileTrackerImpl.java @@ -597,8 +597,9 @@ private void processLocallyRefactoredMethods(Map 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; } }