Skip to content

Commit

Permalink
Support Extract Variable detection when direct field access is replaced
Browse files Browse the repository at this point in the history
with getter call in the initializer of the extracted variable
JetBrains/intellij-community@d5def50
Extract Variable	actionType : int in method private changePathInActionsTree(tree JTree, url ActionUrl) : void from class com.intellij.ide.ui.customization.CustomizableActionsPanel
  • Loading branch information
tsantalis committed Dec 28, 2024
1 parent 87b0460 commit 8dc4884
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.refactoringminer.util.PrefixSuffixUtils;

import gr.uom.java.xmi.LocationInfo.CodeElementType;
import gr.uom.java.xmi.LeafType;
import gr.uom.java.xmi.UMLAnonymousClass;
import gr.uom.java.xmi.UMLAttribute;
import gr.uom.java.xmi.UMLClass;
Expand Down Expand Up @@ -643,6 +644,7 @@ else if(after.startsWith(variableName + " ") && initializer != null) {
overlappingExtractVariable(initializer, before, nonMappedLeavesT2, insideExtractedOrInlinedMethod, refactorings) ||
(initializer.toString().equals("(" + declaration.getType() + ")" + before) && !containsVariableNameReplacement(variableName)) ||
ternaryMatch(initializer, before) ||
fieldAccessReplacedWithGetter(initializer, before) ||
infixOperandMatch(initializer, before) ||
wrappedAsArgument(initializer, before) ||
stringConcatMatch(initializer, before) ||
Expand Down Expand Up @@ -1039,6 +1041,7 @@ else if(before.startsWith("()" + JAVA.LAMBDA_ARROW + variableName + ".")) {
overlappingExtractVariable(initializer, after, nonMappedLeavesT2, insideExtractedOrInlinedMethod, refactorings) ||
(initializer.toString().equals("(" + declaration.getType() + ")" + after) && !containsVariableNameReplacement(variableName)) ||
ternaryMatch(initializer, after) ||
fieldAccessReplacedWithGetter(initializer, after) ||
infixOperandMatch(initializer, after) ||
wrappedAsArgument(initializer, after) ||
stringConcatMatch(initializer, after) ||
Expand Down Expand Up @@ -1325,6 +1328,44 @@ private boolean infixOperandMatch(AbstractExpression initializer, String replace
return false;
}

private boolean fieldAccessReplacedWithGetter(AbstractExpression initializer, String replacedExpression) {
String[] tokens1 = LeafType.CAMEL_CASE_SPLIT_PATTERN.split(initializer.getString());
String[] tokens2 = LeafType.CAMEL_CASE_SPLIT_PATTERN.split(replacedExpression);
if(tokens1.length == tokens2.length) {
int matchingTokens = 0;
for(int i=0; i<tokens1.length; i++) {
String token1 = tokens1[i];
String token2 = tokens2[i];
if(token1.equals(token2)) {
matchingTokens++;
}
else if(token1.contains(".") && token2.contains(".")) {
String prefix1 = token1.substring(0, token1.indexOf("."));
String prefix2 = token2.substring(0, token2.indexOf("."));
if(prefix1.equals(prefix2)) {
matchingTokens++;
}
}
else if(token1.endsWith("()")) {
String prefix1 = token1.substring(0, token1.length()-2);
if(prefix1.equals(token2)) {
matchingTokens++;
}
}
else if(token2.endsWith("()")) {
String prefix2 = token2.substring(0, token2.length()-2);
if(prefix2.equals(token1)) {
matchingTokens++;
}
}
}
if(matchingTokens == tokens1.length) {
return true;
}
}
return false;
}

private boolean ternaryMatch(AbstractExpression initializer, String replacedExpression) {
List<TernaryOperatorExpression> ternaryList = initializer.getTernaryOperatorExpressions();
for(TernaryOperatorExpression ternary : ternaryList) {
Expand Down

0 comments on commit 8dc4884

Please sign in to comment.