1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.unneededThrows;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightMethodUtil;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
import com.intellij.codeInspection.*;
import com.intellij.psi.*;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* @author anna
* @since 15-Nov-2005
*/
public class RedundantThrowsDeclaration extends BaseJavaLocalInspectionTool {
@NotNull
public String getGroupDisplayName() {
return GroupNames.DECLARATION_REDUNDANCY;
}
@NotNull
public String getDisplayName() {
return InspectionsBundle.message("redundant.throws.declaration");
}
@NotNull
@NonNls
public String getShortName() {
return "RedundantThrowsDeclaration";
}
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
final Set<ProblemDescriptor> problems = new HashSet<ProblemDescriptor>();
file.accept(new JavaRecursiveElementWalkingVisitor() {
@Override public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
final ProblemDescriptor descriptor = checkExceptionsNeverThrown(reference, manager, isOnTheFly);
if (descriptor != null) {
problems.add(descriptor);
}
}
});
return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
}
private static ProblemDescriptor checkExceptionsNeverThrown(PsiJavaCodeReferenceElement referenceElement,
InspectionManager inspectionManager,
boolean onTheFly) {
if (!(referenceElement.getParent() instanceof PsiReferenceList)) return null;
PsiReferenceList referenceList = (PsiReferenceList)referenceElement.getParent();
if (!(referenceList.getParent() instanceof PsiMethod)) return null;
PsiMethod method = (PsiMethod)referenceList.getParent();
if (referenceList != method.getThrowsList()) return null;
PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return null;
PsiManager manager = referenceElement.getManager();
PsiClassType exceptionType = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createType(referenceElement);
if (ExceptionUtil.isUncheckedExceptionOrSuperclass(exceptionType)) return null;
PsiCodeBlock body = method.getBody();
if (body == null) return null;
PsiModifierList modifierList = method.getModifierList();
if (!modifierList.hasModifierProperty(PsiModifier.PRIVATE)
&& !modifierList.hasModifierProperty(PsiModifier.STATIC)
&& !modifierList.hasModifierProperty(PsiModifier.FINAL)
&& !method.isConstructor()
&& !(containingClass instanceof PsiAnonymousClass)
&& !containingClass.hasModifierProperty(PsiModifier.FINAL)) {
return null;
}
Collection<PsiClassType> types = ExceptionUtil.collectUnhandledExceptions(body, method);
Collection<PsiClassType> unhandled = new HashSet<PsiClassType>(types);
if (method.isConstructor()) {
// there may be field initializer throwing exception
// that exception must be caught in the constructor
PsiField[] fields = containingClass.getFields();
for (final PsiField field : fields) {
if (field.hasModifierProperty(PsiModifier.STATIC)) continue;
PsiExpression initializer = field.getInitializer();
if (initializer == null) continue;
unhandled.addAll(ExceptionUtil.collectUnhandledExceptions(initializer, field));
}
}
for (PsiClassType unhandledException : unhandled) {
if (unhandledException.isAssignableFrom(exceptionType) || exceptionType.isAssignableFrom(unhandledException)) {
return null;
}
}
if (HighlightMethodUtil.isSerializationRelatedMethod(method, containingClass)) return null;
String description = JavaErrorMessages.message("exception.is.never.thrown", HighlightUtil.formatType(exceptionType));
LocalQuickFix quickFixes = new DeleteThrowsFix(method, exceptionType);
return inspectionManager.createProblemDescriptor(referenceElement, description, quickFixes, ProblemHighlightType.LIKE_UNUSED_SYMBOL, onTheFly);
}
}
|