IG+IPP: remove a little duplication

This commit is contained in:
Bas Leijdekkers
2015-05-15 16:38:37 +02:00
parent 11b40c924b
commit 967f021bd4
7 changed files with 26 additions and 86 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 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.
@@ -27,6 +27,8 @@ import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -117,7 +119,8 @@ public class StringConcatenationInsideStringBufferAppendInspection extends BaseI
return;
}
final PsiExpression argument = arguments[0];
if (!isConcatenation(argument)) {
if (!ExpressionUtils.isConcatenation(ParenthesesUtils.stripParentheses(argument)) ||
PsiUtil.isConstantExpression(argument)) {
return;
}
final PsiMethod method = expression.resolveMethod();
@@ -145,23 +148,5 @@ public class StringConcatenationInsideStringBufferAppendInspection extends BaseI
}
registerMethodCallError(expression, containingClass);
}
private static boolean isConcatenation(PsiExpression expression) {
if (expression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression;
return isConcatenation(parenthesizedExpression.getExpression());
}
if (!(expression instanceof PsiPolyadicExpression)) {
return false;
}
if (PsiUtil.isConstantExpression(expression)) {
return false;
}
final PsiType type = expression.getType();
if (type == null) {
return false;
}
return type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
}
}
@@ -638,4 +638,13 @@ public class ExpressionUtils {
}
return (PsiVariable)target;
}
public static boolean isConcatenation(PsiElement element) {
if (!(element instanceof PsiPolyadicExpression)) {
return false;
}
final PsiPolyadicExpression expression = (PsiPolyadicExpression)element;
final PsiType type = expression.getType();
return type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 Bas Leijdekkers
* Copyright 2008-2015 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,9 +19,9 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiConcatenationUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConcatenationUtils;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -39,7 +39,7 @@ public class ReplaceConcatenationWithFormatStringIntention extends Intention {
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
PsiPolyadicExpression expression = (PsiPolyadicExpression)element;
PsiElement parent = expression.getParent();
while (ConcatenationUtils.isConcatenation(parent)) {
while (ExpressionUtils.isConcatenation(parent)) {
expression = (PsiPolyadicExpression)parent;
parent = expression.getParent();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 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,10 @@ import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
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 org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -51,7 +51,7 @@ public class ReplaceConcatenationWithStringBufferIntention extends MutablyNamedI
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
PsiPolyadicExpression expression = (PsiPolyadicExpression)element;
PsiElement parent = expression.getParent();
while (ConcatenationUtils.isConcatenation(parent)) {
while (ExpressionUtils.isConcatenation(parent)) {
expression = (PsiPolyadicExpression)parent;
parent = expression.getParent();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 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,8 +17,8 @@ package com.siyeh.ipp.concatenation;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.psi.PsiElement;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConcatenationUtils;
class SimpleStringConcatenationPredicate implements PsiElementPredicate {
@@ -30,7 +30,7 @@ class SimpleStringConcatenationPredicate implements PsiElementPredicate {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!ConcatenationUtils.isConcatenation(element)) {
if (!ExpressionUtils.isConcatenation(element)) {
return false;
}
return !(excludeConcatenationsInsideAnnotations && AnnotationUtil.isInsideAnnotation(element));
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2013 Dave Griffith, Bas Leijdekkers
* Copyright 2007-2015 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.
@@ -24,9 +24,9 @@ import com.intellij.psi.PsiPolyadicExpression;
import com.intellij.psi.tree.IElementType;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ConcatenationUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
@@ -40,7 +40,7 @@ public class FlipExpressionIntention extends MutablyNamedIntention {
final String operatorText = sign == null ? "" : sign.getText();
final IElementType tokenType = expression.getOperationTokenType();
final boolean commutative = ParenthesesUtils.isCommutativeOperator(tokenType);
if (commutative && !ConcatenationUtils.isConcatenation(expression)) {
if (commutative && !ExpressionUtils.isConcatenation(expression)) {
return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText);
}
else {
@@ -1,54 +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;
public class ConcatenationUtils {
private ConcatenationUtils() {}
public static boolean isConcatenation(PsiElement element) {
if (!(element instanceof PsiPolyadicExpression)) {
return false;
}
final PsiPolyadicExpression expression = (PsiPolyadicExpression)element;
final IElementType tokenType = expression.getOperationTokenType();
if (!tokenType.equals(JavaTokenType.PLUS)) {
return false;
}
final PsiExpression[] operands = expression.getOperands();
if (operands.length <= 1) {
return false;
}
final PsiType type = expression.getType();
if (type == null) {
for (PsiExpression operand : operands) {
if (hasStringType(operand)) {
return true;
}
}
return false;
}
return type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
private static boolean hasStringType(PsiExpression expression) {
final PsiType type = expression.getType();
return type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
}