diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SuppressionAnnotationInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SuppressionAnnotationInspectionBase.java index 104125a04d54..5d5305cb470b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SuppressionAnnotationInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/maturity/SuppressionAnnotationInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -57,26 +57,29 @@ public class SuppressionAnnotationInspectionBase extends BaseInspection { @Override public void visitComment(PsiComment comment) { super.visitComment(comment); - final String commentText = comment.getText(); final IElementType tokenType = comment.getTokenType(); if (!tokenType.equals(JavaTokenType.END_OF_LINE_COMMENT) && !tokenType.equals(JavaTokenType.C_STYLE_COMMENT)) { return; } - - if (commentText.length() > 2) { - @NonNls final String strippedComment = commentText.substring(2).trim(); - if (strippedComment.startsWith(SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME)) { - final String suppressedIds = JavaSuppressionUtil.getSuppressedInspectionIdsIn(comment); - final Iterable ids = suppressedIds != null ? StringUtil.tokenize(suppressedIds, "[, ]") : null; - if (ids != null) { - for (String id : ids) { - if (!myAllowedSuppressions.contains(id)) { - registerError(comment, comment); - break; - } - } - } + final String commentText = comment.getText(); + if (commentText.length() <= 2) { + return; + } + @NonNls final String strippedComment = commentText.substring(2).trim(); + if (!strippedComment.startsWith(SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME)) { + return; + } + final String suppressedIds = JavaSuppressionUtil.getSuppressedInspectionIdsIn(comment); + if (suppressedIds == null) { + registerError(comment, comment, Boolean.FALSE); + return; + } + final Iterable ids = StringUtil.tokenize(suppressedIds, ","); + for (String id : ids) { + if (!myAllowedSuppressions.contains(id)) { + registerError(comment, comment, Boolean.TRUE); + break; } } } @@ -95,7 +98,10 @@ public class SuppressionAnnotationInspectionBase extends BaseInspection { if (annotationParent instanceof PsiModifierList) { final Collection ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation((PsiModifierList)annotationParent); if (!myAllowedSuppressions.containsAll(ids)) { - registerError(annotation, annotation); + registerError(annotation, annotation, Boolean.TRUE); + } + else if (ids.isEmpty()) { + registerError(annotation, annotation, Boolean.FALSE); } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SuppressionAnnotationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SuppressionAnnotationInspection.java index b08eed04b2b9..e6c7f0d52ec5 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SuppressionAnnotationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/maturity/SuppressionAnnotationInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,8 +15,6 @@ */ package com.siyeh.ig.maturity; -import com.intellij.codeInsight.FileModificationService; -import com.intellij.codeInsight.daemon.impl.RemoveSuppressWarningAction; import com.intellij.codeInspection.*; import com.intellij.codeInspection.ui.ListEditForm; import com.intellij.openapi.project.Project; @@ -32,7 +30,7 @@ import com.siyeh.ig.InspectionGadgetsFix; import org.jetbrains.annotations.NotNull; import javax.swing.*; -import java.util.Collection; +import java.awt.*; /** * User: anna @@ -42,23 +40,24 @@ public class SuppressionAnnotationInspection extends SuppressionAnnotationInspec @Override public JComponent createOptionsPanel() { final ListEditForm form = new ListEditForm("Ignore suppressions", myAllowedSuppressions); - return form.getContentPanel(); + final JComponent panel = form.getContentPanel(); + panel.setPreferredSize(new Dimension(150, 100)); + return panel; } @NotNull @Override - protected InspectionGadgetsFix[] buildFixes(Object... infos) { - if (infos.length == 1) { - if (infos[0] instanceof PsiAnnotation) { - final PsiAnnotation annotation = (PsiAnnotation)infos[0]; - PsiElement parent = annotation.getParent(); - final Collection ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation((PsiModifierList)parent); - if (!ids.isEmpty()) { - return new InspectionGadgetsFix[]{new DelegatingFix(new RemoveAnnotationQuickFix(annotation, null)), new AllowSuppressionsFix()}; - } - } else if (infos[0] instanceof PsiComment) { - return new InspectionGadgetsFix[]{new RemoveSuppressCommentFix(), new AllowSuppressionsFix()}; - } + protected InspectionGadgetsFix[] buildFixes(Object... infos) { + final boolean suppressionIdPresent = ((Boolean)infos[1]).booleanValue(); + if (infos[0] instanceof PsiAnnotation) { + final PsiAnnotation annotation = (PsiAnnotation)infos[0]; + return suppressionIdPresent + ? new InspectionGadgetsFix[]{new DelegatingFix(new RemoveAnnotationQuickFix(annotation, null)), new AllowSuppressionsFix()} + : new InspectionGadgetsFix[]{new DelegatingFix(new RemoveAnnotationQuickFix(annotation, null))}; + } else if (infos[0] instanceof PsiComment) { + return suppressionIdPresent + ? new InspectionGadgetsFix[]{new RemoveSuppressCommentFix(), new AllowSuppressionsFix()} + : new InspectionGadgetsFix[]{new RemoveSuppressCommentFix()}; } return InspectionGadgetsFix.EMPTY_ARRAY; } @@ -68,7 +67,6 @@ public class SuppressionAnnotationInspection extends SuppressionAnnotationInspec protected void doFix(Project project, ProblemDescriptor descriptor) { PsiElement psiElement = descriptor.getPsiElement(); if (psiElement != null) { - if (!FileModificationService.getInstance().preparePsiElementForWrite(psiElement)) return; psiElement.delete(); } } @@ -90,16 +88,23 @@ public class SuppressionAnnotationInspection extends SuppressionAnnotationInspec @Override protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement psiElement = descriptor.getPsiElement(); - final String suppressedIds = JavaSuppressionUtil.getSuppressedInspectionIdsIn(psiElement); - final Iterable ids = suppressedIds != null ? StringUtil.tokenize(suppressedIds, "[, ]") : null; - if (ids != null) { - for (String id : ids) { - if (!myAllowedSuppressions.contains(id)) { - myAllowedSuppressions.add(id); - } - } - saveProfile(project); + final Iterable ids; + if (psiElement instanceof PsiAnnotation) { + ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation((PsiModifierList)psiElement.getParent()); } + else { + final String suppressedIds = JavaSuppressionUtil.getSuppressedInspectionIdsIn(psiElement); + if (suppressedIds == null) { + return; + } + ids = StringUtil.tokenize(suppressedIds, ","); + } + for (String id : ids) { + if (!myAllowedSuppressions.contains(id)) { + myAllowedSuppressions.add(id); + } + } + saveProfile(project); } private void saveProfile(Project project) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/maturity/suppression_annotation/SuppressionAnnotation.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/maturity/suppression_annotation/SuppressionAnnotation.java new file mode 100644 index 000000000000..7009b248020f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/maturity/suppression_annotation/SuppressionAnnotation.java @@ -0,0 +1,21 @@ +public class SuppressionAnnotation { + + @SuppressWarnings("PublicField") + public String s; + + @SuppressWarnings({}) + public String t; + + void foo() { + //noinspection HardCodedStringLiteral + System.out.println("hello"); + //noinspection + System.out.println(); + } + + @SuppressWarnings("FreeSpeech") + void bar() { + //noinspection FreeSpeech + System.out.println(); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SuppressionAnnotationInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SuppressionAnnotationInspectionTest.java new file mode 100644 index 000000000000..77764c191b4a --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/maturity/SuppressionAnnotationInspectionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2014 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; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class SuppressionAnnotationInspectionTest extends LightInspectionTestCase { + + public void testSuppressionAnnotation() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + final SuppressionAnnotationInspection inspection = new SuppressionAnnotationInspection(); + inspection.myAllowedSuppressions.add("FreeSpeech"); + return inspection; + } +} \ No newline at end of file