2 * Copyright 2003-2008 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
.bugs
;
18 import com
.intellij
.psi
.*;
19 import com
.siyeh
.ig
.psiutils
.CollectionUtils
;
20 import org
.jetbrains
.annotations
.NonNls
;
21 import org
.jetbrains
.annotations
.NotNull
;
23 import java
.util
.HashSet
;
26 class CollectionUpdateCalledVisitor
extends JavaRecursiveElementVisitor
{
29 * @noinspection StaticCollection
31 @NonNls private static final Set
<String
> updateNames
=
32 new HashSet
<String
>(31);
34 updateNames
.add("add");
35 updateNames
.add("addAll");
36 updateNames
.add("addAllAbsent");
37 updateNames
.add("addBefore");
38 updateNames
.add("addElement");
39 updateNames
.add("addFirst");
40 updateNames
.add("addIfAbsent");
41 updateNames
.add("addLast");
42 updateNames
.add("clear");
43 updateNames
.add("drainTo");
44 updateNames
.add("insertElementAt");
45 updateNames
.add("load");
46 updateNames
.add("loadFromXML");
47 updateNames
.add("offer");
48 updateNames
.add("push");
49 updateNames
.add("put");
50 updateNames
.add("putAll");
51 updateNames
.add("putIfAbsent");
52 updateNames
.add("remove");
53 updateNames
.add("removeAll");
54 updateNames
.add("removeAllElements");
55 updateNames
.add("replace");
56 updateNames
.add("retainAll");
57 updateNames
.add("removeElementAt");
58 updateNames
.add("removeFirst");
59 updateNames
.add("removeLast");
60 updateNames
.add("removeRange");
61 updateNames
.add("set");
62 updateNames
.add("setElementAt");
63 updateNames
.add("setProperty");
64 updateNames
.add("take");
67 private boolean updated
= false;
68 private final PsiVariable variable
;
70 CollectionUpdateCalledVisitor(PsiVariable variable
){
72 this.variable
= variable
;
75 @Override public void visitElement(@NotNull PsiElement element
){
77 super.visitElement(element
);
81 @Override public void visitMethodCallExpression(
82 @NotNull PsiMethodCallExpression call
){
83 super.visitMethodCallExpression(call
);
87 final PsiReferenceExpression methodExpression
=
88 call
.getMethodExpression();
89 final String methodName
= methodExpression
.getReferenceName();
90 if(!updateNames
.contains(methodName
)){
93 final PsiExpression qualifier
=
94 methodExpression
.getQualifierExpression();
95 if(qualifier
== null || qualifier
instanceof PsiThisExpression
){
96 final PsiMethod method
= call
.resolveMethod();
100 final PsiClass aClass
= method
.getContainingClass();
101 if (CollectionUtils
.isCollectionClassOrInterface(aClass
)){
105 if(!(qualifier
instanceof PsiReferenceExpression
)){
108 final PsiElement referent
= ((PsiReference
)qualifier
).resolve();
109 if(referent
== null){
112 if(referent
.equals(variable
)){
118 public boolean isUpdated(){