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.
17 package com
.intellij
.codeInspection
;
19 import com
.intellij
.codeInsight
.daemon
.EmptyResolveMessageProvider
;
20 import com
.intellij
.openapi
.diagnostic
.Logger
;
21 import com
.intellij
.openapi
.util
.TextRange
;
22 import com
.intellij
.psi
.PsiElement
;
23 import com
.intellij
.psi
.PsiFile
;
24 import com
.intellij
.psi
.PsiReference
;
25 import com
.intellij
.util
.ArrayUtil
;
26 import org
.jetbrains
.annotations
.Nls
;
27 import org
.jetbrains
.annotations
.NotNull
;
28 import org
.jetbrains
.annotations
.Nullable
;
30 import java
.util
.ArrayList
;
31 import java
.util
.List
;
36 public class ProblemsHolder
{
37 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInspection.ProblemsHolder");
38 private final InspectionManager myManager
;
39 private final PsiFile myFile
;
40 private boolean myOnTheFly
;
41 private List
<ProblemDescriptor
> myProblems
= null;
43 public ProblemsHolder(@NotNull InspectionManager manager
, @NotNull PsiFile file
, boolean onTheFly
) {
46 myOnTheFly
= onTheFly
;
49 public void registerProblem(PsiElement psiElement
, @Nls String descriptionTemplate
, LocalQuickFix
... fixes
) {
50 registerProblem(psiElement
, descriptionTemplate
, ProblemHighlightType
.GENERIC_ERROR_OR_WARNING
, fixes
);
53 public void registerProblem(PsiElement psiElement
,
54 String descriptionTemplate
,
55 ProblemHighlightType highlightType
,
56 LocalQuickFix
... fixes
) {
57 registerProblem(myManager
.createProblemDescriptor(psiElement
, descriptionTemplate
, fixes
, highlightType
, myOnTheFly
));
60 public void registerProblem(ProblemDescriptor problemDescriptor
) {
61 if (myProblems
== null) {
62 myProblems
= new ArrayList
<ProblemDescriptor
>(1);
64 PsiElement element
= problemDescriptor
.getPsiElement();
65 if (element
!= null && !isInPsiFile(element
)) {
66 LOG
.error("Reported element " + element
+ " is not from the file '" + myFile
+ "' the inspection was invoked for. Message:" + problemDescriptor
.getDescriptionTemplate());
68 myProblems
.add(problemDescriptor
);
71 private boolean isInPsiFile(@NotNull PsiElement element
) {
72 PsiFile file
= element
.getContainingFile();
73 return ArrayUtil
.indexOf(myFile
.getPsiRoots(), file
) != -1;
76 public void registerProblem(PsiReference reference
, String descriptionTemplate
, ProblemHighlightType highlightType
) {
77 LocalQuickFix
[] fixes
= null;
78 if (reference
instanceof LocalQuickFixProvider
) {
79 fixes
= ((LocalQuickFixProvider
)reference
).getQuickFixes();
82 registerProblem(myManager
.createProblemDescriptor(reference
.getElement(), reference
.getRangeInElement(), descriptionTemplate
, highlightType
,
86 public void registerProblem(PsiReference reference
) {
87 assert reference
instanceof EmptyResolveMessageProvider
;
88 registerProblem(reference
, ((EmptyResolveMessageProvider
)reference
).getUnresolvedMessagePattern(), ProblemHighlightType
.LIKE_UNKNOWN_SYMBOL
);
91 public void registerProblem(@NotNull final PsiElement psiElement
,
92 @NotNull final String message
,
93 final ProblemHighlightType highlightType
,
94 final TextRange rangeInElement
,
95 final LocalQuickFix
... fixes
) {
97 final ProblemDescriptor descriptor
= myManager
.createProblemDescriptor(psiElement
, rangeInElement
, message
, highlightType
, myOnTheFly
,
99 registerProblem(descriptor
);
102 public void registerProblem(@NotNull final PsiElement psiElement
,
103 final TextRange rangeInElement
,
104 @NotNull final String message
,
105 final LocalQuickFix
... fixes
) {
107 final ProblemDescriptor descriptor
= myManager
.createProblemDescriptor(psiElement
, rangeInElement
, message
, ProblemHighlightType
.GENERIC_ERROR_OR_WARNING
, myOnTheFly
, fixes
);
108 registerProblem(descriptor
);
112 public List
<ProblemDescriptor
> getResults() {
113 final List
<ProblemDescriptor
> problems
= myProblems
;
119 public ProblemDescriptor
[] getResultsArray() {
120 final List
<ProblemDescriptor
> problems
= myProblems
;
122 return problems
== null ?
null : problems
.toArray(new ProblemDescriptor
[problems
.size()]);
125 public final InspectionManager
getManager() {
128 public boolean hasResults() {
129 return myProblems
!= null && !myProblems
.isEmpty();
132 public boolean isOnTheFly() {