ExpressionUtils#rename* -> bind*to (IDEA-CR-18002)

This commit is contained in:
Tagir Valeev
2017-02-13 11:50:37 +07:00
parent 60e21e73d3
commit d30ba61324
9 changed files with 18 additions and 18 deletions
@@ -244,7 +244,7 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
ct.replace(e, replacement);
}
ct.replace(nextParameters[0], ct.markUnchanged(prevParameters[0]));
ExpressionUtils.renameReference(nextRef, newName);
ExpressionUtils.bindReferenceTo(nextRef, newName);
PsiExpression prevQualifier = mapCall.getMethodExpression().getQualifierExpression();
if(prevQualifier == null) {
ct.deleteAndRestoreComments(nextQualifier);
@@ -138,7 +138,7 @@ public class MergeFilterChainAction extends PsiElementBaseIntentionAction {
for (PsiReference reference : ReferencesSearch.search(sourceLambdaParams[0]).findAll()) {
final PsiElement referenceElement = reference.getElement();
if (referenceElement instanceof PsiReferenceExpression) {
ExpressionUtils.renameReference((PsiReferenceExpression)referenceElement, name);
ExpressionUtils.bindReferenceTo((PsiReferenceExpression)referenceElement, name);
}
}
}
@@ -110,7 +110,7 @@ public class ExcessiveLambdaUsageInspection extends BaseJavaBatchLocalInspection
PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(lambda, PsiMethodCallExpression.class);
if(call == null) return;
ExpressionUtils.renameCall(call, myInfo.getTargetName(call));
ExpressionUtils.bindCallTo(call, myInfo.getTargetName(call));
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(lambda, ct.text(body));
}
@@ -159,7 +159,7 @@ public class ReplaceInefficientStreamCountInspection extends BaseJavaBatchLocalI
private static void replaceSimpleCount(PsiMethodCallExpression countCall, PsiMethodCallExpression qualifierCall) {
if (!COLLECTION_STREAM.test(qualifierCall)) return;
PsiReferenceExpression methodExpression = qualifierCall.getMethodExpression();
ExpressionUtils.renameCall(qualifierCall, SIZE_METHOD);
ExpressionUtils.bindCallTo(qualifierCall, SIZE_METHOD);
boolean addCast = true;
PsiElement toReplace = countCall;
PsiElement parent = PsiUtil.skipParenthesizedExprUp(countCall.getParent());
@@ -422,7 +422,7 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
if (collectionExpression == null) return null;
collectionStreamCall.replace(collectionExpression);
if (!myStreamMethod.equals(myCollectionMethod)) {
ExpressionUtils.renameCall(streamMethodCall, myCollectionMethod);
ExpressionUtils.bindCallTo(streamMethodCall, myCollectionMethod);
}
return streamMethodCall;
}
@@ -609,7 +609,7 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
}
if (removeParentNegation && !isParentNegated(methodCall)) return null;
if (removeLambdaNegation && !isArgumentLambdaNegated(methodCall)) return null;
ExpressionUtils.renameCall(methodCall, myTo);
ExpressionUtils.bindCallTo(methodCall, myTo);
if (removeLambdaNegation) {
// Casts and array bounds already checked in isArgumentLambdaNegated
PsiExpression body = (PsiExpression)((PsiLambdaExpression)methodCall.getArgumentList().getExpressions()[0]).getBody();
@@ -737,7 +737,7 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
public PsiElement simplify(PsiMethodCallExpression call) {
PsiExpression[] args = call.getArgumentList().getExpressions();
if (args.length != 1) return null;
ExpressionUtils.renameCall(call, "boxed");
ExpressionUtils.bindCallTo(call, "boxed");
args[0].delete();
call.getTypeArgumentList().delete();
return call;
@@ -1034,7 +1034,7 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
if (replacement == null) return null;
ct.replace(arg, replacement);
}
ExpressionUtils.renameCall(qualifier, name);
ExpressionUtils.bindCallTo(qualifier, name);
return ct.replaceAndRestoreComments(call, ct.markUnchanged(qualifier));
}
@@ -1136,7 +1136,7 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
if (args.length != 2) return null;
PsiMethodCallExpression spliteratorCall = tryCast(PsiUtil.skipParenthesizedExprDown(args[0]), PsiMethodCallExpression.class);
if (spliteratorCall == null) return null;
ExpressionUtils.renameCall(spliteratorCall, getMethodName());
ExpressionUtils.bindCallTo(spliteratorCall, getMethodName());
CommentTracker ct = new CommentTracker();
return ct.replace(call, spliteratorCall);
}
@@ -406,7 +406,7 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
CommentTracker ct = new CommentTracker();
ExpressionUtils.renameCall(call, myMethodName);
ExpressionUtils.bindCallTo(call, myMethodName);
PsiExpression replacement;
if(myMethodName.equals("computeIfAbsent")) {
PsiExpression key = args[0];
@@ -432,7 +432,7 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
}
String varName = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(nameCandidate, value, true);
for(PsiReferenceExpression ref : refs) {
ExpressionUtils.renameReference(ref, varName);
ExpressionUtils.bindReferenceTo(ref, varName);
}
replacement = factory.createExpressionFromText(varName + " -> " + ct.text(value), value);
} else if (myMethodName.equals("merge")) {
@@ -190,7 +190,7 @@ public class Java8MapForEachInspection extends BaseJavaBatchLocalInspectionTool
(PsiLambdaExpression)factory.createExpressionFromText("(" + key.myName + "," + value.myName + ")->" + lambdaBody, body);
LambdaRefactoringUtil.simplifyToExpressionLambda(newLambda);
entrySetCall.getArgumentList().add(newLambda);
ExpressionUtils.renameCall(entrySetCall, "forEach");
ExpressionUtils.bindCallTo(entrySetCall, "forEach");
return entrySetCall.getText();
}
@@ -362,7 +362,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
}
}
if (variable != builderVariable) {
ExpressionUtils.renameReference(ref, Objects.requireNonNull(builderVariable.getName()));
ExpressionUtils.bindReferenceTo(ref, Objects.requireNonNull(builderVariable.getName()));
}
PsiMethodCallExpression methodCallExpression = ExpressionUtils.getCallForQualifier(ref);
if(methodCallExpression != null) {
@@ -938,12 +938,12 @@ public class ExpressionUtils {
}
/**
* Rename reference element. The qualifier and type arguments (if present) remain the same
* Bind a reference element to a new name. The qualifier and type arguments (if present) remain the same
*
* @param ref reference element to rename
* @param newName new name
*/
public static void renameReference(@NotNull PsiReferenceExpression ref, @NotNull String newName) {
public static void bindReferenceTo(@NotNull PsiReferenceExpression ref, @NotNull String newName) {
PsiElement nameElement = ref.getReferenceNameElement();
if(nameElement == null) {
throw new IllegalStateException("Name element is null: "+ref);
@@ -954,12 +954,12 @@ public class ExpressionUtils {
}
/**
* Rename method call. Everything else like qualifier, type arguments or call arguments remain the same.
* Bind method call to a new name. Everything else like qualifier, type arguments or call arguments remain the same.
*
* @param call to rename
* @param newName new name
*/
public static void renameCall(@NotNull PsiMethodCallExpression call, @NotNull String newName) {
renameReference(call.getMethodExpression(), newName);
public static void bindCallTo(@NotNull PsiMethodCallExpression call, @NotNull String newName) {
bindReferenceTo(call.getMethodExpression(), newName);
}
}