diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 40ef9575343c..842994427404 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -1817,7 +1817,7 @@ implementationClass="com.siyeh.ig.numeric.BigDecimalMethodWithoutRoundingCalledInspection"/> #ref #loc confusing.octal.escape.sequence.problem.descriptor=Octal escape sequence #ref immediately followed by digit #loc field.accessed.synchronized.and.unsynchronized.option=Simple getters and setters are considered field accesses too diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java index b0af589efef4..8ca39a4dce58 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2014 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. @@ -16,7 +16,7 @@ package com.siyeh.ig.numeric; import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; @@ -37,7 +37,7 @@ import java.util.Set; public class CachedNumberConstructorCallInspection extends BaseInspection { - private static final Set cachedNumberTypes = new HashSet<>(); + static final Set cachedNumberTypes = new HashSet<>(); static { cachedNumberTypes.add(CommonClassNames.JAVA_LANG_LONG); @@ -47,27 +47,30 @@ public class CachedNumberConstructorCallInspection extends BaseInspection { } @SuppressWarnings("PublicField") - public static boolean ignoreStringArguments = false; + public boolean ignoreStringArguments = false; + + @SuppressWarnings("PublicField") + public boolean reportOnlyWhenDeprecated = true; @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "cached.number.constructor.call.display.name"); + return InspectionGadgetsBundle.message("cached.number.constructor.call.display.name"); } @Override @NotNull - public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( + public String buildErrorString(Object... infos) { return InspectionGadgetsBundle.message( "cached.number.constructor.call.problem.descriptor"); } @Nullable @Override public JComponent createOptionsPanel() { - return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("cached.number.constructor.call.ignore.string.arguments.option"), - this, "ignoreStringArguments"); + final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + panel.addCheckbox(InspectionGadgetsBundle.message("cached.number.constructor.call.ignore.string.arguments.option"), "ignoreStringArguments"); + panel.addCheckbox(InspectionGadgetsBundle.message("cached.number.constructor.call.report.only.deprecated"), "reportOnlyWhenDeprecated"); + return panel; } @Override @@ -125,11 +128,10 @@ public class CachedNumberConstructorCallInspection extends BaseInspection { } } - private static class LongConstructorVisitor extends BaseInspectionVisitor { + private class LongConstructorVisitor extends BaseInspectionVisitor { @Override - public void visitNewExpression( - @NotNull PsiNewExpression expression) { + public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); final PsiType type = expression.getType(); if (type == null) { @@ -156,6 +158,10 @@ public class CachedNumberConstructorCallInspection extends BaseInspection { if (argumentType == null || (ignoreStringArguments && argumentType.equalsToText(CommonClassNames.JAVA_LANG_STRING))) { return; } + final PsiMethod method = expression.resolveMethod(); + if (method == null || (reportOnlyWhenDeprecated && !method.isDeprecated())) { + return; + } registerNewExpressionError(expression, expression); } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/CachedNumberConstructorCall.html b/plugins/InspectionGadgets/src/inspectionDescriptions/CachedNumberConstructorCall.html index d9174581c2d2..fcc31c8c77f8 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/CachedNumberConstructorCall.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/CachedNumberConstructorCall.html @@ -13,7 +13,9 @@ This inspection only reports if the project or module is configured to use a language level of 5.0 or higher.

-Use the checkbox below to ignore calls to number constructors with a String argument. +Use the first checkbox below to ignore calls to number constructors with a String argument.

+Use the second checkbox to only report calls to deprecated constructors. +Long, Integer, Short andByte constructors are deprecated since JDK 9. \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java index 1c17d16a04df..29714f6a1b10 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 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. @@ -23,7 +23,9 @@ public class CachedNumberConstructorCallTest extends IGQuickFixesTestCase { @Override protected void setUp() throws Exception { super.setUp(); - myFixture.enableInspections(new CachedNumberConstructorCallInspection()); + final CachedNumberConstructorCallInspection inspection = new CachedNumberConstructorCallInspection(); + inspection.reportOnlyWhenDeprecated = false; + myFixture.enableInspections(inspection); } @Override diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/CachedNumberConstructorCallInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/CachedNumberConstructorCallInspectionTest.java index 3eff4e682919..e657601702f9 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/CachedNumberConstructorCallInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/CachedNumberConstructorCallInspectionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 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. @@ -24,7 +24,9 @@ import com.siyeh.ig.LightInspectionTestCase; public class CachedNumberConstructorCallInspectionTest extends LightInspectionTestCase { @Override protected InspectionProfileEntry getInspection() { - return new CachedNumberConstructorCallInspection(); + final CachedNumberConstructorCallInspection inspection = new CachedNumberConstructorCallInspection(); + inspection.reportOnlyWhenDeprecated = false; + return inspection; } public void testSimple() { doStatementTest("new /*Number constructor call with primitive argument*/Integer/**/(1);"); }