Skip to content

Commit

Permalink
Support Extract Method detection in commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jan 15, 2025
1 parent 75c0985 commit 0d3861b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class AbstractCodeMapping implements LeafMappingProvider {
private int matchingArgumentsWithOperationInvocation;
private boolean matchedWithNullReplacements;
private Set<UMLAnonymousClassDiff> anonymousClassDiffs = new LinkedHashSet<UMLAnonymousClassDiff>();
private List<UMLOperationBodyMapper> lambdaMappers = new ArrayList<UMLOperationBodyMapper>();

public AbstractCodeMapping(AbstractCodeFragment fragment1, AbstractCodeFragment fragment2,
VariableDeclarationContainer operation1, VariableDeclarationContainer operation2) {
Expand Down Expand Up @@ -106,6 +107,14 @@ public Set<UMLAnonymousClassDiff> getAnonymousClassDiffs() {
return anonymousClassDiffs;
}

public void addLambdaMappers(List<UMLOperationBodyMapper> mappers) {
lambdaMappers.addAll(mappers);
}

public List<UMLOperationBodyMapper> getLambdaMappers() {
return lambdaMappers;
}

public boolean containsRefactoringOfType(RefactoringType type) {
for(Refactoring r : refactorings) {
if(r.getRefactoringType().equals(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4140,6 +4140,14 @@ public Set<Replacement> getReplacementsOfType(ReplacementType type) {
return replacements;
}

public boolean containsLambdaMapper() {
for(AbstractCodeMapping mapping : getMappings()) {
if(mapping.getLambdaMappers().size() > 0 && !mapping.getFragment1().getString().equals(mapping.getFragment2().getString()))
return true;
}
return false;
}

public boolean containsCompositeMappingWithoutReplacements() {
for(AbstractCodeMapping mapping : getMappings()) {
if(mapping instanceof CompositeStatementObjectMapping && !mapping.getFragment1().equalFragment(mapping.getFragment2()) && mapping.getReplacements().isEmpty()) {
Expand Down Expand Up @@ -4793,6 +4801,7 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT
if(score2 > 0) {
CompositeStatementObjectMapping mapping = createCompositeMapping(matchingInnerNode1, statement2, parameterToArgumentMap, score2);
mapping.addReplacements(replacements2);
mapping.addLambdaMappers(replacementInfo2.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo2.getSubExpressionMappings());
mappingSet.add(mapping);
}
Expand All @@ -4804,6 +4813,7 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT
(score > 0 || Math.max(statement1.getStatements().size(), statement2.getStatements().size()) == 0)) {
CompositeStatementObjectMapping mapping = createCompositeMapping(statement1, statement2, parameterToArgumentMap, score);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
UMLAbstractClassDiff classDiff = this.classDiff != null ? this.classDiff : parentMapper != null ? parentMapper.classDiff : null;
for(AbstractCodeFragment leaf : leaves2) {
Expand Down Expand Up @@ -4849,6 +4859,7 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT
}
else if(replacements == null && !containsCallToExtractedMethod && statement1.getLocationInfo().getCodeElementType().equals(statement2.getLocationInfo().getCodeElementType()) && score > 0 && mappingSet.size() > 0 && score >= 2.0*mappingSet.first().getCompositeChildMatchingScore()) {
CompositeStatementObjectMapping mapping = createCompositeMapping(statement1, statement2, parameterToArgumentMap, score);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
mappingSet.add(mapping);
}
Expand Down Expand Up @@ -5148,6 +5159,7 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT
if(score2 > 0) {
CompositeStatementObjectMapping mapping = createCompositeMapping(statement1, matchingInnerNode2, parameterToArgumentMap, score2);
mapping.addReplacements(replacements2);
mapping.addLambdaMappers(replacementInfo2.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo2.getSubExpressionMappings());
mappingSet.add(mapping);
}
Expand All @@ -5159,6 +5171,7 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT
(score > 0 || Math.max(statement1.getStatements().size(), statement2.getStatements().size()) == 0)) {
CompositeStatementObjectMapping mapping = createCompositeMapping(statement1, statement2, parameterToArgumentMap, score);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
UMLAbstractClassDiff classDiff = this.classDiff != null ? this.classDiff : parentMapper != null ? parentMapper.classDiff : null;
for(AbstractCodeFragment leaf : leaves2) {
Expand Down Expand Up @@ -5195,6 +5208,7 @@ else if(replacements.size() == 1 && replacementInfo.getReplacements(ReplacementT
}
else if(replacements == null && !containsCallToExtractedMethod && statement1.getLocationInfo().getCodeElementType().equals(statement2.getLocationInfo().getCodeElementType()) && score > 0 && mappingSet.size() > 0 && score >= 2.0*mappingSet.first().getCompositeChildMatchingScore()) {
CompositeStatementObjectMapping mapping = createCompositeMapping(statement1, statement2, parameterToArgumentMap, score);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
mappingSet.add(mapping);
}
Expand Down Expand Up @@ -5363,6 +5377,7 @@ else if(comp.getLocationInfo().getCodeElementType().equals(CodeElementType.IF_ST
if(score > 0) {
CompositeStatementObjectMapping newMapping = createCompositeMapping((CompositeStatementObject)mapping.getFragment1(), innerNode2, parameterToArgumentMap, score);
newMapping.addReplacements(replacementInfo.getReplacements());
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
newMapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
addMapping(newMapping);
mappingsToBeRemoved.add(mapping);
Expand Down Expand Up @@ -6221,6 +6236,7 @@ else if(mapping.getFragment1().getLocationInfo().getStartLine() > endMapping.get
if (replacements != null) {
LeafMapping mapping = createLeafMapping(leaf1, leaf2, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
extractInlineVariableAnalysis(leaves1, leaves2, leaf1, leaf2, mapping, replacementInfo);
mappingSet.add(mapping);
Expand Down Expand Up @@ -6688,6 +6704,7 @@ else if(mapping.getFragment1().getLocationInfo().getStartLine() > endMapping.get
if (replacements != null) {
LeafMapping mapping = createLeafMapping(leaf1, leaf2, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
extractInlineVariableAnalysis(leaves1, leaves2, leaf1, leaf2, mapping, replacementInfo);
mappingSet.add(mapping);
Expand Down Expand Up @@ -7174,6 +7191,7 @@ else if(r1 instanceof MethodInvocationReplacement && r2 instanceof MethodInvocat
if(matchingMappings == mappingSet.size() || matchWithLessReplacements || differentVariableDeclarationNumber) {
LeafMapping mapping = createLeafMapping(leaf, leaf2, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
extractInlineVariableAnalysis(leaves1, leaves2, leaf, leaf2, mapping, replacementInfo);
mappingSet.add(mapping);
Expand Down Expand Up @@ -7244,6 +7262,7 @@ private void checkForOtherPossibleMatchesForFragment1(List<? extends AbstractCod
if (replacements != null) {
LeafMapping mapping = createLeafMapping(leaf1, leaf, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
boolean allowAdd = false;
for(LeafMapping m : mappingSet) {
Expand Down Expand Up @@ -7278,6 +7297,7 @@ else if(parent1 == null && (parent2 == null || parent2.getParent() == null) && (
if (replacements != null) {
LeafMapping mapping = createLeafMapping(leaf1, leaf, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
extractInlineVariableAnalysis(leaves1, leaves2, leaf1, leaf, mapping, replacementInfo);
mappingSet.add(mapping);
Expand Down Expand Up @@ -7360,6 +7380,7 @@ else if(exactMappingsBefore + inexactMappingsBefore < exactMappingsAfter + inexa
if (replacements != null) {
LeafMapping mapping = createLeafMapping(leaf1, leaf, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
int lineDistance = 0;
if(exactMappingsBefore + inexactMappingsBefore == 0 && callsToExtractedMethod == 1) {
Expand Down Expand Up @@ -7448,6 +7469,7 @@ else if(exactMappingsBefore + inexactMappingsBefore < exactMappingsAfter + inexa
if (replacements != null) {
LeafMapping mapping = createLeafMapping(leaf, leaf2, parameterToArgumentMap, equalNumberOfAssertions);
mapping.addReplacements(replacements);
mapping.addLambdaMappers(replacementInfo.getLambdaMappers());
mapping.addSubExpressionMappings(replacementInfo.getSubExpressionMappings());
int lineDistance = 0;
if(exactMappingsBefore + inexactMappingsBefore == 0 && callsToInlinedMethod == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public List<ExtractOperationRefactoring> check(UMLOperation addedOperation) thro
return refactorings;
}
if(!mapper.getNonMappedLeavesT1().isEmpty() || !mapper.getNonMappedInnerNodesT1().isEmpty() ||
!mapper.getReplacementsInvolvingMethodInvocation().isEmpty() || mapper.containsCompositeMappingWithoutReplacements()) {
!mapper.getReplacementsInvolvingMethodInvocation().isEmpty() || mapper.containsCompositeMappingWithoutReplacements() || mapper.containsLambdaMapper()) {
List<AbstractCall> addedOperationInvocations = callCountMap != null ? callCountMap.get(addedOperation) : matchingInvocations(addedOperation, operationInvocations, mapper.getContainer2());
if(addedOperationInvocations.size() > 0) {
int otherAddedMethodsCalled = 0;
Expand Down

0 comments on commit 0d3861b

Please sign in to comment.