diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspectionBase.java similarity index 86% rename from plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java rename to plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspectionBase.java index fd1679da26b0..b382e2f70ea4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SystemOutErrInspectionBase.java @@ -23,12 +23,9 @@ import com.siyeh.HardcodedMethodConstants; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; -import com.siyeh.ig.InspectionGadgetsFix; -import com.siyeh.ig.fixes.SuppressForTestsScopeFix; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -public class SystemOutErrInspection extends BaseInspection { +public class SystemOutErrInspectionBase extends BaseInspection { @Override @NotNull @@ -50,12 +47,6 @@ public class SystemOutErrInspection extends BaseInspection { "use.system.out.err.problem.descriptor"); } - @Nullable - @Override - protected InspectionGadgetsFix buildFix(Object... infos) { - return new SuppressForTestsScopeFix(this); - } - @Override public BaseInspectionVisitor buildVisitor() { return new SystemOutErrVisitor(); @@ -84,7 +75,7 @@ public class SystemOutErrInspection extends BaseInspection { if (!"java.lang.System".equals(className)) { return; } - registerError(expression); + registerError(expression, expression); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspectionBase.java similarity index 86% rename from plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspection.java rename to plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspectionBase.java index ad27bf36e76c..69c9e5642457 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspectionBase.java @@ -20,13 +20,10 @@ import com.siyeh.HardcodedMethodConstants; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; -import com.siyeh.ig.InspectionGadgetsFix; -import com.siyeh.ig.fixes.SuppressForTestsScopeFix; import com.siyeh.ig.psiutils.MethodCallUtils; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -public class ThrowablePrintStackTraceInspection extends BaseInspection { +public class ThrowablePrintStackTraceInspectionBase extends BaseInspection { @Override @NotNull @@ -46,12 +43,6 @@ public class ThrowablePrintStackTraceInspection extends BaseInspection { return InspectionGadgetsBundle.message("printstacktrace.call.problem.descriptor"); } - @Nullable - @Override - protected InspectionGadgetsFix buildFix(Object... infos) { - return new SuppressForTestsScopeFix(this); - } - @Override public BaseInspectionVisitor buildVisitor() { return new ThrowablePrintStackTraceVisitor(); @@ -82,7 +73,7 @@ public class ThrowablePrintStackTraceInspection extends BaseInspection { if (!CommonClassNames.JAVA_LANG_THROWABLE.equals(name)) { return; } - registerMethodCallError(expression); + registerMethodCallError(expression, expression); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/SuppressForTestsScopeFix.java b/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/SuppressForTestsScopeFix.java similarity index 62% rename from plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/SuppressForTestsScopeFix.java rename to plugins/InspectionGadgets/src/com/siyeh/ig/fixes/SuppressForTestsScopeFix.java index ec2e8baf12b1..aa8c592ff3ef 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/SuppressForTestsScopeFix.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/SuppressForTestsScopeFix.java @@ -20,14 +20,21 @@ import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ex.InspectionProfileImpl; import com.intellij.codeInspection.ex.InspectionToolWrapper; +import com.intellij.openapi.command.undo.BasicUndoableAction; +import com.intellij.openapi.command.undo.UndoManager; +import com.intellij.openapi.command.undo.UnexpectedUndoException; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; +import com.intellij.psi.PsiElement; import com.intellij.psi.search.scope.packageSet.NamedScope; import com.intellij.psi.search.scope.packageSet.NamedScopesHolder; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Bas Leijdekkers @@ -36,10 +43,18 @@ public class SuppressForTestsScopeFix extends InspectionGadgetsFix { private final BaseInspection myInspection; - public SuppressForTestsScopeFix(BaseInspection inspection) { + private SuppressForTestsScopeFix(BaseInspection inspection) { myInspection = inspection; } + @Nullable + public static SuppressForTestsScopeFix build(BaseInspection inspection, PsiElement context) { + if (!TestUtils.isInTestSourceContent(context)) { + return null; + } + return new SuppressForTestsScopeFix(inspection); + } + @NotNull @Override public String getName() { @@ -58,7 +73,23 @@ public class SuppressForTestsScopeFix extends InspectionGadgetsFix { } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) { + protected void doFix(final Project project, ProblemDescriptor descriptor) { + addRemoveTestsScope(project, true); + final VirtualFile vFile = descriptor.getPsiElement().getContainingFile().getVirtualFile(); + UndoManager.getInstance(project).undoableActionPerformed(new BasicUndoableAction(vFile) { + @Override + public void undo() throws UnexpectedUndoException { + addRemoveTestsScope(project, false); + } + + @Override + public void redo() throws UnexpectedUndoException { + addRemoveTestsScope(project, true); + } + }); + } + + private void addRemoveTestsScope(Project project, boolean add) { final InspectionProjectProfileManager profileManager = InspectionProjectProfileManager.getInstance(project); final InspectionProfileImpl profile = (InspectionProfileImpl)profileManager.getInspectionProfile(); final String shortName = myInspection.getShortName(); @@ -69,6 +100,12 @@ public class SuppressForTestsScopeFix extends InspectionGadgetsFix { final NamedScope namedScope = NamedScopesHolder.getScope(project, "Tests"); final HighlightDisplayKey key = HighlightDisplayKey.find(shortName); final HighlightDisplayLevel level = profile.getErrorLevel(key, namedScope, project); - profile.addScope(tool, namedScope, level, false, project); + if (add) { + profile.addScope(tool, namedScope, level, false, project); + } + else { + profile.removeScope(shortName, 0, project); + } + profile.scopesChanged(); } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SystemOutErrInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SystemOutErrInspection.java new file mode 100644 index 000000000000..859efaa8e7bd --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SystemOutErrInspection.java @@ -0,0 +1,34 @@ +/* + * 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.psi.PsiElement; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.SuppressForTestsScopeFix; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class SystemOutErrInspection extends SystemOutErrInspectionBase { + + @Nullable + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiElement context = (PsiElement)infos[0]; + return SuppressForTestsScopeFix.build(this, context); + } +} diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspection.java new file mode 100644 index 000000000000..d53926f8d670 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/ThrowablePrintStackTraceInspection.java @@ -0,0 +1,34 @@ +/* + * 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.psi.PsiElement; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.SuppressForTestsScopeFix; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class ThrowablePrintStackTraceInspection extends ThrowablePrintStackTraceInspectionBase { + + @Nullable + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiElement context = (PsiElement)infos[0]; + return SuppressForTestsScopeFix.build(this, context); + } +}