IG: add option to only report @Deprecated constructors (IDEA-172378)

This commit is contained in:
Bas Leijdekkers
2017-07-17 11:04:30 +02:00
parent dfdcca58d6
commit aeb97f359e
6 changed files with 32 additions and 19 deletions
@@ -1817,7 +1817,7 @@
implementationClass="com.siyeh.ig.numeric.BigDecimalMethodWithoutRoundingCalledInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="CachedNumberConstructorCall" bundle="com.siyeh.InspectionGadgetsBundle"
key="cached.number.constructor.call.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.numeric.issues" enabledByDefault="false" level="WARNING"
groupKey="group.names.numeric.issues" enabledByDefault="true" level="WARNING"
implementationClass="com.siyeh.ig.numeric.CachedNumberConstructorCallInspection"/>
<localInspection groupPath="Java" language="JAVA" suppressId="NumericCastThatLosesPrecision" shortName="CastThatLosesPrecision"
bundle="com.siyeh.InspectionGadgetsBundle" key="cast.that.loses.precision.display.name"
@@ -1383,6 +1383,7 @@ cached.number.constructor.call.display.name=Number constructor call with primiti
cached.number.constructor.call.problem.descriptor=Number constructor call with primitive argument #loc
cached.number.constructor.call.quickfix=Replace with ''{0}.valueOf()'' call
cached.number.constructor.call.ignore.string.arguments.option=Ignore new number expressions with a String argument
cached.number.constructor.call.report.only.deprecated=Report only when constructor is @Deprecated
chained.equality.comparisons.problem.descriptor=Chained equality comparison <code>#ref</code> #loc
confusing.octal.escape.sequence.problem.descriptor=Octal escape sequence <code>#ref</code> immediately followed by digit #loc
field.accessed.synchronized.and.unsynchronized.option=Simple getters and setters are considered field accesses too
@@ -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<String> cachedNumberTypes = new HashSet<>();
static final Set<String> 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);
}
}
@@ -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.
<!-- tooltip end -->
<p>
Use the checkbox below to ignore calls to number constructors with a <b>String</b> argument.
Use the first checkbox below to ignore calls to number constructors with a <b>String</b> argument.
<p>
Use the second checkbox to only report calls to deprecated constructors.
<b>Long</b>, <b>Integer</b>, <b>Short</b> and<b>Byte</b> constructors are deprecated since JDK 9.
</body>
</html>
@@ -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
@@ -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);"); }