update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInspection / duplicateThrows / DuplicateThrowsInspection.java
blobf3ad6f12a26614b4f88daa566a8edd3ef3400aae
1 /*
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.duplicateThrows;
18 import com.intellij.codeInsight.daemon.GroupNames;
19 import com.intellij.codeInspection.DeleteThrowsFix;
20 import com.intellij.codeInspection.InspectionsBundle;
21 import com.intellij.codeInspection.ProblemHighlightType;
22 import com.intellij.codeInspection.ProblemsHolder;
23 import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
24 import com.intellij.psi.*;
25 import org.jetbrains.annotations.NotNull;
27 public class DuplicateThrowsInspection extends BaseLocalInspectionTool {
28 @NotNull
29 public String getDisplayName() {
30 return InspectionsBundle.message("inspection.duplicate.throws.display.name");
33 @NotNull
34 public String getGroupDisplayName() {
35 return GroupNames.DECLARATION_REDUNDANCY;
38 @NotNull
39 public String getShortName() {
40 return "DuplicateThrows";
44 @NotNull
45 public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
46 return new JavaElementVisitor() {
47 @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
50 @Override public void visitMethod(PsiMethod method) {
51 PsiReferenceList throwsList = method.getThrowsList();
52 PsiJavaCodeReferenceElement[] refs = throwsList.getReferenceElements();
53 PsiClassType[] types = throwsList.getReferencedTypes();
54 outer:
55 for (int i = 0; i < types.length; i++) {
56 PsiClassType type = types[i];
57 for (int j = i+1; j < types.length; j++) {
58 PsiClassType otherType = types[j];
59 String problem = null;
60 PsiJavaCodeReferenceElement ref = refs[i];
61 if (type.equals(otherType)) {
62 problem = InspectionsBundle.message("inspection.duplicate.throws.problem");
64 else if (otherType.isAssignableFrom(type)) {
65 problem = InspectionsBundle.message("inspection.duplicate.throws.more.general.problem", otherType.getCanonicalText());
67 else if (type.isAssignableFrom(otherType)) {
68 problem = InspectionsBundle.message("inspection.duplicate.throws.more.general.problem", type.getCanonicalText());
69 ref = refs[j];
70 type = otherType;
72 if (problem != null) {
73 holder.registerProblem(ref, problem, ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DeleteThrowsFix(method, type));
74 //break outer;