Remove ParenthesesUtils duplication

This commit is contained in:
Bas Leijdekkers
2013-07-18 14:11:55 +02:00
parent fb5677f2a7
commit 5c5df8707d
27 changed files with 77 additions and 541 deletions
@@ -89,7 +89,7 @@ public class ParenthesesUtils {
return expression;
}
private static boolean isCommutativeOperator(@NotNull IElementType token) {
public static boolean isCommutativeOperator(@NotNull IElementType token) {
return !(token.equals(JavaTokenType.MINUS) ||
token.equals(JavaTokenType.DIV) ||
token.equals(JavaTokenType.PERC) ||
@@ -173,7 +173,7 @@ public class ParenthesesUtils {
return -1;
}
private static int getPrecedenceForOperator(@NotNull IElementType operator) {
public static int getPrecedenceForOperator(@NotNull IElementType operator) {
final Integer precedence = s_binaryOperatorPrecedence.get(operator);
if (precedence == null) {
throw new IllegalArgumentException("unknown operator: " + operator);
@@ -454,8 +454,8 @@ public class ParenthesesUtils {
return areParenthesesNeeded(child, (PsiExpression)parent, ignoreClarifyingParentheses);
}
private static boolean areParenthesesNeeded(PsiExpression expression, PsiExpression parentExpression,
boolean ignoreClarifyingParentheses) {
public static boolean areParenthesesNeeded(PsiExpression expression, PsiExpression parentExpression,
boolean ignoreClarifyingParentheses) {
if (parentExpression instanceof PsiParenthesizedExpression || parentExpression instanceof PsiArrayInitializerExpression) {
return false;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -26,9 +26,9 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiUtilCore;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.psiutils.BoolUtils;
import com.siyeh.ipp.psiutils.ComparisonUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -19,11 +19,11 @@ import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.BoolUtils;
import com.siyeh.ipp.psiutils.ComparisonUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
public class DemorgansIntention extends MutablyNamedIntention {
@@ -52,9 +52,8 @@ public class DemorgansIntention extends MutablyNamedIntention {
private static String convertConjunctionExpression(PsiPolyadicExpression polyadicExpression) {
final IElementType tokenType = polyadicExpression.getOperationTokenType();
final String flippedConjunction;
final boolean tokenTypeAndAnd = tokenType.equals(JavaTokenType.ANDAND);
flippedConjunction = tokenTypeAndAnd ? "||" : "&&";
final String flippedConjunction = tokenTypeAndAnd ? "||" : "&&";
final StringBuilder result = new StringBuilder();
for (PsiExpression operand : polyadicExpression.getOperands()) {
if (result.length() != 0) {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2013 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.
@@ -17,12 +17,12 @@ package com.siyeh.ipp.commutative;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.siyeh.IntentionPowerPackBundle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class FlipCommutativeMethodCallIntention extends MutablyNamedIntention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -19,10 +19,10 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConcatenationUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2006 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -18,9 +18,9 @@ package com.siyeh.ipp.conditional;
import com.intellij.psi.PsiConditionalExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ErrorUtil;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
class RemoveConditionalPredicate implements PsiElementPredicate {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -17,13 +17,13 @@ package com.siyeh.ipp.conditional;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -120,7 +120,7 @@ public class ReplaceConditionalWithIfIntention extends Intention {
if (expressionParent instanceof PsiParenthesizedExpression) {
final PsiElement grandParent = expressionParent.getParent();
if (replacementExpression == null || !(grandParent instanceof PsiExpression) ||
!ParenthesesUtils.areParenthesesNeeded(replacementExpression, (PsiExpression) grandParent)) {
!ParenthesesUtils.areParenthesesNeeded(replacementExpression, (PsiExpression) grandParent, false)) {
appendElementText(element, expressionParent, replacementExpression, out);
return;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2013 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.
@@ -15,14 +15,17 @@
*/
package com.siyeh.ipp.equality;
import com.intellij.psi.*;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ReplaceEqualityWithEqualsIntention extends Intention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2013 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.
@@ -18,9 +18,9 @@ package com.siyeh.ipp.equality;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2013 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.
@@ -17,9 +17,9 @@ package com.siyeh.ipp.equality;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
public class ReplaceEqualsWithEqualityIntention extends Intention {
@@ -26,7 +26,7 @@ import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConcatenationUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
public class FlipExpressionIntention extends MutablyNamedIntention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -21,9 +21,9 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2010 Bas Leijdekkers
* Copyright 2009-2013 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,16 +15,16 @@
*/
package com.siyeh.ipp.forloop;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ExpressionUtils;
import com.siyeh.ipp.psiutils.VariableAccessUtils;
import com.siyeh.ipp.psiutils.ComparisonUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.intellij.openapi.project.Project;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ComparisonUtils;
import com.siyeh.ipp.psiutils.ExpressionUtils;
import com.siyeh.ipp.psiutils.VariableAccessUtils;
import org.jetbrains.annotations.NotNull;
public class ReverseForLoopDirectionIntention extends Intention {
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2012 Bas Leijdekkers
* Copyright 2009-2013 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,11 +15,11 @@
*/
package com.siyeh.ipp.opassign;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.siyeh.ipp.psiutils.VariableAccessUtils;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.VariableAccessUtils;
class ReplaceAssignmentWithPostfixExpressionPredicate implements PsiElementPredicate {
@@ -19,9 +19,9 @@ import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.TypeConversionUtil;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -17,9 +17,9 @@ package com.siyeh.ipp.parenthesis;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
public class RemoveUnnecessaryParenthesesIntention extends Intention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -16,8 +16,8 @@
package com.siyeh.ipp.parenthesis;
import com.intellij.psi.*;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
class UnnecessaryParenthesesPredicate implements PsiElementPredicate {
@@ -34,6 +34,6 @@ class UnnecessaryParenthesesPredicate implements PsiElementPredicate {
return false;
}
final PsiParenthesizedExpression expression = (PsiParenthesizedExpression)element;
return !ParenthesesUtils.areParenthesesNeeded(expression);
return !ParenthesesUtils.areParenthesesNeeded(expression, false);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -17,6 +17,7 @@ package com.siyeh.ipp.psiutils;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
@@ -1,470 +0,0 @@
/*
* Copyright 2003-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.psiutils;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
public class ParenthesesUtils {
private ParenthesesUtils() {}
public static final int PARENTHESIZED_PRECEDENCE = 0;
public static final int LITERAL_PRECEDENCE = 0;
public static final int METHOD_CALL_PRECEDENCE = 1;
public static final int POSTFIX_PRECEDENCE = 2;
public static final int PREFIX_PRECEDENCE = 3;
public static final int TYPE_CAST_PRECEDENCE = 4;
public static final int MULTIPLICATIVE_PRECEDENCE = 5;
public static final int ADDITIVE_PRECEDENCE = 6;
public static final int SHIFT_PRECEDENCE = 7;
public static final int RELATIONAL_PRECEDENCE = 8;
public static final int EQUALITY_PRECEDENCE = 9;
public static final int BINARY_AND_PRECEDENCE = 10;
public static final int BINARY_XOR_PRECEDENCE = 11;
public static final int BINARY_OR_PRECEDENCE = 12;
public static final int AND_PRECEDENCE = 13;
public static final int OR_PRECEDENCE = 14;
public static final int CONDITIONAL_PRECEDENCE = 15;
public static final int ASSIGNMENT_PRECEDENCE = 16;
public static final int NUM_PRECEDENCES = 17;
private static final Map<IElementType, Integer> s_binaryOperatorPrecedence = new HashMap<IElementType, Integer>(NUM_PRECEDENCES);
static {
s_binaryOperatorPrecedence.put(JavaTokenType.PLUS, ADDITIVE_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.MINUS, ADDITIVE_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.ASTERISK, MULTIPLICATIVE_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.DIV, MULTIPLICATIVE_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.PERC, MULTIPLICATIVE_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.ANDAND, AND_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.OROR, OR_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.AND, BINARY_AND_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.OR, BINARY_OR_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.XOR, BINARY_XOR_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.LTLT, SHIFT_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.GTGT, SHIFT_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.GTGTGT, SHIFT_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.GT, RELATIONAL_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.GE, RELATIONAL_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.LT, RELATIONAL_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.LE, RELATIONAL_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.EQEQ, EQUALITY_PRECEDENCE);
s_binaryOperatorPrecedence.put(JavaTokenType.NE, EQUALITY_PRECEDENCE);
}
@Nullable
public static PsiExpression stripParentheses(@Nullable PsiExpression expression) {
while (expression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression;
expression = parenthesizedExpression.getExpression();
}
return expression;
}
public static boolean isCommutativeOperator(@NotNull IElementType token) {
return !(token.equals(JavaTokenType.MINUS) ||
token.equals(JavaTokenType.DIV) ||
token.equals(JavaTokenType.PERC) ||
token.equals(JavaTokenType.LTLT) ||
token.equals(JavaTokenType.GTGT) ||
token.equals(JavaTokenType.GTGTGT));
}
public static int getPrecedence(PsiExpression expression) {
if (expression instanceof PsiThisExpression ||
expression instanceof PsiLiteralExpression ||
expression instanceof PsiSuperExpression ||
expression instanceof PsiClassObjectAccessExpression ||
expression instanceof PsiArrayAccessExpression ||
expression instanceof PsiArrayInitializerExpression) {
return LITERAL_PRECEDENCE;
}
if (expression instanceof PsiReferenceExpression) {
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression;
if (referenceExpression.getQualifier() != null) {
return METHOD_CALL_PRECEDENCE;
}
else {
return LITERAL_PRECEDENCE;
}
}
if (expression instanceof PsiMethodCallExpression || expression instanceof PsiNewExpression) {
return METHOD_CALL_PRECEDENCE;
}
if (expression instanceof PsiTypeCastExpression) {
return TYPE_CAST_PRECEDENCE;
}
if (expression instanceof PsiPrefixExpression) {
return PREFIX_PRECEDENCE;
}
if (expression instanceof PsiPostfixExpression) {
return POSTFIX_PRECEDENCE;
}
if (expression instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
return getPrecedenceForOperator(polyadicExpression.getOperationTokenType());
}
if (expression instanceof PsiInstanceOfExpression) {
return RELATIONAL_PRECEDENCE;
}
if (expression instanceof PsiConditionalExpression) {
return CONDITIONAL_PRECEDENCE;
}
if (expression instanceof PsiAssignmentExpression) {
return ASSIGNMENT_PRECEDENCE;
}
if (expression instanceof PsiParenthesizedExpression) {
return PARENTHESIZED_PRECEDENCE;
}
return -1;
}
public static int getPrecedenceForOperator(@NotNull IElementType operator) {
final Integer precedence = s_binaryOperatorPrecedence.get(operator);
if (precedence == null) {
throw new IllegalArgumentException("unknown operator: " + operator);
}
return precedence.intValue();
}
public static void removeParentheses(@NotNull PsiExpression expression, boolean ignoreClarifyingParentheses) {
if (expression instanceof PsiMethodCallExpression) {
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expression;
removeParensFromMethodCallExpression(methodCall, ignoreClarifyingParentheses);
}
if (expression instanceof PsiReferenceExpression) {
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression;
removeParensFromReferenceExpression(referenceExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiNewExpression) {
final PsiNewExpression newExpression = (PsiNewExpression)expression;
removeParensFromNewExpression(newExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiAssignmentExpression) {
final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expression;
removeParensFromAssignmentExpression(assignmentExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiArrayInitializerExpression) {
final PsiArrayInitializerExpression arrayInitializerExpression = (PsiArrayInitializerExpression)expression;
removeParensFromArrayInitializerExpression(arrayInitializerExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiTypeCastExpression) {
final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)expression;
removeParensFromTypeCastExpression(typeCastExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiArrayAccessExpression) {
final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression)expression;
removeParensFromArrayAccessExpression(arrayAccessExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression)expression;
removeParensFromPrefixExpression(prefixExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiPostfixExpression) {
final PsiPostfixExpression postfixExpression = (PsiPostfixExpression)expression;
removeParensFromPostfixExpression(postfixExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
removeParensFromPolyadicExpression(polyadicExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiInstanceOfExpression) {
final PsiInstanceOfExpression instanceofExpression = (PsiInstanceOfExpression)expression;
removeParensFromInstanceOfExpression(instanceofExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiConditionalExpression) {
final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)expression;
removeParensFromConditionalExpression(conditionalExpression, ignoreClarifyingParentheses);
}
if (expression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression;
removeParensFromParenthesizedExpression(parenthesizedExpression, ignoreClarifyingParentheses);
}
}
private static void removeParensFromReferenceExpression(@NotNull PsiReferenceExpression referenceExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression qualifier = referenceExpression.getQualifierExpression();
if (qualifier != null) {
removeParentheses(qualifier, ignoreClarifyingParentheses);
}
}
private static void removeParensFromParenthesizedExpression(@NotNull PsiParenthesizedExpression parenthesizedExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression body = parenthesizedExpression.getExpression();
if (body == null) {
parenthesizedExpression.delete();
return;
}
final PsiElement parent = parenthesizedExpression.getParent();
if (!(parent instanceof PsiExpression) || parent instanceof PsiParenthesizedExpression ||
parent instanceof PsiArrayInitializerExpression) {
final PsiExpression newExpression = (PsiExpression)parenthesizedExpression.replace(body);
removeParentheses(newExpression, ignoreClarifyingParentheses);
return;
}
else if (parent instanceof PsiArrayAccessExpression) {
final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression)parent;
if (parenthesizedExpression == arrayAccessExpression.getIndexExpression()) {
// use addAfter() + delete() instead of replace() to
// workaround automatic insertion of parentheses by psi
final PsiExpression newExpression = (PsiExpression)parent.addAfter(body, parenthesizedExpression);
parenthesizedExpression.delete();
removeParentheses(newExpression, ignoreClarifyingParentheses);
return;
}
}
final PsiExpression parentExpression = (PsiExpression)parent;
final int parentPrecedence = getPrecedence(parentExpression);
final int childPrecedence = getPrecedence(body);
if (parentPrecedence < childPrecedence) {
final PsiElement bodyParent = body.getParent();
final PsiParenthesizedExpression newParenthesizedExpression = (PsiParenthesizedExpression)parenthesizedExpression.replace(bodyParent);
final PsiExpression expression = newParenthesizedExpression.getExpression();
if (expression != null) {
removeParentheses(expression, ignoreClarifyingParentheses);
}
}
else if (parentPrecedence == childPrecedence) {
if (parentExpression instanceof PsiPolyadicExpression && body instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression parentPolyadicExpression = (PsiPolyadicExpression)parentExpression;
final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType();
final PsiPolyadicExpression bodyPolyadicExpression = (PsiPolyadicExpression)body;
final IElementType bodyOperator = bodyPolyadicExpression.getOperationTokenType();
final PsiType parentType = parentPolyadicExpression.getType();
final PsiType bodyType = body.getType();
if (parentType != null && parentType.equals(bodyType) && parentOperator.equals(bodyOperator)) {
final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands();
if (PsiTreeUtil.isAncestor(parentOperands[0], body, true) || isCommutativeOperator(bodyOperator)) {
// use addAfter() + delete() instead of replace() to
// workaround automatic insertion of parentheses by psi
final PsiExpression newExpression = (PsiExpression)parent.addAfter(body, parenthesizedExpression);
parenthesizedExpression.delete();
removeParentheses(newExpression, ignoreClarifyingParentheses);
return;
}
}
if (ignoreClarifyingParentheses) {
if (parentOperator.equals(bodyOperator)) {
removeParentheses(body, ignoreClarifyingParentheses);
}
}
else {
final PsiExpression newExpression = (PsiExpression)parenthesizedExpression.replace(body);
removeParentheses(newExpression, ignoreClarifyingParentheses);
}
}
else {
final PsiExpression newExpression = (PsiExpression)parenthesizedExpression.replace(body);
removeParentheses(newExpression, ignoreClarifyingParentheses);
}
}
else {
if (ignoreClarifyingParentheses && parent instanceof PsiPolyadicExpression &&
(body instanceof PsiPolyadicExpression || body instanceof PsiInstanceOfExpression)) {
removeParentheses(body, ignoreClarifyingParentheses);
}
else {
final PsiExpression newExpression = (PsiExpression)parenthesizedExpression.replace(body);
removeParentheses(newExpression, ignoreClarifyingParentheses);
}
}
}
private static void removeParensFromConditionalExpression(@NotNull PsiConditionalExpression conditionalExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression condition = conditionalExpression.getCondition();
removeParentheses(condition, ignoreClarifyingParentheses);
final PsiExpression thenBranch = conditionalExpression.getThenExpression();
if (thenBranch != null) {
removeParentheses(thenBranch, ignoreClarifyingParentheses);
}
final PsiExpression elseBranch = conditionalExpression.getElseExpression();
if (elseBranch != null) {
removeParentheses(elseBranch, ignoreClarifyingParentheses);
}
}
private static void removeParensFromInstanceOfExpression(@NotNull PsiInstanceOfExpression instanceofExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression operand = instanceofExpression.getOperand();
removeParentheses(operand, ignoreClarifyingParentheses);
}
private static void removeParensFromPolyadicExpression(@NotNull PsiPolyadicExpression polyadicExpression,
boolean ignoreClarifyingParentheses) {
for (PsiExpression operand : polyadicExpression.getOperands()) {
removeParentheses(operand, ignoreClarifyingParentheses);
}
}
private static void removeParensFromPostfixExpression(@NotNull PsiPostfixExpression postfixExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression operand = postfixExpression.getOperand();
removeParentheses(operand, ignoreClarifyingParentheses);
}
private static void removeParensFromPrefixExpression(@NotNull PsiPrefixExpression prefixExpression, boolean ignoreClarifyingParentheses) {
final PsiExpression operand = prefixExpression.getOperand();
if (operand != null) {
removeParentheses(operand, ignoreClarifyingParentheses);
}
}
private static void removeParensFromArrayAccessExpression(@NotNull PsiArrayAccessExpression arrayAccessExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression();
removeParentheses(arrayExpression, ignoreClarifyingParentheses);
final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression();
if (indexExpression != null) {
removeParentheses(indexExpression, ignoreClarifyingParentheses);
}
}
private static void removeParensFromTypeCastExpression(@NotNull PsiTypeCastExpression typeCastExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression operand = typeCastExpression.getOperand();
if (operand != null) {
removeParentheses(operand, ignoreClarifyingParentheses);
}
}
private static void removeParensFromArrayInitializerExpression(@NotNull PsiArrayInitializerExpression arrayInitializerExpression,
boolean ignoreClarifyingParentheses) {
final PsiExpression[] initializers = arrayInitializerExpression.getInitializers();
for (final PsiExpression initializer : initializers) {
removeParentheses(initializer, ignoreClarifyingParentheses);
}
}
private static void removeParensFromAssignmentExpression(@NotNull PsiAssignmentExpression assignment,
boolean ignoreClarifyingParentheses) {
final PsiExpression lhs = assignment.getLExpression();
final PsiExpression rhs = assignment.getRExpression();
removeParentheses(lhs, ignoreClarifyingParentheses);
if (rhs != null) {
removeParentheses(rhs, ignoreClarifyingParentheses);
}
}
private static void removeParensFromNewExpression(@NotNull PsiNewExpression newExpression, boolean ignoreClarifyingParentheses) {
final PsiExpression[] dimensions = newExpression.getArrayDimensions();
for (PsiExpression dimension : dimensions) {
removeParentheses(dimension, ignoreClarifyingParentheses);
}
final PsiExpression qualifier = newExpression.getQualifier();
if (qualifier != null) {
removeParentheses(qualifier, ignoreClarifyingParentheses);
}
final PsiExpression arrayInitializer = newExpression.getArrayInitializer();
if (arrayInitializer != null) {
removeParentheses(arrayInitializer, ignoreClarifyingParentheses);
}
final PsiExpressionList argumentList = newExpression.getArgumentList();
if (argumentList != null) {
final PsiExpression[] arguments = argumentList.getExpressions();
for (PsiExpression argument : arguments) {
removeParentheses(argument, ignoreClarifyingParentheses);
}
}
}
private static void removeParensFromMethodCallExpression(@NotNull PsiMethodCallExpression methodCallExpression,
boolean ignoreClarifyingParentheses) {
final PsiReferenceExpression target = methodCallExpression.getMethodExpression();
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
removeParentheses(target, ignoreClarifyingParentheses);
for (final PsiExpression argument : arguments) {
removeParentheses(argument, ignoreClarifyingParentheses);
}
}
public static boolean areParenthesesNeeded(PsiParenthesizedExpression expression) {
final PsiElement parent = expression.getParent();
if (!(parent instanceof PsiExpression)) {
return false;
}
final PsiExpression child = expression.getExpression();
if (child == null) {
return true;
}
if (parent instanceof PsiArrayAccessExpression) {
final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression)parent;
final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression();
if (expression == indexExpression) {
return false;
}
}
return areParenthesesNeeded(child, (PsiExpression)parent);
}
public static boolean areParenthesesNeeded(PsiExpression expression, PsiExpression parentExpression) {
if (parentExpression instanceof PsiParenthesizedExpression || parentExpression instanceof PsiArrayInitializerExpression) {
return false;
}
final int parentPrecedence = getPrecedence(parentExpression);
final int childPrecedence = getPrecedence(expression);
if (parentPrecedence > childPrecedence) {
return false;
}
if (parentExpression instanceof PsiPolyadicExpression && expression instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression parentPolyadicExpression = (PsiPolyadicExpression)parentExpression;
final PsiType parentType = parentPolyadicExpression.getType();
if (parentType == null) {
return true;
}
final PsiPolyadicExpression childPolyadicExpression = (PsiPolyadicExpression)expression;
final PsiType childType = childPolyadicExpression.getType();
if (!parentType.equals(childType)) {
return true;
}
if (childType.equalsToText(CommonClassNames.JAVA_LANG_STRING) &&
!PsiTreeUtil.isAncestor(parentPolyadicExpression.getOperands()[0], childPolyadicExpression, true)) {
final PsiExpression[] operands = childPolyadicExpression.getOperands();
for (PsiExpression operand : operands) {
if (!childType.equals(operand.getType())) {
return true;
}
}
}
else if (childType.equals(PsiType.BOOLEAN)) {
final PsiExpression[] operands = childPolyadicExpression.getOperands();
for (PsiExpression operand : operands) {
if (!PsiType.BOOLEAN.equals(operand.getType())) {
return true;
}
}
}
final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands();
if (!PsiTreeUtil.isAncestor(parentOperands[0], expression, false)) {
final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType();
if (!isCommutativeOperator(parentOperator)) {
return true;
}
}
}
return parentPrecedence < childPrecedence;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2010 Bas Leijdekkers
* Copyright 2009-2013 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package com.siyeh.ipp.psiutils;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -17,6 +17,7 @@ package com.siyeh.ipp.psiutils;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2013 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.
@@ -18,10 +18,10 @@ package com.siyeh.ipp.shift;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.siyeh.IntentionPowerPackBundle;
import org.jetbrains.annotations.NotNull;
public class ReplaceMultiplyWithShiftIntention extends MutablyNamedIntention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -18,10 +18,10 @@ package com.siyeh.ipp.shift;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.siyeh.IntentionPowerPackBundle;
import org.jetbrains.annotations.NotNull;
public class ReplaceShiftWithMultiplyIntention extends MutablyNamedIntention {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -20,10 +20,10 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ErrorUtil;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2013 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.
@@ -17,12 +17,12 @@ package com.siyeh.ipp.trivialif;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConditionalUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class MergeIfAndIntention extends Intention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2006 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -18,11 +18,11 @@ package com.siyeh.ipp.trivialif;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class MergeIfOrIntention extends Intention {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -20,10 +20,11 @@ import com.intellij.psi.impl.PsiDiamondTypeUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.EquivalenceChecker;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConditionalUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ReplaceIfWithConditionalIntention extends Intention {
@@ -181,7 +182,7 @@ public class ReplaceIfWithConditionalIntention extends Intention {
if (elseValue == null) {
return null;
}
final StringBuilder conditional = new StringBuilder();
@NonNls final StringBuilder conditional = new StringBuilder();
final String conditionText = getExpressionText(condition);
conditional.append(conditionText);
conditional.append('?');