move fileScope and filesScope back from GlobalSearchScopes to GlobalSearchScope
[idea/community.git] / plugins / InspectionGadgets / src / com / siyeh / ig / maturity / ObsoleteCollectionInspection.java
1 /*
2  * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.siyeh.ig.maturity;
17
18 import com.intellij.psi.*;
19 import com.intellij.psi.search.GlobalSearchScope;
20 import com.intellij.psi.search.searches.ReferencesSearch;
21 import com.intellij.psi.util.PsiTreeUtil;
22 import com.intellij.util.Query;
23 import com.siyeh.InspectionGadgetsBundle;
24 import com.siyeh.ig.BaseInspection;
25 import com.siyeh.ig.BaseInspectionVisitor;
26 import com.siyeh.ig.psiutils.LibraryUtil;
27 import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import javax.swing.*;
33
34 public class ObsoleteCollectionInspection extends BaseInspection {
35
36     @SuppressWarnings({"PublicField"})
37     public boolean ignoreRequiredObsoleteCollectionTypes = false;
38
39     @Override
40     @NotNull
41     public String getID(){
42         return "UseOfObsoleteCollectionType";
43     }
44
45     @Override @NotNull
46     public String getDisplayName(){
47         return InspectionGadgetsBundle.message(
48                 "use.obsolete.collection.type.display.name");
49     }
50
51     @Override @NotNull
52     public String buildErrorString(Object... infos){
53         return InspectionGadgetsBundle.message(
54                 "use.obsolete.collection.type.problem.descriptor");
55     }
56
57     @Override @Nullable
58     public JComponent createOptionsPanel() {
59         return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
60                 "use.obsolete.collection.type.ignore.library.arguments.option"
61         ), this, "ignoreRequiredObsoleteCollectionTypes");
62     }
63
64     @Override
65     public BaseInspectionVisitor buildVisitor(){
66         return new ObsoleteCollectionVisitor();
67     }
68
69     private class ObsoleteCollectionVisitor
70             extends BaseInspectionVisitor{
71
72         @Override public void visitVariable(@NotNull PsiVariable variable){
73             super.visitVariable(variable);
74             final PsiType type = variable.getType();
75             if (!isObsoleteCollectionType(type)){
76                 return;
77             }
78             if (LibraryUtil.isOverrideOfLibraryMethodParameter(variable)) {
79                 return;
80             }
81             final PsiTypeElement typeElement = variable.getTypeElement();
82             if (typeElement == null) {
83                 return;
84             }
85             if (ignoreRequiredObsoleteCollectionTypes &&
86                     isUsedAsParameterForLibraryMethod(variable)) {
87                 return;
88             }
89             registerError(typeElement);
90         }
91
92         @Override public void visitMethod(PsiMethod method) {
93             super.visitMethod(method);
94             final PsiType returnType = method.getReturnType();
95             if (!isObsoleteCollectionType(returnType)) {
96                 return;
97             }
98             if (LibraryUtil.isOverrideOfLibraryMethod(method)) {
99                 return;
100             }
101             final PsiTypeElement typeElement = method.getReturnTypeElement();
102             if (typeElement == null) {
103                 return;
104             }
105             if (ignoreRequiredObsoleteCollectionTypes &&
106                     isUsedAsParameterForLibraryMethod(method)) {
107                 return;
108             }
109             registerError(typeElement);
110         }
111
112         @Override public void visitNewExpression(
113                 @NotNull PsiNewExpression newExpression){
114             super.visitNewExpression(newExpression);
115             final PsiType type = newExpression.getType();
116             if (!isObsoleteCollectionType(type)){
117                 return;
118             }
119             if (ignoreRequiredObsoleteCollectionTypes &&
120                     isRequiredObsoleteCollectionElement(newExpression)) {
121                 return;
122             }
123             registerNewExpressionError(newExpression);
124         }
125
126         private boolean isObsoleteCollectionType(PsiType type) {
127             if(type == null){
128                 return false;
129             }
130             final PsiType deepComponentType = type.getDeepComponentType();
131             if (!(deepComponentType instanceof PsiClassType)) {
132                 return false;
133             }
134             final PsiClassType classType = (PsiClassType)deepComponentType;
135             @NonNls final String className = classType.getClassName();
136             if (!"Vector".equals(className) && !"Hashtable".equals(className)) {
137                 return false;
138             }
139             final PsiClass aClass = classType.resolve();
140             if (aClass == null) {
141                 return false;
142             }
143             final String name = aClass.getQualifiedName();
144             return "java.util.Vector".equals(name) ||
145                    "java.util.Hashtable".equals(name);
146         }
147
148         private boolean isUsedAsParameterForLibraryMethod(
149                 PsiNamedElement namedElement) {
150             final PsiFile containingFile = namedElement.getContainingFile();
151             final Query<PsiReference> query =
152                     ReferencesSearch.search(namedElement,
153                             GlobalSearchScope.fileScope(containingFile));
154             for (PsiReference reference : query) {
155                 final PsiElement element = reference.getElement();
156                 if (isRequiredObsoleteCollectionElement(element)) {
157                     return true;
158                 }
159             }
160             return false;
161         }
162
163         private boolean isRequiredObsoleteCollectionElement(
164                 PsiElement element) {
165             final PsiElement parent = element.getParent();
166             if (parent instanceof PsiVariable) {
167                 final PsiVariable variable = (PsiVariable)parent;
168                 final PsiType variableType = variable.getType();
169                 if (isObsoleteCollectionType(variableType)) {
170                     return true;
171                 }
172             } else if (parent instanceof PsiReturnStatement) {
173                 final PsiMethod method = PsiTreeUtil.getParentOfType(parent,
174                         PsiMethod.class);
175                 if (method != null) {
176                     final PsiType returnType = method.getReturnType();
177                     if (isObsoleteCollectionType(returnType)) {
178                         return true;
179                     }
180                 }
181             } else if (parent instanceof PsiAssignmentExpression) {
182                 final PsiAssignmentExpression assignmentExpression =
183                         (PsiAssignmentExpression)parent;
184                 final PsiExpression lhs = assignmentExpression.getLExpression();
185                 final PsiType lhsType = lhs.getType();
186                 if (isObsoleteCollectionType(lhsType)) {
187                     return true;
188                 }
189
190             }
191             if (!(parent instanceof PsiExpressionList)) {
192                 return false;
193             }
194             final PsiExpressionList argumentList = (PsiExpressionList)parent;
195             final PsiElement grandParent = parent.getParent();
196             if (!(grandParent instanceof PsiCallExpression)) {
197                 return false;
198             }
199             final PsiCallExpression callExpression =
200                     (PsiCallExpression)grandParent;
201             final int index = getIndexOfArgument(argumentList, element);
202             final PsiMethod method = callExpression.resolveMethod();
203             if (method == null) {
204                 return false;
205             }
206             final PsiParameterList parameterList =
207                     method.getParameterList();
208             final PsiParameter[] parameters = parameterList.getParameters();
209             if (index >= parameters.length) {
210                 final PsiParameter lastParameter =
211                         parameters[parameters.length - 1];
212                 if (!lastParameter.isVarArgs()) {
213                     return false;
214                 }
215                 final PsiType type = lastParameter.getType();
216                 if (!(type instanceof PsiEllipsisType)) {
217                     return false;
218                 }
219                 final PsiEllipsisType ellipsisType = (PsiEllipsisType) type;
220                 final PsiType componentType = ellipsisType.getComponentType();
221                 return isObsoleteCollectionType(componentType);
222             }
223             final PsiParameter parameter = parameters[index];
224             final PsiType type = parameter.getType();
225             return isObsoleteCollectionType(type);
226         }
227
228         private int getIndexOfArgument(PsiExpressionList argumentList,
229                                        PsiElement argument) {
230             final PsiExpression[] expressions =
231                     argumentList.getExpressions();
232             int index = -1;
233             for (PsiExpression expression : expressions) {
234                 index++;
235                 if (expression.equals(argument)) {
236                     break;
237                 }
238             }
239             return index;
240         }
241     }
242 }