From 5d2c8a92a77f46876560e7ec97b5d29ec184fbd9 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 4 Nov 2013 20:39:42 +0100 Subject: [PATCH] part 2 of IDEA-106425 (Ignore some 'Code maturity issues'-inspections for test code) --- .../siyeh/InspectionGadgetsBundle.properties | 4 +- .../ig/maturity/SystemOutErrInspection.java | 24 ++++++-- .../maturity/SystemOutErrInspectionTest.java | 55 +++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SystemOutErrInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 2cebfb936284..fac473321a73 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -258,7 +258,7 @@ use.obsolete.collection.type.ignore.library.arguments.option=Ignore obsolete col inspection.suppression.annotation.display.name=Inspection suppression annotation inspection.suppression.annotation.problem.descriptor=Inspection suppression annotation #ref #loc use.system.out.err.display.name=Use of System.out or System.err -use.system.out.err.problem.descriptor=Uses of 'System.out' and 'System.err' should probably be replaced with more robust logging #loc +use.system.out.err.problem.descriptor=Uses of #ref should probably be replaced with more robust logging #loc dumpstack.call.display.name=Call to 'Thread.dumpStack()' dumpstack.call.problem.descriptor=Call to Thread.#ref() should probably be replaced with more robust logging #loc printstacktrace.call.display.name=Call to 'printStackTrace()' @@ -2050,4 +2050,4 @@ package.info.without.package.quickfix=add ''package {0};'' package.info.without.package.family.quickfix=add package statement auto.closeable.resource.display.name=AutoCloseable used without 'try'-with-resources auto.closeable.resource.problem.descriptor=''{0}'' used without ''try''-with-resources statement -auto.closeable.resource.returned.option=Ignore AutoCloseable instances returned from method calls \ No newline at end of file +auto.closeable.resource.returned.option=Ignore AutoCloseable instances returned from method calls diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java index a0f08cf62116..3c8188ac5091 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 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,6 +15,7 @@ */ package com.siyeh.ig.maturity; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiField; @@ -23,10 +24,17 @@ import com.siyeh.HardcodedMethodConstants; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; public class SystemOutErrInspection extends BaseInspection { + @SuppressWarnings("PublicField") + public boolean ignoreInTestCode = false; + @Override @NotNull public String getID() { @@ -47,16 +55,21 @@ public class SystemOutErrInspection extends BaseInspection { "use.system.out.err.problem.descriptor"); } + @Nullable + @Override + public JComponent createOptionsPanel() { + return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("ignore.in.test.code"), this, "ignoreInTestCode"); + } + @Override public BaseInspectionVisitor buildVisitor() { return new SystemOutErrVisitor(); } - private static class SystemOutErrVisitor extends BaseInspectionVisitor { + private class SystemOutErrVisitor extends BaseInspectionVisitor { @Override - public void visitReferenceExpression( - @NotNull PsiReferenceExpression expression) { + public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); final String name = expression.getReferenceName(); if (!HardcodedMethodConstants.OUT.equals(name) && @@ -76,6 +89,9 @@ public class SystemOutErrInspection extends BaseInspection { if (!"java.lang.System".equals(className)) { return; } + if (ignoreInTestCode && TestUtils.isInTestCode(expression)) { + return; + } registerError(expression); } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SystemOutErrInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SystemOutErrInspectionTest.java new file mode 100644 index 000000000000..382f76dee124 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SystemOutErrInspectionTest.java @@ -0,0 +1,55 @@ +/* + * 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.siyeh.ig.maturity; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class SystemOutErrInspectionTest extends LightInspectionTestCase { + + public void testSimple() { + doStatementTest("/*Uses of 'System.out' should probably be replaced with more robust logging*/System.out/**/.println(\"debugging\");"); + } + + public void testInTest() { + addEnvironmentClass("package org.junit;" + + "@Retention(RetentionPolicy.RUNTIME) " + + "@Target({ElementType.METHOD}) " + + "public @interface Test {}"); + doMemberTest("@org.junit.Test public void testSomething() {" + + " System.out.println(\"debugger\");" + + "}"); + } + + public void testMultiple() { + doMemberTest("public void foo() {" + + " /*Uses of 'System.out' should probably be replaced with more robust logging*/System.out/**/.println(0);" + + " /*Uses of 'System.err' should probably be replaced with more robust logging*/System.err/**/.println(0);" + + " final java.io.PrintStream out = /*Uses of 'System.out' should probably be replaced with more robust logging*/System.out/**/;" + + " final java.io.PrintStream err = /*Uses of 'System.err' should probably be replaced with more robust logging*/System.err/**/;" + + "}"); + } + + @Override + protected InspectionProfileEntry getInspection() { + final SystemOutErrInspection inspection = new SystemOutErrInspection(); + inspection.ignoreInTestCode = true; + return inspection; + } +}