mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
suppression warnings inspection: allow to accept some suppressions (IDEA-23392)
This commit is contained in:
@@ -160,7 +160,7 @@ public class JavaSuppressionUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
static Collection<String> getInspectionIdsSuppressedInAnnotation(@NotNull PsiModifierListOwner owner) {
|
||||
public static Collection<String> getInspectionIdsSuppressedInAnnotation(@NotNull PsiModifierListOwner owner) {
|
||||
if (!PsiUtil.isLanguageLevel5OrHigher(owner)) return Collections.emptyList();
|
||||
PsiModifierList modifierList = owner.getModifierList();
|
||||
return getInspectionIdsSuppressedInAnnotation(modifierList);
|
||||
|
||||
+17
-12
@@ -16,11 +16,9 @@
|
||||
package com.siyeh.ig.maturity;
|
||||
|
||||
import com.intellij.codeInspection.BatchSuppressManager;
|
||||
import com.intellij.codeInspection.SuppressionUtil;
|
||||
import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.psi.PsiAnnotation;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiJavaCodeReferenceElement;
|
||||
import com.intellij.codeInspection.JavaSuppressionUtil;
|
||||
import com.intellij.codeInspection.SuppressionUtilCore;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
@@ -28,7 +26,12 @@ import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SuppressionAnnotationInspection extends BaseInspection {
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class SuppressionAnnotationInspectionBase extends BaseInspection {
|
||||
public List<String> myAllowedSuppressions = new ArrayList<String>();
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
@@ -49,8 +52,7 @@ public class SuppressionAnnotationInspection extends BaseInspection {
|
||||
return new SuppressionAnnotationVisitor();
|
||||
}
|
||||
|
||||
private static class SuppressionAnnotationVisitor
|
||||
extends BaseInspectionVisitor {
|
||||
private class SuppressionAnnotationVisitor extends BaseInspectionVisitor {
|
||||
@Override
|
||||
public void visitComment(PsiComment comment) {
|
||||
super.visitComment(comment);
|
||||
@@ -61,7 +63,7 @@ public class SuppressionAnnotationInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
@NonNls final String strippedComment = commentText.substring(2).trim();
|
||||
if (strippedComment.startsWith(SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME)) {
|
||||
if (strippedComment.startsWith(SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME)) {
|
||||
registerError(comment);
|
||||
}
|
||||
}
|
||||
@@ -69,15 +71,18 @@ public class SuppressionAnnotationInspection extends BaseInspection {
|
||||
@Override
|
||||
public void visitAnnotation(PsiAnnotation annotation) {
|
||||
super.visitAnnotation(annotation);
|
||||
final PsiJavaCodeReferenceElement reference =
|
||||
annotation.getNameReferenceElement();
|
||||
final PsiJavaCodeReferenceElement reference = annotation.getNameReferenceElement();
|
||||
if (reference == null) {
|
||||
return;
|
||||
}
|
||||
@NonNls final String text = reference.getText();
|
||||
if ("SuppressWarnings".equals(text) ||
|
||||
BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME.equals(text)) {
|
||||
registerError(annotation);
|
||||
final Collection<String> ids =
|
||||
JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation((PsiModifierList)annotation.getParent());
|
||||
if (!myAllowedSuppressions.containsAll(ids)) {
|
||||
registerError(annotation, annotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.InspectionProfile;
|
||||
import com.intellij.codeInspection.JavaSuppressionUtil;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ui.ListEditForm;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.profile.codeInspection.InspectionProfileManager;
|
||||
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.PsiAnnotation;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiModifierList;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 10/24/13
|
||||
*/
|
||||
public class SuppressionAnnotationInspection extends SuppressionAnnotationInspectionBase {
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
final ListEditForm form = new ListEditForm("Ignore suppressions", myAllowedSuppressions);
|
||||
return form.getContentPanel();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
if (infos.length == 1 && infos[0] instanceof PsiAnnotation) {
|
||||
final PsiAnnotation annotation = (PsiAnnotation)infos[0];
|
||||
final Collection<String> ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation((PsiModifierList)annotation.getParent());
|
||||
if (!ids.isEmpty()) {
|
||||
return new InspectionGadgetsFix() {
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement psiElement = descriptor.getPsiElement();
|
||||
if (psiElement instanceof PsiAnnotation) {
|
||||
final Collection<String> ids = JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation((PsiModifierList)psiElement.getParent());
|
||||
for (String id : ids) {
|
||||
if (!myAllowedSuppressions.contains(id)) {
|
||||
myAllowedSuppressions.add(id);
|
||||
}
|
||||
}
|
||||
saveProfile(project);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveProfile(Project project) {
|
||||
final InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
|
||||
InspectionProfileManager.getInstance().fireProfileChanged(inspectionProfile);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return "Allow suppressions";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user