mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
remove some duplication
This commit is contained in:
+96
@@ -488,4 +488,100 @@ public class ExpressionUtils {
|
||||
public static boolean hasStringType(@Nullable PsiExpression expression) {
|
||||
return hasType(expression, CommonClassNames.JAVA_LANG_STRING);
|
||||
}
|
||||
|
||||
public static boolean isConversionToStringNecessary(PsiExpression expression) {
|
||||
final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression);
|
||||
if (parent instanceof PsiPolyadicExpression) {
|
||||
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
|
||||
final PsiType type = polyadicExpression.getType();
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) {
|
||||
return true;
|
||||
}
|
||||
final PsiExpression[] operands = polyadicExpression.getOperands();
|
||||
int index = -1;
|
||||
for (int i = 0, length = operands.length; i < length; i++) {
|
||||
final PsiExpression operand = operands[i];
|
||||
if (expression.equals(operand)) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
if (index > 0) {
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) {
|
||||
return true;
|
||||
}
|
||||
} else if (operands.length > 1) {
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (parent instanceof PsiExpressionList) {
|
||||
final PsiExpressionList expressionList = (PsiExpressionList)parent;
|
||||
final PsiElement grandParent = expressionList.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return true;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
final PsiReferenceExpression methodExpression1 = methodCallExpression.getMethodExpression();
|
||||
@NonNls final String name = methodExpression1.getReferenceName();
|
||||
final PsiExpression[] expressions = expressionList.getExpressions();
|
||||
if ("insert".equals(name)) {
|
||||
if (expressions.length < 2 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[1]))) {
|
||||
return true;
|
||||
}
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("append".equals(name)) {
|
||||
if (expressions.length < 1 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[0]))) {
|
||||
return true;
|
||||
}
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("print".equals(name) || "println".equals(name)) {
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.io.PrintStream", "java.io.PrintWriter")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("trace".equals(name) || "debug".equals(name) || "info".equals(name) || "warn".equals(name) || "error".equals(name)) {
|
||||
if (!isCallToMethodIn(methodCallExpression, "org.slf4j.Logger")) {
|
||||
return true;
|
||||
}
|
||||
int l = 1;
|
||||
for (int i = 0; i < expressions.length; i++) {
|
||||
final PsiExpression expression1 = expressions[i];
|
||||
if (i == 0 && TypeUtils.expressionHasTypeOrSubtype(expression1, "org.slf4j.Marker")) {
|
||||
l = 2;
|
||||
}
|
||||
if (expression1 == expression && i < l) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isCallToMethodIn(PsiMethodCallExpression methodCallExpression, String... classNames) {
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (containingClass == null) {
|
||||
return false;
|
||||
}
|
||||
final String qualifiedName = containingClass.getQualifiedName();
|
||||
for (String className : classNames) {
|
||||
if (className.equals(qualifiedName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+2
-100
@@ -23,6 +23,7 @@ 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 com.siyeh.ig.psiutils.TypeUtils;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
@@ -103,10 +104,7 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection {
|
||||
super.visitMethodCallExpression(expression);
|
||||
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
|
||||
@NonNls final String referenceName = methodExpression.getReferenceName();
|
||||
if (!"valueOf".equals(referenceName)) {
|
||||
return;
|
||||
}
|
||||
if (isCallToStringValueOfNecessary(expression)) {
|
||||
if (!"valueOf".equals(referenceName) || ExpressionUtils.isConversionToStringNecessary(expression)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpressionList argumentList = expression.getArgumentList();
|
||||
@@ -137,101 +135,5 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection {
|
||||
}
|
||||
registerError(expression, calculateReplacementText(argument));
|
||||
}
|
||||
|
||||
private static boolean isCallToStringValueOfNecessary(PsiMethodCallExpression expression) {
|
||||
final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression);
|
||||
if (parent instanceof PsiPolyadicExpression) {
|
||||
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
|
||||
final PsiType type = polyadicExpression.getType();
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) {
|
||||
return true;
|
||||
}
|
||||
final PsiExpression[] operands = polyadicExpression.getOperands();
|
||||
int index = -1;
|
||||
for (int i = 0, length = operands.length; i < length; i++) {
|
||||
final PsiExpression operand = operands[i];
|
||||
if (expression.equals(operand)) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
if (index > 0) {
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) {
|
||||
return true;
|
||||
}
|
||||
} else if (operands.length > 1) {
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (parent instanceof PsiExpressionList) {
|
||||
final PsiExpressionList expressionList = (PsiExpressionList)parent;
|
||||
final PsiElement grandParent = expressionList.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return true;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
final PsiReferenceExpression methodExpression1 = methodCallExpression.getMethodExpression();
|
||||
@NonNls final String name = methodExpression1.getReferenceName();
|
||||
final PsiExpression[] expressions = expressionList.getExpressions();
|
||||
if ("insert".equals(name)) {
|
||||
if (expressions.length < 2 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[1]))) {
|
||||
return true;
|
||||
}
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("append".equals(name)) {
|
||||
if (expressions.length < 1 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[0]))) {
|
||||
return true;
|
||||
}
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("print".equals(name) || "println".equals(name)) {
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.io.PrintStream", "java.io.PrintWriter")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("trace".equals(name) || "debug".equals(name) || "info".equals(name) || "warn".equals(name) || "error".equals(name)) {
|
||||
if (!isCallToMethodIn(methodCallExpression, "org.slf4j.Logger")) {
|
||||
return true;
|
||||
}
|
||||
int l = 1;
|
||||
for (int i = 0; i < expressions.length; i++) {
|
||||
final PsiExpression expression1 = expressions[i];
|
||||
if (i == 0 && TypeUtils.expressionHasTypeOrSubtype(expression1, "org.slf4j.Marker")) {
|
||||
l = 2;
|
||||
}
|
||||
if (expression1 == expression && i < l) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isCallToMethodIn(PsiMethodCallExpression methodCallExpression, String... classNames) {
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (containingClass == null) {
|
||||
return false;
|
||||
}
|
||||
final String qualifiedName = containingClass.getQualifiedName();
|
||||
for (String className : classNames) {
|
||||
if (className.equals(qualifiedName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-102
@@ -23,8 +23,7 @@ import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -100,10 +99,7 @@ public class UnnecessaryToStringCallInspection extends BaseInspection {
|
||||
super.visitMethodCallExpression(expression);
|
||||
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
|
||||
@NonNls final String referenceName = methodExpression.getReferenceName();
|
||||
if (!"toString".equals(referenceName)) {
|
||||
return;
|
||||
}
|
||||
if (isToStringCallNecessary(expression)) {
|
||||
if (!"toString".equals(referenceName) || ExpressionUtils.isConversionToStringNecessary(expression)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpressionList argumentList = expression.getArgumentList();
|
||||
@@ -118,101 +114,5 @@ public class UnnecessaryToStringCallInspection extends BaseInspection {
|
||||
}
|
||||
registerMethodCallError(expression, calculateReplacementText(qualifier));
|
||||
}
|
||||
|
||||
private static boolean isToStringCallNecessary(PsiMethodCallExpression expression) {
|
||||
final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression);
|
||||
if (parent instanceof PsiPolyadicExpression) {
|
||||
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
|
||||
final PsiType type = polyadicExpression.getType();
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) {
|
||||
return true;
|
||||
}
|
||||
final PsiExpression[] operands = polyadicExpression.getOperands();
|
||||
int index = -1;
|
||||
for (int i = 0, length = operands.length; i < length; i++) {
|
||||
final PsiExpression operand = operands[i];
|
||||
if (expression.equals(operand)) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
if (index > 0) {
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) {
|
||||
return true;
|
||||
}
|
||||
} else if (operands.length > 1) {
|
||||
if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (parent instanceof PsiExpressionList) {
|
||||
final PsiExpressionList expressionList = (PsiExpressionList)parent;
|
||||
final PsiElement grandParent = expressionList.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return true;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
final PsiReferenceExpression methodExpression1 = methodCallExpression.getMethodExpression();
|
||||
@NonNls final String name = methodExpression1.getReferenceName();
|
||||
final PsiExpression[] expressions = expressionList.getExpressions();
|
||||
if ("insert".equals(name)) {
|
||||
if (expressions.length < 2 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[1]))) {
|
||||
return true;
|
||||
}
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("append".equals(name)) {
|
||||
if (expressions.length < 1 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[0]))) {
|
||||
return true;
|
||||
}
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("print".equals(name) || "println".equals(name)) {
|
||||
if (!isCallToMethodIn(methodCallExpression, "java.io.PrintStream", "java.io.PrintWriter")) {
|
||||
return true;
|
||||
}
|
||||
} else if ("trace".equals(name) || "debug".equals(name) || "info".equals(name) || "warn".equals(name) || "error".equals(name)) {
|
||||
if (!isCallToMethodIn(methodCallExpression, "org.slf4j.Logger")) {
|
||||
return true;
|
||||
}
|
||||
int l = 1;
|
||||
for (int i = 0; i < expressions.length; i++) {
|
||||
final PsiExpression expression1 = expressions[i];
|
||||
if (i == 0 && TypeUtils.expressionHasTypeOrSubtype(expression1, "org.slf4j.Marker")) {
|
||||
l = 2;
|
||||
}
|
||||
if (expression1 == expression && i < l) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isCallToMethodIn(PsiMethodCallExpression methodCallExpression, String... classNames) {
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (containingClass == null) {
|
||||
return false;
|
||||
}
|
||||
final String qualifiedName = containingClass.getQualifiedName();
|
||||
for (String className : classNames) {
|
||||
if (className.equals(qualifiedName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user