mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
better fix for IDEA-82335 (Replace ?: with if breaks the semantics)
This commit is contained in:
+23
-49
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -36,25 +36,19 @@ public class ReplaceConditionalWithIfIntention extends Intention {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processIntention(@NotNull PsiElement element)
|
||||
throws IncorrectOperationException {
|
||||
final PsiConditionalExpression expression =
|
||||
(PsiConditionalExpression)element;
|
||||
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
|
||||
final PsiConditionalExpression expression = (PsiConditionalExpression)element;
|
||||
replaceConditionalWithIf(expression);
|
||||
}
|
||||
|
||||
private static void replaceConditionalWithIf(
|
||||
PsiConditionalExpression expression)
|
||||
throws IncorrectOperationException {
|
||||
final PsiStatement statement =
|
||||
PsiTreeUtil.getParentOfType(expression, PsiStatement.class);
|
||||
private static void replaceConditionalWithIf(PsiConditionalExpression expression) throws IncorrectOperationException {
|
||||
final PsiStatement statement = PsiTreeUtil.getParentOfType(expression, PsiStatement.class);
|
||||
if (statement == null) {
|
||||
return;
|
||||
}
|
||||
final PsiVariable variable;
|
||||
if (statement instanceof PsiDeclarationStatement) {
|
||||
variable =
|
||||
PsiTreeUtil.getParentOfType(expression, PsiVariable.class);
|
||||
variable = PsiTreeUtil.getParentOfType(expression, PsiVariable.class);
|
||||
}
|
||||
else {
|
||||
variable = null;
|
||||
@@ -62,8 +56,7 @@ public class ReplaceConditionalWithIfIntention extends Intention {
|
||||
final PsiExpression thenExpression = expression.getThenExpression();
|
||||
final PsiExpression elseExpression = expression.getElseExpression();
|
||||
final PsiExpression condition = expression.getCondition();
|
||||
final PsiExpression strippedCondition =
|
||||
ParenthesesUtils.stripParentheses(condition);
|
||||
final PsiExpression strippedCondition = ParenthesesUtils.stripParentheses(condition);
|
||||
final StringBuilder newStatement = new StringBuilder();
|
||||
newStatement.append("if(");
|
||||
if (strippedCondition != null) {
|
||||
@@ -78,36 +71,29 @@ public class ReplaceConditionalWithIfIntention extends Intention {
|
||||
if (initializer == null) {
|
||||
return;
|
||||
}
|
||||
appendElementTextWithoutParentheses(initializer, expression,
|
||||
thenExpression, newStatement);
|
||||
appendElementTextWithoutParentheses(initializer, expression, thenExpression, newStatement);
|
||||
newStatement.append("; else ");
|
||||
newStatement.append(name);
|
||||
newStatement.append('=');
|
||||
appendElementTextWithoutParentheses(initializer, expression,
|
||||
elseExpression, newStatement);
|
||||
appendElementTextWithoutParentheses(initializer, expression, elseExpression, newStatement);
|
||||
newStatement.append(';');
|
||||
initializer.delete();
|
||||
final PsiManager manager = statement.getManager();
|
||||
final Project project = manager.getProject();
|
||||
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
|
||||
final PsiElementFactory factory = facade.getElementFactory();
|
||||
final PsiStatement ifStatement = factory.createStatementFromText(
|
||||
newStatement.toString(), statement);
|
||||
final PsiStatement ifStatement = factory.createStatementFromText(newStatement.toString(), statement);
|
||||
final PsiElement parent = statement.getParent();
|
||||
final PsiElement addedElement = parent.addAfter(ifStatement,
|
||||
statement);
|
||||
final PsiElement addedElement = parent.addAfter(ifStatement, statement);
|
||||
final CodeStyleManager styleManager = CodeStyleManager.getInstance(manager.getProject());
|
||||
styleManager.reformat(addedElement);
|
||||
}
|
||||
else {
|
||||
final PsiElement expressionParent = expression.getParent();
|
||||
final boolean addBraces =
|
||||
expressionParent instanceof PsiIfStatement;
|
||||
final boolean addBraces = PsiTreeUtil.getParentOfType(expression, PsiIfStatement.class, true, PsiStatement.class) != null;
|
||||
if (addBraces || thenExpression == null) {
|
||||
newStatement.append('{');
|
||||
}
|
||||
appendElementTextWithoutParentheses(statement, expression,
|
||||
thenExpression, newStatement);
|
||||
appendElementTextWithoutParentheses(statement, expression, thenExpression, newStatement);
|
||||
if (addBraces) {
|
||||
newStatement.append("} else {");
|
||||
}
|
||||
@@ -120,8 +106,7 @@ public class ReplaceConditionalWithIfIntention extends Intention {
|
||||
newStatement.append('{');
|
||||
}
|
||||
}
|
||||
appendElementTextWithoutParentheses(statement, expression,
|
||||
elseExpression, newStatement);
|
||||
appendElementTextWithoutParentheses(statement, expression, elseExpression, newStatement);
|
||||
if (addBraces || elseExpression == null) {
|
||||
newStatement.append('}');
|
||||
}
|
||||
@@ -129,33 +114,23 @@ public class ReplaceConditionalWithIfIntention extends Intention {
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendElementTextWithoutParentheses(
|
||||
@NotNull PsiElement element,
|
||||
@NotNull PsiElement elementToReplace,
|
||||
@Nullable PsiExpression replacementExpression,
|
||||
@NotNull StringBuilder out) {
|
||||
private static void appendElementTextWithoutParentheses(@NotNull PsiElement element, @NotNull PsiElement elementToReplace,
|
||||
@Nullable PsiExpression replacementExpression, @NotNull StringBuilder out) {
|
||||
final PsiElement expressionParent = elementToReplace.getParent();
|
||||
if (expressionParent instanceof PsiParenthesizedExpression) {
|
||||
final PsiElement grandParent = expressionParent.getParent();
|
||||
if (!ParenthesesUtils.areParenthesesNeeded(replacementExpression,
|
||||
grandParent, true)) {
|
||||
appendElementText(element, expressionParent,
|
||||
replacementExpression, out);
|
||||
if (!ParenthesesUtils.areParenthesesNeeded(replacementExpression, grandParent, true)) {
|
||||
appendElementText(element, expressionParent, replacementExpression, out);
|
||||
return;
|
||||
}
|
||||
}
|
||||
appendElementText(element, elementToReplace, replacementExpression,
|
||||
out);
|
||||
appendElementText(element, elementToReplace, replacementExpression, out);
|
||||
}
|
||||
|
||||
private static void appendElementText(
|
||||
@NotNull PsiElement element,
|
||||
@NotNull PsiElement elementToReplace,
|
||||
@Nullable PsiExpression replacementExpression,
|
||||
@NotNull StringBuilder out) {
|
||||
private static void appendElementText(@NotNull PsiElement element, @NotNull PsiElement elementToReplace,
|
||||
@Nullable PsiExpression replacementExpression, @NotNull StringBuilder out) {
|
||||
if (element.equals(elementToReplace)) {
|
||||
final String replacementText = (replacementExpression == null) ?
|
||||
"" : replacementExpression.getText();
|
||||
final String replacementText = (replacementExpression == null) ? "" : replacementExpression.getText();
|
||||
out.append(replacementText);
|
||||
return;
|
||||
}
|
||||
@@ -172,8 +147,7 @@ public class ReplaceConditionalWithIfIntention extends Intention {
|
||||
return;
|
||||
}
|
||||
for (PsiElement child : children) {
|
||||
appendElementText(child, elementToReplace, replacementExpression,
|
||||
out);
|
||||
appendElementText(child, elementToReplace, replacementExpression, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-15
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,39 +25,32 @@ class ReplaceConditionalWithIfPredicate implements PsiElementPredicate {
|
||||
if (!(element instanceof PsiConditionalExpression)) {
|
||||
return false;
|
||||
}
|
||||
final PsiConditionalExpression conditionalExpression =
|
||||
(PsiConditionalExpression)element;
|
||||
final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)element;
|
||||
if (JspPsiUtil.isInJspFile(element)) {
|
||||
final PsiExpression thenExpression =
|
||||
conditionalExpression.getThenExpression();
|
||||
final PsiExpression thenExpression = conditionalExpression.getThenExpression();
|
||||
if (thenExpression == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiExpression elseExpression =
|
||||
conditionalExpression.getElseExpression();
|
||||
final PsiExpression elseExpression = conditionalExpression.getElseExpression();
|
||||
if (elseExpression == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
final PsiElement parent = conditionalExpression.getParent();
|
||||
if (parent instanceof PsiExpressionStatement || parent instanceof PsiExpressionList) {
|
||||
if (parent instanceof PsiExpressionStatement) {
|
||||
return false;
|
||||
}
|
||||
final PsiMember member = PsiTreeUtil.getParentOfType(element,
|
||||
PsiMember.class);
|
||||
final PsiMember member = PsiTreeUtil.getParentOfType(element, PsiMember.class);
|
||||
if (member instanceof PsiMethod) {
|
||||
final PsiMethod method = (PsiMethod)member;
|
||||
if (!method.isConstructor()) {
|
||||
return true;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression =
|
||||
PsiTreeUtil.getParentOfType(element,
|
||||
PsiMethodCallExpression.class);
|
||||
final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
|
||||
if (methodCallExpression == null) {
|
||||
return true;
|
||||
}
|
||||
final PsiReferenceExpression methodExpression =
|
||||
methodCallExpression.getMethodExpression();
|
||||
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
|
||||
final String methodName = methodExpression.getReferenceName();
|
||||
return !"super".equals(methodName);
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,10 +1,10 @@
|
||||
class A {
|
||||
boolean g(Class c) {return false;}
|
||||
|
||||
|
||||
boolean f() {
|
||||
if (g(this instanceof A <caret>? A.class : Object.class))
|
||||
return true;
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class A {
|
||||
boolean g(Class c) {return false;}
|
||||
|
||||
boolean f() {
|
||||
if (this instanceof A) {
|
||||
if (g(A.class))
|
||||
return true;
|
||||
} else {
|
||||
if (g(Object.class))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 Bas Leijdekkers
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,7 @@ import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
public class ReplaceConditionalWithIfTest extends IPPTestCase {
|
||||
public void testConditionalAsArgument() { assertIntentionNotAvailable(); }
|
||||
public void testConditionalAsArgument() { doTest(); }
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message(
|
||||
|
||||
Reference in New Issue
Block a user