blob: 1351a29f5679f6689fa759402ff18a4f9362f272 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright 2007-2012 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.ig.junit;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class AssertEqualsBetweenInconvertibleTypesInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message("assertequals.between.inconvertible.types.display.name");
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
final PsiType comparedType = (PsiType)infos[0];
final PsiType comparisonType = (PsiType)infos[1];
final String comparedTypeText = comparedType.getPresentableText();
final String comparisonTypeText = comparisonType.getPresentableText();
return InspectionGadgetsBundle.message("assertequals.between.inconvertible.types.problem.descriptor",
StringUtil.escapeXml(comparedTypeText),
StringUtil.escapeXml(comparisonTypeText));
}
@Override
public boolean isEnabledByDefault() {
return true;
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new AssertEqualsBetweenInconvertibleTypesVisitor();
}
private static class AssertEqualsBetweenInconvertibleTypesVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
@NonNls final String methodName = methodExpression.getReferenceName();
if (!"assertEquals".equals(methodName)) {
return;
}
final PsiMethod method = expression.resolveMethod();
if (method == null) {
return;
}
final PsiClass containingClass = method.getContainingClass();
if (!InheritanceUtil.isInheritor(containingClass, "junit.framework.Assert") &&
!InheritanceUtil.isInheritor(containingClass, "org.junit.Assert")) {
return;
}
final PsiParameterList parameterList = method.getParameterList();
final PsiParameter[] parameters = parameterList.getParameters();
if (parameters.length < 2) {
return;
}
final PsiType firstParameterType = parameters[0].getType();
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
final int argumentIndex;
if (firstParameterType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
if (arguments.length < 3) {
return;
}
argumentIndex = 1;
}
else {
if (arguments.length < 2) {
return;
}
argumentIndex = 0;
}
final PsiExpression expression1 = arguments[argumentIndex];
final PsiExpression expression2 = arguments[argumentIndex + 1];
final PsiType type1 = expression1.getType();
if (type1 == null) {
return;
}
final PsiType type2 = expression2.getType();
if (type2 == null) {
return;
}
final PsiType parameterType1 = parameters[argumentIndex].getType();
final PsiType parameterType2 = parameters[argumentIndex + 1].getType();
final PsiClassType objectType = TypeUtils.getObjectType(expression);
if (!objectType.equals(parameterType1) || !objectType.equals(parameterType2)) {
return;
}
if (TypeConversionUtil.areTypesConvertible(type1, type2)) {
return;
}
registerMethodCallError(expression, type1, type2);
}
}
}
|