IDEADEV-15934
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / errorhandling / UnusedCatchParameterInspection.java
blob36886336c04a98021391f29fecb2eac4cb49de76
1 /*
2 * Copyright 2003-2008 Dave Griffith, 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.errorhandling;
18 import com.intellij.codeInspection.ProblemDescriptor;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.psi.*;
21 import com.intellij.util.IncorrectOperationException;
22 import com.siyeh.InspectionGadgetsBundle;
23 import com.siyeh.ig.BaseInspection;
24 import com.siyeh.ig.BaseInspectionVisitor;
25 import com.siyeh.ig.InspectionGadgetsFix;
26 import com.siyeh.ig.psiutils.TestUtils;
27 import com.siyeh.ig.ui.MultipleCheckboxOptionsPanel;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 import javax.swing.*;
34 public class UnusedCatchParameterInspection extends BaseInspection {
36 /** @noinspection PublicField */
37 public boolean m_ignoreCatchBlocksWithComments = false;
38 /** @noinspection PublicField */
39 public boolean m_ignoreTestCases = false;
41 @NotNull
42 public String getDisplayName() {
43 return InspectionGadgetsBundle.message(
44 "unused.catch.parameter.display.name");
47 public JComponent createOptionsPanel() {
48 final MultipleCheckboxOptionsPanel optionsPanel =
49 new MultipleCheckboxOptionsPanel(this);
50 optionsPanel.addCheckbox(InspectionGadgetsBundle.message(
51 "unused.catch.parameter.ignore.catch.option"),
52 "m_ignoreCatchBlocksWithComments");
53 optionsPanel.addCheckbox(InspectionGadgetsBundle.message(
54 "unused.catch.parameter.ignore.empty.option"),
55 "m_ignoreTestCases");
56 return optionsPanel;
59 @NotNull
60 protected String buildErrorString(Object... infos) {
61 final boolean namedIgnoreButUsed = ((Boolean) infos[0]).booleanValue();
62 if (namedIgnoreButUsed) {
63 return InspectionGadgetsBundle.message(
64 "used.catch.parameter.named.ignore.problem.descriptor"
67 return InspectionGadgetsBundle.message(
68 "unused.catch.parameter.problem.descriptor");
71 @Nullable
72 protected InspectionGadgetsFix buildFix(Object... infos) {
73 final boolean namedIgnoreButUsed = ((Boolean) infos[0]).booleanValue();
74 if (namedIgnoreButUsed) {
75 return null;
77 return new UnusedCatchParameterFix();
80 private static class UnusedCatchParameterFix extends InspectionGadgetsFix {
82 @NotNull
83 public String getName() {
84 return InspectionGadgetsBundle.message(
85 "rename.catch.parameter.to.ignored");
88 protected void doFix(Project project, ProblemDescriptor descriptor)
89 throws IncorrectOperationException {
90 final PsiElement element = descriptor.getPsiElement();
91 if (!(element instanceof PsiIdentifier)) {
92 return;
94 final PsiIdentifier identifier = (PsiIdentifier)element;
95 final PsiElementFactory factory =
96 JavaPsiFacade.getInstance(project).getElementFactory();
97 final PsiIdentifier newIdentifier =
98 factory.createIdentifier("ignored");
99 identifier.replace(newIdentifier);
103 public BaseInspectionVisitor buildVisitor() {
104 return new UnusedCatchParameterVisitor();
107 private class UnusedCatchParameterVisitor extends BaseInspectionVisitor {
109 @Override public void visitTryStatement(
110 @NotNull PsiTryStatement statement) {
111 super.visitTryStatement(statement);
112 if (m_ignoreTestCases && TestUtils.isPartOfJUnitTestMethod(
113 statement)) {
114 return;
116 final PsiCatchSection[] catchSections =
117 statement.getCatchSections();
118 for (PsiCatchSection catchSection : catchSections) {
119 checkCatchSection(catchSection);
123 private void checkCatchSection(PsiCatchSection section) {
124 final PsiParameter parameter = section.getParameter();
125 if (parameter == null) {
126 return;
128 @NonNls final String parametername = parameter.getName();
129 final boolean namedIgnore = "ignore".equals(parametername) ||
130 "ignored".equals(parametername);
131 final PsiCodeBlock block = section.getCatchBlock();
132 if (block == null) {
133 return;
135 if (m_ignoreCatchBlocksWithComments) {
136 final PsiElement[] children = block.getChildren();
137 for (final PsiElement child : children) {
138 if (child instanceof PsiComment) {
139 return;
143 final CatchParameterUsedVisitor visitor =
144 new CatchParameterUsedVisitor(parameter);
145 block.accept(visitor);
146 if (visitor.isUsed()) {
147 if (namedIgnore) {
148 registerVariableError(parameter, Boolean.valueOf(true));
150 return;
151 } else if (namedIgnore) {
152 return;
154 registerVariableError(parameter, Boolean.valueOf(false));