IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / junit / MultipleExceptionsDeclaredOnTestMethodInspection.java
blobaaad522baac6fff52885f00532218a6a27807496
1 /*
2 * Copyright 2009 Bas Leijdekkers
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.siyeh.ig.junit;
18 import com.intellij.codeInspection.ProblemDescriptor;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.psi.*;
21 import com.intellij.psi.search.GlobalSearchScope;
22 import com.intellij.psi.search.searches.MethodReferencesSearch;
23 import com.intellij.util.IncorrectOperationException;
24 import com.intellij.util.Query;
25 import com.siyeh.InspectionGadgetsBundle;
26 import com.siyeh.ig.BaseInspection;
27 import com.siyeh.ig.BaseInspectionVisitor;
28 import com.siyeh.ig.InspectionGadgetsFix;
29 import com.siyeh.ig.psiutils.TestUtils;
30 import org.jetbrains.annotations.Nls;
31 import org.jetbrains.annotations.NotNull;
33 import java.text.MessageFormat;
35 public class MultipleExceptionsDeclaredOnTestMethodInspection
36 extends BaseInspection {
38 @Nls
39 @NotNull
40 @Override
41 public String getDisplayName() {
42 return InspectionGadgetsBundle.message(
43 "multiple.exceptions.declared.on.test.method.display.name");
46 @NotNull
47 @Override
48 protected String buildErrorString(Object... infos) {
49 return InspectionGadgetsBundle.message(
50 "multiple.exceptions.declared.on.test.method.problem.descriptor");
53 @Override
54 protected InspectionGadgetsFix buildFix(Object... infos) {
55 return new MultipleExceptionsDeclaredOnTestMethodFix();
58 private class MultipleExceptionsDeclaredOnTestMethodFix
59 extends InspectionGadgetsFix {
61 @NotNull
62 public String getName() {
63 return InspectionGadgetsBundle.message(
64 "multiple.exceptions.declared.on.test.method.quickfix");
67 @Override
68 protected void doFix(Project project, ProblemDescriptor descriptor)
69 throws IncorrectOperationException {
70 final PsiElement element = descriptor.getPsiElement();
71 if (!(element instanceof PsiReferenceList)) {
72 return;
74 final PsiReferenceList referenceList = (PsiReferenceList) element;
75 final PsiJavaCodeReferenceElement[] referenceElements =
76 referenceList.getReferenceElements();
77 for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
78 referenceElement.delete();
80 final PsiElementFactory factory = JavaPsiFacade.getElementFactory(
81 project);
82 final GlobalSearchScope scope = referenceList.getResolveScope();
83 final PsiJavaCodeReferenceElement referenceElement =
84 factory.createReferenceElementByFQClassName(
85 "java.lang.Exception", scope);
86 referenceList.add(referenceElement);
90 @Override
91 public BaseInspectionVisitor buildVisitor() {
92 return new RedundantExceptionDeclarationVisitor();
95 private class RedundantExceptionDeclarationVisitor
96 extends BaseInspectionVisitor {
98 @Override
99 public void visitMethod(PsiMethod method) {
100 super.visitMethod(method);
101 if (!TestUtils.isJUnitTestMethod(method)) {
102 return;
104 final PsiReferenceList throwsList = method.getThrowsList();
105 final PsiJavaCodeReferenceElement[] referenceElements =
106 throwsList.getReferenceElements();
107 if (referenceElements.length < 2) {
108 return;
111 final Query<PsiReference> query =
112 MethodReferencesSearch.search(method);
113 final PsiReference firstReference = query.findFirst();
114 if (firstReference != null) {
115 return;
117 registerError(throwsList);