2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.siyeh
.ig
.assignment
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.tree
.IElementType
;
20 import com
.intellij
.psi
.util
.PsiTreeUtil
;
21 import com
.intellij
.codeInspection
.ui
.SingleCheckboxOptionsPanel
;
22 import com
.siyeh
.InspectionGadgetsBundle
;
23 import com
.siyeh
.ig
.BaseInspection
;
24 import com
.siyeh
.ig
.BaseInspectionVisitor
;
25 import com
.siyeh
.ig
.psiutils
.CollectionUtils
;
26 import org
.jetbrains
.annotations
.NotNull
;
27 import org
.jetbrains
.annotations
.Nullable
;
31 public class AssignmentToCollectionFieldFromParameterInspection
32 extends BaseInspection
{
34 /** @noinspection PublicField*/
35 public boolean ignorePrivateMethods
= true;
38 public String
getID(){
39 return "AssignmentToCollectionOrArrayFieldFromParameter";
43 public String
getDisplayName(){
44 return InspectionGadgetsBundle
.message(
45 "assignment.collection.array.field.from.parameter.display.name");
49 public String
buildErrorString(Object
... infos
){
50 final PsiExpression rhs
= (PsiExpression
)infos
[0];
51 final PsiField field
= (PsiField
)infos
[1];
52 final PsiType type
= field
.getType();
53 if(type
instanceof PsiArrayType
){
54 return InspectionGadgetsBundle
.message(
55 "assignment.collection.array.field.from.parameter.problem.descriptor.array",
58 return InspectionGadgetsBundle
.message(
59 "assignment.collection.array.field.from.parameter.problem.descriptor.collection",
65 public JComponent
createOptionsPanel(){
66 return new SingleCheckboxOptionsPanel(
67 InspectionGadgetsBundle
.message(
68 "assignment.collection.array.field.option"), this,
69 "ignorePrivateMethods");
72 public BaseInspectionVisitor
buildVisitor(){
73 return new AssignmentToCollectionFieldFromParameterVisitor();
76 private class AssignmentToCollectionFieldFromParameterVisitor
77 extends BaseInspectionVisitor
{
79 @Override public void visitAssignmentExpression(@NotNull
80 PsiAssignmentExpression expression
){
81 super.visitAssignmentExpression(expression
);
82 final PsiExpression rhs
= expression
.getRExpression();
83 if(!(rhs
instanceof PsiReferenceExpression
)){
86 final PsiJavaToken sign
= expression
.getOperationSign();
87 final IElementType tokenType
= sign
.getTokenType();
88 if(!tokenType
.equals(JavaTokenType
.EQ
)){
91 final PsiExpression lhs
= expression
.getLExpression();
92 if(!(lhs
instanceof PsiReferenceExpression
)){
95 if(ignorePrivateMethods
){
96 final PsiMethod containingMethod
=
97 PsiTreeUtil
.getParentOfType(expression
,
99 if(containingMethod
== null ||
100 containingMethod
.hasModifierProperty(
101 PsiModifier
.PRIVATE
)){
105 final PsiElement element
= ((PsiReference
) rhs
).resolve();
106 if(!(element
instanceof PsiParameter
)){
109 if(!(element
.getParent() instanceof PsiParameterList
)){
112 final PsiReferenceExpression referenceExpression
=
113 (PsiReferenceExpression
)lhs
;
114 final PsiElement referent
= referenceExpression
.resolve();
115 if(!(referent
instanceof PsiField
)) {
118 final PsiField field
= (PsiField
)referent
;
119 if(!CollectionUtils
.isArrayOrCollectionField(field
)){
122 registerError(lhs
, rhs
, field
);