From 5c5df8707d40e1f09a875fd122011d7bfe681473 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 18 Jul 2013 14:08:50 +0200 Subject: [PATCH] Remove ParenthesesUtils duplication --- .../siyeh/ig/psiutils/ParenthesesUtils.java | 8 +- .../src/com/siyeh/ipp/base/Intention.java | 4 +- .../siyeh/ipp/bool/DemorgansIntention.java | 7 +- .../FlipCommutativeMethodCallIntention.java | 8 +- ...oncatenationWithStringBufferIntention.java | 4 +- .../RemoveConditionalPredicate.java | 4 +- .../ReplaceConditionalWithIfIntention.java | 8 +- .../ReplaceEqualityWithEqualsIntention.java | 11 +- ...eplaceEqualityWithSafeEqualsIntention.java | 4 +- .../ReplaceEqualsWithEqualityIntention.java | 4 +- .../expression/FlipExpressionIntention.java | 2 +- ...rEachLoopWithIteratorForLoopIntention.java | 4 +- .../ReverseForLoopDirectionIntention.java | 16 +- ...ignmentWithPostfixExpressionPredicate.java | 8 +- ...atorAssignmentWithAssignmentIntention.java | 2 +- ...RemoveUnnecessaryParenthesesIntention.java | 4 +- .../UnnecessaryParenthesesPredicate.java | 6 +- .../src/com/siyeh/ipp/psiutils/BoolUtils.java | 3 +- .../siyeh/ipp/psiutils/ParenthesesUtils.java | 470 ------------------ .../ipp/psiutils/VariableAccessUtils.java | 3 +- .../ipp/psiutils/VariableAssignedVisitor.java | 3 +- .../ReplaceMultiplyWithShiftIntention.java | 6 +- .../ReplaceShiftWithMultiplyIntention.java | 6 +- .../trivialif/ConvertToNestedIfIntention.java | 4 +- .../ipp/trivialif/MergeIfAndIntention.java | 6 +- .../ipp/trivialif/MergeIfOrIntention.java | 6 +- .../ReplaceIfWithConditionalIntention.java | 7 +- 27 files changed, 77 insertions(+), 541 deletions(-) delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java index 8523c161d0fa..f93f13bcb9a4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ParenthesesUtils.java @@ -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; } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java index 9e25bb8a1aee..04d41b66be47 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/bool/DemorgansIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/bool/DemorgansIntention.java index ce7cb8dd7cd7..6d7487523ac5 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/bool/DemorgansIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/bool/DemorgansIntention.java @@ -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) { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/commutative/FlipCommutativeMethodCallIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/commutative/FlipCommutativeMethodCallIntention.java index 9e9e96a45cc4..d560d0892770 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/commutative/FlipCommutativeMethodCallIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/commutative/FlipCommutativeMethodCallIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java index 0c2b1e06f720..8ac97455f23b 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/RemoveConditionalPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/RemoveConditionalPredicate.java index 7fbc2952ddec..2da21d18a909 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/RemoveConditionalPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/RemoveConditionalPredicate.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/ReplaceConditionalWithIfIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/ReplaceConditionalWithIfIntention.java index 1a1cf925799f..ccdc0c3ee621 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/ReplaceConditionalWithIfIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/conditional/ReplaceConditionalWithIfIntention.java @@ -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; } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java index ebe0916af832..a9afb4833253 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java index edb897241734..d5d14f7b59ee 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualsWithEqualityIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualsWithEqualityIntention.java index e9686eb755cc..5faa4d7f6871 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualsWithEqualityIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualsWithEqualityIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipExpressionIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipExpressionIntention.java index 5c992ad66f37..fe4e86213069 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipExpressionIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipExpressionIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIteratorForLoopIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIteratorForLoopIntention.java index eab3a56a97db..f99fdd387879 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIteratorForLoopIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIteratorForLoopIntention.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReverseForLoopDirectionIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReverseForLoopDirectionIntention.java index 0bdbf425703d..1fb39141df76 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReverseForLoopDirectionIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReverseForLoopDirectionIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java index 1d135e288c54..e239e85893af 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceOperatorAssignmentWithAssignmentIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceOperatorAssignmentWithAssignmentIntention.java index ea69411b0db1..c94b02db8900 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceOperatorAssignmentWithAssignmentIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceOperatorAssignmentWithAssignmentIntention.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/RemoveUnnecessaryParenthesesIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/RemoveUnnecessaryParenthesesIntention.java index 46e9755bc312..50fdf885eff4 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/RemoveUnnecessaryParenthesesIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/RemoveUnnecessaryParenthesesIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/UnnecessaryParenthesesPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/UnnecessaryParenthesesPredicate.java index eb5c754be291..62ca11621566 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/UnnecessaryParenthesesPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/parenthesis/UnnecessaryParenthesesPredicate.java @@ -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); } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/BoolUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/BoolUtils.java index b809c1631b5a..b5e01947a586 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/BoolUtils.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/BoolUtils.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java deleted file mode 100644 index ab2971af6b81..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java +++ /dev/null @@ -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 s_binaryOperatorPrecedence = new HashMap(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; - } -} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAccessUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAccessUtils.java index d129e7be53ac..46e221414a91 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAccessUtils.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAccessUtils.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAssignedVisitor.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAssignedVisitor.java index ac9386077e9c..8352febe9861 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAssignedVisitor.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/VariableAssignedVisitor.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceMultiplyWithShiftIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceMultiplyWithShiftIntention.java index e47c9d7e7bcf..713f81a4796c 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceMultiplyWithShiftIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceMultiplyWithShiftIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceShiftWithMultiplyIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceShiftWithMultiplyIntention.java index fd5507ca9e1b..f23ad1143cb2 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceShiftWithMultiplyIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/shift/ReplaceShiftWithMultiplyIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java index d911c0d1ab72..a768babe075c 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ConvertToNestedIfIntention.java @@ -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; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfAndIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfAndIntention.java index c38480a70068..eb9984c91dbf 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfAndIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfAndIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfOrIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfOrIntention.java index 0454034c0f36..599879661903 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfOrIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfOrIntention.java @@ -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 { diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java index be4ec1abe597..b7f95f7bafbe 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java @@ -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('?');