2 * Copyright 2000-2009 JetBrains s.r.o.
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
.intellij
.codeInspection
.nullable
;
18 import com
.intellij
.codeInsight
.AnnotationUtil
;
19 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
20 import com
.intellij
.codeInsight
.intention
.AddAnnotationFix
;
21 import com
.intellij
.codeInspection
.InspectionsBundle
;
22 import com
.intellij
.codeInspection
.LocalQuickFix
;
23 import com
.intellij
.codeInspection
.ProblemDescriptor
;
24 import com
.intellij
.openapi
.diagnostic
.Logger
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.psi
.PsiMethod
;
28 import com
.intellij
.psi
.PsiParameter
;
29 import com
.intellij
.psi
.search
.GlobalSearchScope
;
30 import com
.intellij
.psi
.search
.searches
.OverridingMethodsSearch
;
31 import com
.intellij
.psi
.util
.ClassUtil
;
32 import com
.intellij
.psi
.util
.PsiTreeUtil
;
33 import com
.intellij
.util
.ArrayUtil
;
34 import com
.intellij
.util
.IncorrectOperationException
;
35 import org
.jetbrains
.annotations
.NotNull
;
37 import java
.util
.ArrayList
;
38 import java
.util
.List
;
43 public class AnnotateOverriddenMethodParameterFix
implements LocalQuickFix
{
44 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInspection.AnnotateMethodFix");
45 private final String myAnnotation
;
46 private final String
[] myAnnosToRemove
;
48 public AnnotateOverriddenMethodParameterFix(final String fqn
, String
... annosToRemove
) {
50 myAnnosToRemove
= annosToRemove
;
54 public String
getName() {
55 return InspectionsBundle
.message("annotate.overridden.methods.parameters", ClassUtil
.extractClassName(myAnnotation
));
58 public void applyFix(@NotNull Project project
, @NotNull ProblemDescriptor descriptor
) {
59 final PsiElement psiElement
= descriptor
.getPsiElement();
61 PsiParameter parameter
= PsiTreeUtil
.getParentOfType(psiElement
, PsiParameter
.class, false);
62 if (parameter
== null) return;
63 PsiMethod method
= PsiTreeUtil
.getParentOfType(parameter
, PsiMethod
.class);
64 if (method
== null) return;
65 PsiParameter
[] parameters
= method
.getParameterList().getParameters();
66 int index
= ArrayUtil
.find(parameters
, parameter
);
68 List
<PsiParameter
> toAnnotate
= new ArrayList
<PsiParameter
>();
70 PsiMethod
[] methods
= OverridingMethodsSearch
.search(method
, GlobalSearchScope
.allScope(project
), true).toArray(PsiMethod
.EMPTY_ARRAY
);
71 for (PsiMethod psiMethod
: methods
) {
72 PsiParameter
[] psiParameters
= psiMethod
.getParameterList().getParameters();
73 if (index
>= psiParameters
.length
) continue;
74 PsiParameter psiParameter
= psiParameters
[index
];
75 if (!AnnotationUtil
.isAnnotated(psiParameter
, myAnnotation
, false) && psiMethod
.getManager().isInProject(psiMethod
)) {
76 toAnnotate
.add(psiParameter
);
80 CodeInsightUtilBase
.preparePsiElementsForWrite(toAnnotate
);
81 for (PsiParameter psiParam
: toAnnotate
) {
83 new AddAnnotationFix(myAnnotation
, psiParam
, myAnnosToRemove
).invoke(project
, null, psiParam
.getContainingFile());
85 catch (IncorrectOperationException e
) {
92 public String
getFamilyName() {