mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
CharToStringPredicate: reuse StringToCharPredicate#isInConcatenationContext
This commit is contained in:
+4
-82
@@ -16,12 +16,10 @@
|
||||
package com.siyeh.ipp.chartostring;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiLiteralExpression;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import static com.intellij.psi.CommonClassNames.JAVA_LANG_STRING;
|
||||
|
||||
class CharToStringPredicate implements PsiElementPredicate {
|
||||
|
||||
@@ -44,82 +42,6 @@ class CharToStringPredicate implements PsiElementPredicate {
|
||||
// not satisfied with character literals of more than one character
|
||||
return false;
|
||||
}
|
||||
return isInConcatenationContext(expression);
|
||||
}
|
||||
|
||||
private static boolean isInConcatenationContext(PsiElement element) {
|
||||
final PsiElement parent = element.getParent();
|
||||
if (parent instanceof PsiPolyadicExpression) {
|
||||
final PsiPolyadicExpression parentExpression =
|
||||
(PsiPolyadicExpression)parent;
|
||||
final PsiType parentType = parentExpression.getType();
|
||||
if (parentType == null) {
|
||||
return false;
|
||||
}
|
||||
final String parentTypeText = parentType.getCanonicalText();
|
||||
return JAVA_LANG_STRING.equals(parentTypeText);
|
||||
}
|
||||
else if (parent instanceof PsiAssignmentExpression) {
|
||||
final PsiAssignmentExpression parentExpression =
|
||||
(PsiAssignmentExpression)parent;
|
||||
final IElementType tokenType = parentExpression.getOperationTokenType();
|
||||
if (!JavaTokenType.PLUSEQ.equals(tokenType)) {
|
||||
return false;
|
||||
}
|
||||
final PsiType parentType = parentExpression.getType();
|
||||
if (parentType == null) {
|
||||
return false;
|
||||
}
|
||||
final String parentTypeText = parentType.getCanonicalText();
|
||||
return JAVA_LANG_STRING.equals(parentTypeText);
|
||||
}
|
||||
else if (parent instanceof PsiExpressionList) {
|
||||
final PsiElement grandParent = parent.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return false;
|
||||
}
|
||||
final PsiMethodCallExpression methodCall =
|
||||
(PsiMethodCallExpression)grandParent;
|
||||
final PsiReferenceExpression methodExpression =
|
||||
methodCall.getMethodExpression();
|
||||
final PsiExpression qualifierExpression =
|
||||
methodExpression.getQualifierExpression();
|
||||
final PsiType type;
|
||||
if (qualifierExpression == null) {
|
||||
// to use the intention inside the source of
|
||||
// String and StringBuffer
|
||||
type = methodExpression.getType();
|
||||
}
|
||||
else {
|
||||
type = qualifierExpression.getType();
|
||||
}
|
||||
if (type == null) {
|
||||
return false;
|
||||
}
|
||||
final String className = type.getCanonicalText();
|
||||
if (CommonClassNames.JAVA_LANG_STRING_BUFFER.equals(className) ||
|
||||
CommonClassNames.JAVA_LANG_STRING_BUILDER.equals(className)) {
|
||||
@NonNls final String methodName =
|
||||
methodExpression.getReferenceName();
|
||||
if (!"append".equals(methodName) &&
|
||||
!"insert".equals(methodName)) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement method = methodExpression.resolve();
|
||||
return method != null;
|
||||
}
|
||||
else if (JAVA_LANG_STRING.equals(className)) {
|
||||
@NonNls final String methodName =
|
||||
methodExpression.getReferenceName();
|
||||
if (!"indexOf".equals(methodName) &&
|
||||
!"lastIndexOf".equals(methodName) &&
|
||||
!"replace".equals(methodName)) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement method = methodExpression.resolve();
|
||||
return method != null;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return StringToCharPredicate.isInConcatenationContext(expression);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class StringToCharPredicate implements PsiElementPredicate {
|
||||
return isInConcatenationContext(expression);
|
||||
}
|
||||
|
||||
private static boolean isInConcatenationContext(PsiExpression element) {
|
||||
static boolean isInConcatenationContext(PsiExpression element) {
|
||||
if (ExpressionUtils.isStringConcatenationOperand(element)) return true;
|
||||
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(element.getParent());
|
||||
if (parent instanceof PsiAssignmentExpression) {
|
||||
@@ -110,12 +110,7 @@ class StringToCharPredicate implements PsiElementPredicate {
|
||||
final PsiElement method = methodExpression.resolve();
|
||||
return method != null;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user