3
3
import com .intellij .codeInsight .daemon .GroupNames ;
4
4
import com .intellij .openapi .diagnostic .Logger ;
5
5
import com .intellij .openapi .project .Project ;
6
- import com .intellij .openapi .util .Ref ;
7
6
import com .intellij .psi .*;
8
7
import com .intellij .psi .tree .IElementType ;
9
8
import com .intellij .ui .DocumentAdapter ;
14
13
import javax .swing .*;
15
14
import javax .swing .event .DocumentEvent ;
16
15
import java .awt .*;
17
- import java .util .ArrayList ;
18
- import java .util .Arrays ;
19
16
import java .util .StringTokenizer ;
20
17
21
18
/**
@@ -26,13 +23,17 @@ public class ComparingReferencesInspection extends BaseJavaLocalInspectionTool {
26
23
27
24
private final LocalQuickFix myQuickFix = new MyQuickFix ();
28
25
29
- @ SuppressWarnings ({"WeakerAccess" }) @ NonNls public String CHECKED_CLASSES = "java.lang.String;java.util.Date" ;
30
- @ NonNls private static final String DESCRIPTION_TEMPLATE = InspectionsBundle .message ("inspection.comparing.references.problem.descriptor" );
26
+ @ SuppressWarnings ({"WeakerAccess" })
27
+ @ NonNls
28
+ public String CHECKED_CLASSES = "java.lang.String;java.util.Date" ;
29
+ @ NonNls
30
+ private static final String DESCRIPTION_TEMPLATE =
31
+ InspectionsBundle .message ("inspection.comparing.references.problem.descriptor" );
31
32
32
33
@ NotNull
33
34
public String getDisplayName () {
34
35
35
- return "'==' or '!=' instead of 'equals()'" ;
36
+ return "'==' or '!=' instead of 'equals()'" ;
36
37
}
37
38
38
39
@ NotNull
@@ -57,32 +58,33 @@ private boolean isCheckedType(PsiType type) {
57
58
return false ;
58
59
}
59
60
60
- @ NotNull
61
- @ Override
62
- public PsiElementVisitor buildVisitor (@ NotNull final ProblemsHolder holder , boolean isOnTheFly ) {
63
- return new JavaElementVisitor () {
61
+ @ NotNull
62
+ @ Override
63
+ public PsiElementVisitor buildVisitor (@ NotNull final ProblemsHolder holder , boolean isOnTheFly ) {
64
+ return new JavaElementVisitor () {
64
65
65
- @ Override
66
- public void visitReferenceExpression (PsiReferenceExpression psiReferenceExpression ) {
67
- }
66
+ @ Override
67
+ public void visitReferenceExpression (PsiReferenceExpression psiReferenceExpression ) {
68
+ }
68
69
69
70
70
- @ Override public void visitBinaryExpression (PsiBinaryExpression expression ) {
71
- super .visitBinaryExpression (expression );
71
+ @ Override
72
+ public void visitBinaryExpression (PsiBinaryExpression expression ) {
73
+ super .visitBinaryExpression (expression );
72
74
IElementType opSign = expression .getOperationTokenType ();
73
- if (opSign == JavaTokenType .EQEQ || opSign == JavaTokenType .NE ) {
74
- PsiExpression lOperand = expression .getLOperand ();
75
- PsiExpression rOperand = expression .getROperand ();
76
- if (rOperand == null || isNullLiteral (lOperand ) || isNullLiteral (rOperand )) return ;
77
-
78
- PsiType lType = lOperand .getType ();
79
- PsiType rType = rOperand .getType ();
80
-
81
- if (isCheckedType (lType ) || isCheckedType (rType )) {
82
- holder .registerProblem (expression ,
83
- DESCRIPTION_TEMPLATE , myQuickFix );
84
- }
75
+ if (opSign == JavaTokenType .EQEQ || opSign == JavaTokenType .NE ) {
76
+ PsiExpression lOperand = expression .getLOperand ();
77
+ PsiExpression rOperand = expression .getROperand ();
78
+ if (rOperand == null || isNullLiteral (lOperand ) || isNullLiteral (rOperand )) return ;
79
+
80
+ PsiType lType = lOperand .getType ();
81
+ PsiType rType = rOperand .getType ();
82
+
83
+ if (isCheckedType (lType ) || isCheckedType (rType )) {
84
+ holder .registerProblem (expression ,
85
+ DESCRIPTION_TEMPLATE , myQuickFix );
85
86
}
87
+ }
86
88
}
87
89
};
88
90
}
@@ -94,35 +96,35 @@ private static boolean isNullLiteral(PsiExpression expr) {
94
96
private static class MyQuickFix implements LocalQuickFix {
95
97
@ NotNull
96
98
public String getName () {
97
- // The test (see the TestThisPlugin class) uses this string to identify the quick fix action.
99
+ // The test (see the TestThisPlugin class) uses this string to identify the quick fix action.
98
100
return InspectionsBundle .message ("inspection.comparing.references.use.quickfix" );
99
101
}
100
102
101
103
102
104
public void applyFix (@ NotNull Project project , @ NotNull ProblemDescriptor descriptor ) {
103
105
try {
104
- PsiBinaryExpression binaryExpression = (PsiBinaryExpression )descriptor .getPsiElement ();
106
+ PsiBinaryExpression binaryExpression = (PsiBinaryExpression ) descriptor .getPsiElement ();
105
107
IElementType opSign = binaryExpression .getOperationTokenType ();
106
108
PsiExpression lExpr = binaryExpression .getLOperand ();
107
109
PsiExpression rExpr = binaryExpression .getROperand ();
108
110
if (rExpr == null )
109
111
return ;
110
112
111
113
PsiElementFactory factory = JavaPsiFacade .getInstance (project ).getElementFactory ();
112
- PsiMethodCallExpression equalsCall = (PsiMethodCallExpression )factory .createExpressionFromText ("a.equals(b)" , null );
114
+ PsiMethodCallExpression equalsCall =
115
+ (PsiMethodCallExpression ) factory .createExpressionFromText ("a.equals(b)" , null );
113
116
114
117
equalsCall .getMethodExpression ().getQualifierExpression ().replace (lExpr );
115
118
equalsCall .getArgumentList ().getExpressions ()[0 ].replace (rExpr );
116
119
117
- PsiExpression result = (PsiExpression )binaryExpression .replace (equalsCall );
120
+ PsiExpression result = (PsiExpression ) binaryExpression .replace (equalsCall );
118
121
119
122
if (opSign == JavaTokenType .NE ) {
120
- PsiPrefixExpression negation = (PsiPrefixExpression )factory .createExpressionFromText ("!a" , null );
123
+ PsiPrefixExpression negation = (PsiPrefixExpression ) factory .createExpressionFromText ("!a" , null );
121
124
negation .getOperand ().replace (result );
122
125
result .replace (negation );
123
126
}
124
- }
125
- catch (IncorrectOperationException e ) {
127
+ } catch (IncorrectOperationException e ) {
126
128
LOG .error (e );
127
129
}
128
130
}
0 commit comments