From 973060cc0e0f5ecba51129470bb65af9a6996398 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 1 Mar 2012 14:45:22 +0100 Subject: [PATCH] add option to ignore in test code to some inspections --- .../siyeh/InspectionGadgetsBundle.properties | 2 +- .../ig/abstraction/MagicNumberInspection.java | 37 +++--- .../EmptyCatchBlockInspection.java | 22 +--- .../TooBroadCatchInspection.java | 119 +++++++++--------- .../TooBroadThrowsInspection.java | 19 ++- .../UnusedCatchParameterInspection.java | 22 +--- .../StringConcatenationInspection.java | 41 ++---- .../src/com/siyeh/ig/psiutils/TestUtils.java | 21 ++-- .../inspectionDescriptions/MagicNumber.html | 4 +- .../inspectionDescriptions/TooBroadCatch.html | 4 + .../TooBroadThrows.html | 4 + .../UnusedCatchParameter.html | 5 +- 12 files changed, 137 insertions(+), 163 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index cd07c42dfc12..aaa04a063614 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -352,7 +352,7 @@ string.concatenation.ignore.system.err.option=Ignore for 'System.err.print' argu string.concatenation.ignore.assert.option=Ignore for assert statement arguments string.concatenation.ignore.exceptions.option=Ignore for throwable arguments string.concatenation.ignore.constant.initializers.option=Ignore for initializers of constants -string.concatenation.ignore.in.test.code=Ignore in test code +ignore.in.test.code=Ignore in test code string.equalsignorecase.call.display.name=Call to 'String.equalsIgnoreCase()' string.equalsignorecase.call.problem.descriptor=String.#ref() using internationalized strings #loc string.equals.call.display.name=Call to 'String.equals()' diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/abstraction/MagicNumberInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/abstraction/MagicNumberInspection.java index dfc35b435212..0857b04f3888 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/abstraction/MagicNumberInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/abstraction/MagicNumberInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ */ package com.siyeh.ig.abstraction; -import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.InspectionGadgetsBundle; @@ -26,16 +26,19 @@ import com.siyeh.ig.fixes.IntroduceConstantFix; import com.siyeh.ig.psiutils.ClassUtils; import com.siyeh.ig.psiutils.ExpressionUtils; import com.siyeh.ig.psiutils.MethodUtils; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.NotNull; -import javax.swing.JComponent; +import javax.swing.*; public class MagicNumberInspection extends BaseInspection { /** * @noinspection PublicField */ - public boolean m_ignoreInHashCode = true; + public boolean ignoreInHashCode = true; + + public boolean ignoreInTestCode = false; @Override @NotNull @@ -46,16 +49,15 @@ public class MagicNumberInspection extends BaseInspection { @Override @NotNull public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "magic.number.problem.descriptor"); + return InspectionGadgetsBundle.message("magic.number.problem.descriptor"); } @Override public JComponent createOptionsPanel() { - return new SingleCheckboxOptionsPanel( - InspectionGadgetsBundle.message( - "magic.number.ignore.option"), - this, "m_ignoreInHashCode"); + final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + panel.addCheckbox(InspectionGadgetsBundle.message("magic.number.ignore.option"), "ignoreInHashCode"); + panel.addCheckbox(InspectionGadgetsBundle.message("ignore.in.test.code"), "ignoreInTestCode"); + return panel; } @Override @@ -76,8 +78,7 @@ public class MagicNumberInspection extends BaseInspection { private class MagicNumberVisitor extends BaseInspectionVisitor { @Override - public void visitLiteralExpression( - @NotNull PsiLiteralExpression expression) { + public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) { super.visitLiteralExpression(expression); final PsiType type = expression.getType(); if (!ClassUtils.isPrimitiveNumericType(type)) { @@ -92,14 +93,15 @@ public class MagicNumberInspection extends BaseInspection { if (ExpressionUtils.isDeclaredConstant(expression)) { return; } - if (m_ignoreInHashCode) { - final PsiMethod containingMethod = - PsiTreeUtil.getParentOfType(expression, - PsiMethod.class); + if (ignoreInHashCode) { + final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class); if (MethodUtils.isHashCode(containingMethod)) { return; } } + if (ignoreInTestCode && TestUtils.isInTestCode(expression)) { + return; + } final PsiElement parent = expression.getParent(); if (parent instanceof PsiPrefixExpression) { registerError(parent); @@ -110,8 +112,7 @@ public class MagicNumberInspection extends BaseInspection { } private boolean isSpecialCaseLiteral(PsiLiteralExpression expression) { - final Object object = - ExpressionUtils.computeConstantExpression(expression); + final Object object = ExpressionUtils.computeConstantExpression(expression); if (object instanceof Integer) { final int i = ((Integer)object).intValue(); return i >= 0 && i <= 10 || i == 100 || i == 1000; diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyCatchBlockInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyCatchBlockInspection.java index 4838ae6065a8..2e4255f5bd5a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyCatchBlockInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyCatchBlockInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,10 @@ */ package com.siyeh.ig.errorhandling; -import com.intellij.codeInsight.TestFrameworks; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; -import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -128,25 +126,15 @@ public class EmptyCatchBlockInspection extends BaseInspection { private class EmptyCatchBlockVisitor extends BaseInspectionVisitor { @Override - public void visitTryStatement( - @NotNull PsiTryStatement statement) { + public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { return; } - if (m_ignoreTestCases) { - if (TestUtils.isPartOfJUnitTestMethod(statement)) { - return; - } - final PsiClass containingClass = - PsiTreeUtil.getParentOfType(statement, PsiClass.class); - if (containingClass != null && - TestFrameworks.getInstance().isTestClass(containingClass)) { - return; - } + if (m_ignoreTestCases && TestUtils.isInTestCode(statement)) { + return; } - final PsiCatchSection[] catchSections = - statement.getCatchSections(); + final PsiCatchSection[] catchSections = statement.getCatchSections(); for (final PsiCatchSection section : catchSections) { checkCatchSection(section); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadCatchInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadCatchInspection.java index d0cfd2fadeab..9fde8cd1ace8 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadCatchInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadCatchInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package com.siyeh.ig.errorhandling; import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ScrollType; @@ -31,6 +31,7 @@ import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.ExceptionUtils; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -41,6 +42,9 @@ public class TooBroadCatchInspection extends BaseInspection { @SuppressWarnings({"PublicField"}) public boolean onlyWarnOnRootExceptions = false; + @SuppressWarnings("UnusedDeclaration") + public boolean ignoreInTestCode = false; + @Override @NotNull public String getID() { @@ -91,12 +95,14 @@ public class TooBroadCatchInspection extends BaseInspection { @Override public JComponent createOptionsPanel() { - return new SingleCheckboxOptionsPanel( - InspectionGadgetsBundle.message("too.broad.catch.option"), - this, "onlyWarnOnRootExceptions"); + final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + panel.addCheckbox(InspectionGadgetsBundle.message("too.broad.catch.option"), "onlyWarnOnRootExceptions"); + panel.addCheckbox(InspectionGadgetsBundle.message("ignore.in.test.code"), "ignoreInTestCode"); + return panel; } private static class AddCatchSectionFix extends InspectionGadgetsFix { + private final SmartPsiElementPointer myThrown; private final String myText; @@ -105,57 +111,54 @@ public class TooBroadCatchInspection extends BaseInspection { myText = thrown.getName(); } + @NotNull + public String getName() { + return InspectionGadgetsBundle.message("too.broad.catch.quickfix", myText); + } + @Override - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { - PsiElement typeElement = descriptor.getPsiElement(); - if (typeElement == null) return; - PsiElement catchParameter = typeElement.getParent(); - if (!(catchParameter instanceof PsiParameter)) return; - PsiElement catchBlock = ((PsiParameter)catchParameter).getDeclarationScope(); - if (!(catchBlock instanceof PsiCatchSection)) return; + protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { + final PsiElement typeElement = descriptor.getPsiElement(); + if (typeElement == null) { + return; + } + final PsiElement catchParameter = typeElement.getParent(); + if (!(catchParameter instanceof PsiParameter)) { + return; + } + final PsiElement catchBlock = ((PsiParameter)catchParameter).getDeclarationScope(); + if (!(catchBlock instanceof PsiCatchSection)) { + return; + } final PsiCatchSection myBeforeCatchSection = (PsiCatchSection)catchBlock; final PsiTryStatement myTryStatement = myBeforeCatchSection.getTryStatement(); - final JavaCodeStyleManager codeStyleManager = - JavaCodeStyleManager.getInstance(project); - final String name = codeStyleManager.suggestUniqueVariableName("e", - myTryStatement.getTryBlock(), false); - final PsiElementFactory factory = - JavaPsiFacade.getInstance(project).getElementFactory(); - PsiClass aClass = myThrown.getElement(); - if (aClass == null) return; - final PsiCatchSection section = - factory.createCatchSection(factory.createType(aClass), name, - myTryStatement); - final PsiCatchSection element = (PsiCatchSection) - myTryStatement.addBefore(section, myBeforeCatchSection); + final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); + final String name = codeStyleManager.suggestUniqueVariableName("e", myTryStatement.getTryBlock(), false); + final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); + final PsiClass aClass = myThrown.getElement(); + if (aClass == null) { + return; + } + final PsiCatchSection section = factory.createCatchSection(factory.createType(aClass), name, myTryStatement); + final PsiCatchSection element = (PsiCatchSection)myTryStatement.addBefore(section, myBeforeCatchSection); codeStyleManager.shortenClassReferences(element); if (isOnTheFly()) { final TextRange range = getRangeToSelect(element.getCatchBlock()); final PsiFile file = element.getContainingFile(); - final Editor editor = FileEditorManager.getInstance(project) - .getSelectedTextEditor(); + final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) { return; } - final Document document = PsiDocumentManager - .getInstance(project).getDocument(file); + final Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (editor.getDocument() != document) { return; } editor.getCaretModel().moveToOffset(range.getStartOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); - editor.getSelectionModel().setSelection(range.getStartOffset(), - range.getEndOffset()); + editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset()); } } - - @NotNull - public String getName() { - return InspectionGadgetsBundle.message("too.broad.catch.quickfix", - myText); - } } private static TextRange getRangeToSelect(PsiCodeBlock block) { @@ -187,24 +190,22 @@ public class TooBroadCatchInspection extends BaseInspection { return new TooBroadCatchVisitor(); } - private class TooBroadCatchVisitor - extends BaseInspectionVisitor { + private class TooBroadCatchVisitor extends BaseInspectionVisitor { @Override - public void visitTryStatement( - @NotNull PsiTryStatement statement) { + public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); final PsiCodeBlock tryBlock = statement.getTryBlock(); if (tryBlock == null) { return; } - final Set exceptionsThrown = - ExceptionUtils.calculateExceptionsThrown(tryBlock); + if (ignoreInTestCode && TestUtils.isInTestCode(statement)) { + return; + } + final Set exceptionsThrown = ExceptionUtils.calculateExceptionsThrown(tryBlock); final int numExceptionsThrown = exceptionsThrown.size(); - final Set exceptionsCaught = - new HashSet(numExceptionsThrown); - final PsiCatchSection[] catchSections = - statement.getCatchSections(); + final Set exceptionsCaught = new HashSet(numExceptionsThrown); + final PsiCatchSection[] catchSections = statement.getCatchSections(); for (final PsiCatchSection catchSection : catchSections) { final PsiParameter parameter = catchSection.getParameter(); if (parameter == null) { @@ -212,10 +213,8 @@ public class TooBroadCatchInspection extends BaseInspection { } final PsiType typeCaught = parameter.getType(); if (typeCaught instanceof PsiDisjunctionType) { - final PsiDisjunctionType disjunctionType = - (PsiDisjunctionType)typeCaught; - final List types = - disjunctionType.getDisjunctions(); + final PsiDisjunctionType disjunctionType = (PsiDisjunctionType)typeCaught; + final List types = disjunctionType.getDisjunctions(); for (PsiType type : types) { register(exceptionsThrown, exceptionsCaught, parameter, type); } @@ -227,29 +226,23 @@ public class TooBroadCatchInspection extends BaseInspection { } private void register(Set exceptionsThrown, Set exceptionsCaught, PsiParameter parameter, PsiType type) { - final List maskedExceptions = - findMaskedExceptions(exceptionsThrown, - exceptionsCaught, type); + final List maskedExceptions = findMaskedExceptions(exceptionsThrown, exceptionsCaught, type); if (!maskedExceptions.isEmpty()) { - final PsiTypeElement typeElement = - parameter.getTypeElement(); + final PsiTypeElement typeElement = parameter.getTypeElement(); registerError(typeElement, maskedExceptions); } } - private List findMaskedExceptions( - Set exceptionsThrown, - Set exceptionsCaught, PsiType typeCaught) { + private List findMaskedExceptions(Set exceptionsThrown, Set exceptionsCaught, PsiType typeCaught) { if (exceptionsThrown.contains(typeCaught)) { exceptionsCaught.add(typeCaught); exceptionsThrown.remove(typeCaught); } final List typesMasked = new ArrayList(); for (PsiClassType typeThrown : exceptionsThrown) { - if (!exceptionsCaught.contains(typeThrown) && - typeCaught.isAssignableFrom(typeThrown)) { + if (!exceptionsCaught.contains(typeThrown) && typeCaught.isAssignableFrom(typeThrown)) { exceptionsCaught.add(typeThrown); - PsiClass aClass = typeThrown.resolve(); + final PsiClass aClass = typeThrown.resolve(); if (aClass != null) { typesMasked.add(aClass); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadThrowsInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadThrowsInspection.java index c8a5aa89e85c..c6b4143b739a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadThrowsInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/TooBroadThrowsInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 Bas Leijdekkers + * Copyright 2010-2012 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package com.siyeh.ig.errorhandling; import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.util.IncorrectOperationException; @@ -26,6 +26,7 @@ import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.ExceptionUtils; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -36,6 +37,9 @@ public class TooBroadThrowsInspection extends BaseInspection { @SuppressWarnings({"PublicField"}) public boolean onlyWarnOnRootExceptions = false; + @SuppressWarnings("UnusedDeclaration") + public boolean ignoreInTestCode = false; + @Override @NotNull public String getID() { @@ -77,7 +81,10 @@ public class TooBroadThrowsInspection extends BaseInspection { @Override public JComponent createOptionsPanel() { - return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("too.broad.catch.option"), this, "onlyWarnOnRootExceptions"); + final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + panel.addCheckbox(InspectionGadgetsBundle.message("too.broad.catch.option"), "onlyWarnOnRootExceptions"); + panel.addCheckbox(InspectionGadgetsBundle.message("ignore.in.test.code"), "ignoreInTestCode"); + return panel; } @NotNull @@ -137,8 +144,7 @@ public class TooBroadThrowsInspection extends BaseInspection { return new TooBroadThrowsVisitor(); } - private class TooBroadThrowsVisitor - extends BaseInspectionVisitor { + private class TooBroadThrowsVisitor extends BaseInspectionVisitor { @Override public void visitMethod(PsiMethod method) { @@ -155,6 +161,9 @@ public class TooBroadThrowsInspection extends BaseInspection { if (body == null) { return; } + if (ignoreInTestCode && TestUtils.isInTestCode(method)) { + return; + } final Set exceptionsThrown = ExceptionUtils.calculateExceptionsThrown(body); final PsiClassType[] referencedExceptions = throwsList.getReferencedTypes(); final Set exceptionsDeclared = new HashSet(referencedExceptions.length); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/UnusedCatchParameterInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/UnusedCatchParameterInspection.java index f70f243fef32..7d68417d133d 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/UnusedCatchParameterInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/UnusedCatchParameterInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,8 @@ */ package com.siyeh.ig.errorhandling; -import com.intellij.codeInsight.TestFrameworks; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.psi.*; -import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -93,22 +91,12 @@ public class UnusedCatchParameterInspection extends BaseInspection { private class UnusedCatchParameterVisitor extends BaseInspectionVisitor { @Override - public void visitTryStatement( - @NotNull PsiTryStatement statement) { + public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); - if (m_ignoreTestCases) { - final PsiClass containingClass = - PsiTreeUtil.getParentOfType(statement, PsiClass.class); - if (containingClass != null && - TestFrameworks.getInstance().isTestClass(containingClass)) { - return; - } - if (TestUtils.isPartOfJUnitTestMethod(statement)) { - return; - } + if (m_ignoreTestCases && TestUtils.isInTestCode(statement)) { + return; } - final PsiCatchSection[] catchSections = - statement.getCatchSections(); + final PsiCatchSection[] catchSections = statement.getCatchSections(); for (PsiCatchSection catchSection : catchSections) { checkCatchSection(catchSection); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/internationalization/StringConcatenationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/internationalization/StringConcatenationInspection.java index 45e0bd13717f..05d324243be2 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/internationalization/StringConcatenationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/internationalization/StringConcatenationInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package com.siyeh.ig.internationalization; import com.intellij.codeInsight.AnnotationUtil; -import com.intellij.codeInsight.TestFrameworks; import com.intellij.codeInsight.intention.AddAnnotationFix; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.psi.*; @@ -149,26 +148,14 @@ public class StringConcatenationInspection extends BaseInspection { @Override @Nullable public JComponent createOptionsPanel() { - final MultipleCheckboxOptionsPanel optionsPanel = - new MultipleCheckboxOptionsPanel(this); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "string.concatenation.ignore.assert.option"), - "ignoreAsserts"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "string.concatenation.ignore.system.out.option"), - "ignoreSystemOuts"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "string.concatenation.ignore.system.err.option"), - "ignoreSystemErrs"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "string.concatenation.ignore.exceptions.option"), - "ignoreThrowableArguments"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "string.concatenation.ignore.constant.initializers.option"), + final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("string.concatenation.ignore.assert.option"), "ignoreAsserts"); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("string.concatenation.ignore.system.out.option"), "ignoreSystemOuts"); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("string.concatenation.ignore.system.err.option"), "ignoreSystemErrs"); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("string.concatenation.ignore.exceptions.option"), "ignoreThrowableArguments"); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("string.concatenation.ignore.constant.initializers.option"), "ignoreConstantInitializers"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "string.concatenation.ignore.in.test.code"), - "ignoreInTestCode"); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("ignore.in.test.code"), "ignoreInTestCode"); return optionsPanel; } @@ -201,16 +188,8 @@ public class StringConcatenationInspection extends BaseInspection { if (isInsideAnnotation(expression)) { return; } - if (ignoreInTestCode) { - if (TestUtils.isPartOfJUnitTestMethod(expression)) { - return; - } - final PsiClass containingClass = - PsiTreeUtil.getParentOfType(expression, PsiClass.class); - if (containingClass != null && - TestFrameworks.getInstance().isTestClass(containingClass)) { - return; - } + if (ignoreInTestCode && TestUtils.isInTestCode(expression)) { + return; } if (ignoreAsserts) { final PsiAssertStatement assertStatement = diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/TestUtils.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/TestUtils.java index 3a260323f95a..3e3381c305c5 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/TestUtils.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/TestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.siyeh.ig.psiutils; import com.intellij.codeInsight.AnnotationUtil; +import com.intellij.codeInsight.TestFrameworks; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; @@ -49,8 +50,7 @@ public class TestUtils { } public static boolean isPartOfJUnitTestMethod(@NotNull PsiElement element) { - final PsiMethod method = PsiTreeUtil.getParentOfType(element, - PsiMethod.class); + final PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false); return method != null && isJUnitTestMethod(method); } @@ -98,13 +98,18 @@ public class TestUtils { } public static boolean isJUnit4TestMethod(@Nullable PsiMethod method) { - return method != null && - AnnotationUtil.isAnnotated(method, "org.junit.Test", true); + return method != null && AnnotationUtil.isAnnotated(method, "org.junit.Test", true); } public static boolean isJUnitTestClass(@Nullable PsiClass targetClass) { - return targetClass != null && - InheritanceUtil.isInheritor(targetClass, - "junit.framework.TestCase"); + return targetClass != null && InheritanceUtil.isInheritor(targetClass, "junit.framework.TestCase"); + } + + public static boolean isInTestCode(PsiElement element) { + if (isPartOfJUnitTestMethod(element)) { + return true; + } + final PsiClass containingClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); + return containingClass != null && TestFrameworks.getInstance().isTestClass(containingClass); } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/MagicNumber.html b/plugins/InspectionGadgets/src/inspectionDescriptions/MagicNumber.html index 812cacb95854..38e1d5b28aff 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/MagicNumber.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/MagicNumber.html @@ -5,7 +5,9 @@ This inspection reports "magic numbers", literal numeric constants used without number" is changed in one code location but not another. The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000, 0L, 1L, 2L, 0.0, 1.0, 0.0F and 1.0F are not reported by this inspection.

-Use the checkbox below to disable this inspection within 'hashCode()' methods. +Use the first checkbox below to disable this inspection within hashCode() methods. +

+Use the second checkbox below to ignore magic numbers in test code.

Powered by InspectionGadgets diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadCatch.html b/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadCatch.html index f05a86fef324..8b5e20ed78ea 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadCatch.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadCatch.html @@ -3,6 +3,10 @@ This inspection reports catch blocks which have parameters which are more generic than the exceptions thrown by the corresponding try block.

+Use the first checkbox below to have this inspection only warn on the most generic exceptions. +

+Use the second checkbox below to ignore overly broad catch blocks in test code. +

Powered by InspectionGadgets \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadThrows.html b/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadThrows.html index d91ea43283bf..16bd11038702 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadThrows.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/TooBroadThrows.html @@ -3,6 +3,10 @@ This inspection reports throws clauses which contain exceptions which are more generic than the exceptions actually thrown by the method.

+Use the first checkbox below to have this inspection only warn on the most generic exceptions. +

+Use the second checkbox below to ignore overly broad throws clauses in test code. +

New in 10, Powered by InspectionGadgets \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnusedCatchParameter.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnusedCatchParameter.html index f5fb424b021c..202a563c522e 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnusedCatchParameter.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnusedCatchParameter.html @@ -5,8 +5,9 @@ corresponding blocks. This inspection will not report any catch parameter named "ignore" or "ignored". Conversely this inspection will warn on any catch parameters named "ignore" or "ignored" that are actually used.

-Use the checkboxes below to disable this inspection for catch -blocks with comments, catch blocks in test code. +Use the first checkbox below to disable this inspection for catch blocks with comments. +

+Use the second checkbox below to ignore catch blocks in test code.

Powered by InspectionGadgets