From 63cbf2cb9b7251f86ce90ac5f398fd4fbb19a38e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 7 Jul 2017 15:48:01 +0200 Subject: [PATCH] IDEA-96785 ("Result of object allocation ignored": Allow suppression for certain classes) --- ...ObjectAllocationIgnoredInspectionBase.java | 24 +++++++++++++------ ...ltOfObjectAllocationIgnoredInspection.java | 11 ++++++++- .../ResultOfObjectAllocationIgnored.java | 1 + ...ObjectAllocationIgnoredInspectionTest.java | 4 +++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java index 6471277153c9..0ee937b2e43d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2017 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,9 +15,8 @@ */ package com.siyeh.ig.bugs; -import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiExpressionStatement; -import com.intellij.psi.PsiNewExpression; +import com.intellij.psi.*; +import com.intellij.util.containers.OrderedSet; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -25,6 +24,8 @@ import org.jetbrains.annotations.NotNull; public class ResultOfObjectAllocationIgnoredInspectionBase extends BaseInspection { + @SuppressWarnings("PublicField") public OrderedSet ignoredClasses = new OrderedSet<>(); + @Override @NotNull public String getDisplayName() { @@ -42,7 +43,7 @@ public class ResultOfObjectAllocationIgnoredInspectionBase extends BaseInspectio return new ResultOfObjectAllocationIgnoredVisitor(); } - private static class ResultOfObjectAllocationIgnoredVisitor extends BaseInspectionVisitor { + private class ResultOfObjectAllocationIgnoredVisitor extends BaseInspectionVisitor { @Override public void visitExpressionStatement(@NotNull PsiExpressionStatement statement) { @@ -53,10 +54,19 @@ public class ResultOfObjectAllocationIgnoredInspectionBase extends BaseInspectio } final PsiNewExpression newExpression = (PsiNewExpression)expression; final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions(); - if (arrayDimensions.length != 0) { + if (arrayDimensions.length != 0 || newExpression.getArrayInitializer() != null) { return; } - if (newExpression.getArrayInitializer() != null) { + final PsiJavaCodeReferenceElement reference = newExpression.getClassOrAnonymousClassReference(); + if (reference == null) { + return; + } + final PsiElement target = reference.resolve(); + if (!(target instanceof PsiClass)) { + return; + } + final PsiClass aClass = (PsiClass)target; + if (!(expression instanceof PsiAnonymousClass) && ignoredClasses.contains(aClass.getQualifiedName())) { return; } registerNewExpressionError(newExpression, newExpression); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java index d31cd55a8e34..a3730f539319 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -18,13 +18,22 @@ package com.siyeh.ig.bugs; import com.intellij.psi.PsiElement; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.fixes.SuppressForTestsScopeFix; +import com.siyeh.ig.ui.UiUtils; import org.jetbrains.annotations.Nullable; +import javax.swing.*; + /** * @author Bas Leijdekkers */ public class ResultOfObjectAllocationIgnoredInspection extends ResultOfObjectAllocationIgnoredInspectionBase { + @Nullable + @Override + public JComponent createOptionsPanel() { + return UiUtils.createTreeClassChooserList(ignoredClasses, "Ignored classes", "Choose class for which object allocation can be ignored"); + } + @Nullable @Override protected InspectionGadgetsFix buildFix(Object... infos) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java index c5c38cbdb5ed..41132335a44d 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/result_of_object_allocation_ignored/ResultOfObjectAllocationIgnored.java @@ -8,6 +8,7 @@ public class ResultOfObjectAllocationIgnored { public static void foo() { new Integer(3); + new javax.swing.JFrame(); } void boom() { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java index 5f18f1e7a86a..7c21cddec555 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/ResultOfObjectAllocationIgnoredInspectionTest.java @@ -31,6 +31,8 @@ public class ResultOfObjectAllocationIgnoredInspectionTest extends LightInspecti @Nullable @Override protected InspectionProfileEntry getInspection() { - return new ResultOfObjectAllocationIgnoredInspection(); + final ResultOfObjectAllocationIgnoredInspection inspection = new ResultOfObjectAllocationIgnoredInspection(); + inspection.ignoredClasses.add("javax.swing.JFrame"); + return inspection; } } \ No newline at end of file