From 3335fc4880207e801c6fdeb3ad4be7ea548a304a Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 16 Feb 2011 22:16:30 +0100 Subject: [PATCH] IDEA-64082 (Add option "skip private methods" to Return of Null inspections) --- .../siyeh/InspectionGadgetsBundle.properties | 7 ++- .../siyeh/ig/bugs/ReturnNullInspection.java | 58 ++++++++++++++----- .../inspectionDescriptions/ReturnNull.html | 8 ++- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index 0a9522cf8c9c..fe1727b77099 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -155,10 +155,11 @@ use.0index.in.jdbc.resultset.display.name=Use of index 0 in JDBC ResultSet use.0index.in.jdbc.resultset.problem.descriptor=Use of index '0' in JDBC ResultSet #loc return.of.null.display.name=Return of 'null' return.of.null.problem.descriptor=Return of #ref #loc -return.of.null.arrays.option=Methods that return arrays +return.of.null.arrays.option=Report methods that return arrays return.of.null.quickfix=Annotate method as @Nullable -return.of.null.objects.option=Methods that return objects -return.of.null.collections.option=Methods that return collection objects +return.of.null.objects.option=Report methods that return objects +return.of.null.collections.option=Report methods that return collection objects +return.of.null.ignore.private.option=Ignore private methods static.method.via.subclass.display.name=Static method referenced via subclass static.method.via.subclass.problem.descriptor=Static method #ref() declared on class ''{0}'' but referenced via class ''{1}'' #loc static.method.via.subclass.rationalize.quickfix=Rationalize static method call diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReturnNullInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReturnNullInspection.java index e54ac775a86a..0417043d9ddf 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReturnNullInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReturnNullInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2011 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. @@ -28,6 +28,7 @@ import com.siyeh.ig.DelegatingFix; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.CollectionUtils; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; +import org.intellij.lang.annotations.Pattern; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -35,43 +36,56 @@ import javax.swing.*; public class ReturnNullInspection extends BaseInspection { - /** @noinspection PublicField*/ + @SuppressWarnings({"PublicField"}) public boolean m_reportObjectMethods = true; - /** @noinspection PublicField*/ + @SuppressWarnings({"PublicField"}) public boolean m_reportArrayMethods = true; - /** @noinspection PublicField*/ + @SuppressWarnings({"PublicField"}) public boolean m_reportCollectionMethods = true; + @SuppressWarnings({"PublicField"}) + public boolean m_ignorePrivateMethods = false; + @Override + @Pattern("[a-zA-Z_0-9.-]+") @NotNull public String getID() { return "ReturnOfNull"; } + @Override @NotNull public String getDisplayName() { return InspectionGadgetsBundle.message("return.of.null.display.name"); } + @Override @NotNull public String buildErrorString(Object... infos) { return InspectionGadgetsBundle.message( "return.of.null.problem.descriptor"); } + @Override @Nullable protected InspectionGadgetsFix buildFix(Object... infos) { final PsiElement elt = (PsiElement)infos[0]; if (!AnnotationUtil.isAnnotatingApplicable(elt)) { return null; } - final NullableNotNullManager manager = NullableNotNullManager.getInstance(elt.getProject()); - return new DelegatingFix(new AnnotateMethodFix( - manager.getDefaultNullable(), ArrayUtil.toStringArray(manager.getNotNulls()))); + final NullableNotNullManager manager = + NullableNotNullManager.getInstance(elt.getProject()); + return new DelegatingFix(new AnnotateMethodFix( + manager.getDefaultNullable(), + ArrayUtil.toStringArray(manager.getNotNulls()))); } - public JComponent createOptionsPanel() { + @Override + public JComponent createOptionsPanel() { final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message( + "return.of.null.ignore.private.option"), + "m_ignorePrivateMethods"); optionsPanel.addCheckbox(InspectionGadgetsBundle.message( "return.of.null.arrays.option"), "m_reportArrayMethods"); optionsPanel.addCheckbox(InspectionGadgetsBundle.message( @@ -82,6 +96,7 @@ public class ReturnNullInspection extends BaseInspection { return optionsPanel; } + @Override public BaseInspectionVisitor buildVisitor() { return new ReturnNullVisitor(); } @@ -109,21 +124,32 @@ public class ReturnNullInspection extends BaseInspection { if (method == null) { return; } + if (m_ignorePrivateMethods && + method.hasModifierProperty(PsiModifier.PRIVATE)) { + return; + } final PsiType returnType = method.getReturnType(); if (returnType == null) { return; } final boolean isArray = returnType.getArrayDimensions() > 0; - if (NullableNotNullManager.getInstance(method.getProject()).isNullable(method, false)) { + final NullableNotNullManager nullableNotNullManager = + NullableNotNullManager.getInstance(method.getProject()); + if (nullableNotNullManager.isNullable(method, false)) { return; } - if (m_reportCollectionMethods && - CollectionUtils.isCollectionClassOrInterface(returnType)) { - registerError(value, value); - } else if (m_reportArrayMethods && isArray) { - registerError(value, value); - } else if (m_reportObjectMethods && !isArray) { - registerError(value, value); + if (CollectionUtils.isCollectionClassOrInterface(returnType)) { + if (m_reportCollectionMethods) { + registerError(value, value); + } + } else if (isArray) { + if (m_reportArrayMethods) { + registerError(value, value); + } + } else { + if (m_reportObjectMethods) { + registerError(value, value); + } } } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ReturnNull.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ReturnNull.html index 18cfb942ff84..9d543b953b86 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ReturnNull.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ReturnNull.html @@ -2,10 +2,12 @@ This inspection reports return statements with null values. While occasionally useful, this construct may make the code more prone -to failing with a NullPointerException, and often indicates that the developer doesn't really understand the classes -intended semantics. +to failing with a NullPointerException, and often indicates that the +developer doesn't really understand the classes intended semantics.

-Use the controls below to specify whether this inspection should report +Use the first control below to let this inspection ignore private methods. +

+Use bottom three controls to specify whether this inspection should report null values on array returns, collection object returns, plain object returns, or a combination of the three.