option to warn about members in deprecated classes (IDEA-112084)

This commit is contained in:
Anna Kozlova
2014-06-05 20:03:26 +04:00
parent 27ec65a6f8
commit 8f10d715d3
4 changed files with 75 additions and 8 deletions
@@ -18,13 +18,18 @@ package com.intellij.codeInspection.deprecation;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightMessageUtil;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool;
import com.intellij.codeInspection.DeprecationUtil;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod;
import com.intellij.psi.util.PsiTreeUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -39,15 +44,18 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
@NonNls public static final String SHORT_NAME = DeprecationUtil.DEPRECATION_SHORT_NAME;
@NonNls public static final String ID = DeprecationUtil.DEPRECATION_ID;
public static final String DISPLAY_NAME = DeprecationUtil.DEPRECATION_DISPLAY_NAME;
public static final String IGNORE_METHODS_OF_DEPRECATED_NAME = "IGNORE_METHODS_OF_DEPRECATED";
public boolean IGNORE_INSIDE_DEPRECATED = false;
public boolean IGNORE_ABSTRACT_DEPRECATED_OVERRIDES = true;
public boolean IGNORE_IMPORT_STATEMENTS = true;
public boolean IGNORE_METHODS_OF_DEPRECATED = true;
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new DeprecationElementVisitor(holder, IGNORE_INSIDE_DEPRECATED, IGNORE_ABSTRACT_DEPRECATED_OVERRIDES, IGNORE_IMPORT_STATEMENTS);
return new DeprecationElementVisitor(holder, IGNORE_INSIDE_DEPRECATED, IGNORE_ABSTRACT_DEPRECATED_OVERRIDES,
IGNORE_IMPORT_STATEMENTS, IGNORE_METHODS_OF_DEPRECATED);
}
@Override
@@ -56,6 +64,18 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
return DISPLAY_NAME;
}
@Override
public void writeSettings(@NotNull Element node) throws WriteExternalException {
node.addContent(new Element("option").setAttribute("name", "IGNORE_INSIDE_DEPRECATED").setAttribute("value", String.valueOf(IGNORE_INSIDE_DEPRECATED)));
node.addContent(new Element("option").setAttribute("name", "IGNORE_ABSTRACT_DEPRECATED_OVERRIDES").setAttribute("value", String.valueOf(IGNORE_ABSTRACT_DEPRECATED_OVERRIDES)));
node.addContent(new Element("option").setAttribute("name", "IGNORE_IMPORT_STATEMENTS").setAttribute("value", String.valueOf(IGNORE_IMPORT_STATEMENTS)));
if (!IGNORE_METHODS_OF_DEPRECATED) {
node.addContent(new Element("option").setAttribute("name", "IGNORE_METHODS_OF_DEPRECATED").setAttribute("value", String.valueOf(IGNORE_METHODS_OF_DEPRECATED)));
}
}
@Override
@NotNull
public String getGroupDisplayName() {
@@ -86,6 +106,7 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
panel.addCheckbox("Ignore inside deprecated members", "IGNORE_INSIDE_DEPRECATED");
panel.addCheckbox("Ignore inside non-static imports", "IGNORE_IMPORT_STATEMENTS");
panel.addCheckbox("<html>Ignore overrides of deprecated abstract methods from non-deprecated supers</html>", "IGNORE_ABSTRACT_DEPRECATED_OVERRIDES");
panel.addCheckbox("Ignore members of deprecated classes", IGNORE_METHODS_OF_DEPRECATED_NAME);
return panel;
}
@@ -95,29 +116,32 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
private final boolean myIgnoreInsideDeprecated;
private final boolean myIgnoreAbstractDeprecatedOverrides;
private final boolean myIgnoreImportStatements;
private final boolean myIgnoreMethodsOfDeprecated;
public DeprecationElementVisitor(final ProblemsHolder holder,
boolean ignoreInsideDeprecated,
boolean ignoreAbstractDeprecatedOverrides,
boolean ignoreImportStatements) {
boolean ignoreImportStatements,
boolean ignoreMethodsOfDeprecated) {
myHolder = holder;
myIgnoreInsideDeprecated = ignoreInsideDeprecated;
myIgnoreAbstractDeprecatedOverrides = ignoreAbstractDeprecatedOverrides;
myIgnoreImportStatements = ignoreImportStatements;
myIgnoreMethodsOfDeprecated = ignoreMethodsOfDeprecated;
}
@Override
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
JavaResolveResult result = reference.advancedResolve(true);
PsiElement resolved = result.getElement();
checkDeprecated(resolved, reference.getReferenceNameElement(), null, myIgnoreInsideDeprecated, myIgnoreImportStatements, myHolder);
checkDeprecated(resolved, reference.getReferenceNameElement(), null, myIgnoreInsideDeprecated, myIgnoreImportStatements, myIgnoreMethodsOfDeprecated, myHolder);
}
@Override
public void visitImportStaticStatement(PsiImportStaticStatement statement) {
final PsiJavaCodeReferenceElement importReference = statement.getImportReference();
if (importReference != null) {
checkDeprecated(importReference.resolve(), importReference.getReferenceNameElement(), null, myIgnoreInsideDeprecated, false, myHolder);
checkDeprecated(importReference.resolve(), importReference.getReferenceNameElement(), null, myIgnoreInsideDeprecated, false, true, myHolder);
}
}
@@ -148,7 +172,7 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
PsiMethod constructor = result == null ? null : result.getElement();
if (constructor != null && expression.getClassOrAnonymousClassReference() != null) {
if (expression.getClassReference() == null && constructor.getParameterList().getParametersCount() == 0) return;
checkDeprecated(constructor, expression.getClassOrAnonymousClassReference(), null, myIgnoreInsideDeprecated, myIgnoreImportStatements, myHolder);
checkDeprecated(constructor, expression.getClassOrAnonymousClassReference(), null, myIgnoreInsideDeprecated, myIgnoreImportStatements, true, myHolder);
}
}
}
@@ -241,7 +265,7 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
PsiElement elementToHighlight,
@Nullable TextRange rangeInElement,
ProblemsHolder holder) {
checkDeprecated(refElement, elementToHighlight, rangeInElement, false, false, holder);
checkDeprecated(refElement, elementToHighlight, rangeInElement, false, false, true, holder);
}
public static void checkDeprecated(PsiElement refElement,
@@ -249,9 +273,16 @@ public class DeprecationInspection extends BaseJavaBatchLocalInspectionTool {
@Nullable TextRange rangeInElement,
boolean ignoreInsideDeprecated,
boolean ignoreImportStatements,
boolean ignoreMethodsOfDeprecated,
ProblemsHolder holder) {
if (!(refElement instanceof PsiDocCommentOwner)) return;
if (!((PsiDocCommentOwner)refElement).isDeprecated()) return;
if (!((PsiDocCommentOwner)refElement).isDeprecated()) {
if (!ignoreMethodsOfDeprecated) {
checkDeprecated(((PsiDocCommentOwner)refElement).getContainingClass(), elementToHighlight, rangeInElement,
ignoreInsideDeprecated, ignoreImportStatements, false, holder);
}
return;
}
if (ignoreInsideDeprecated) {
PsiElement parent = elementToHighlight;
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>11</line>
<problem_class severity="WARNING" attribute_key="DEPRECATED_ATTRIBUTES">Deprecated API usage</problem_class>
<description>'Test' is deprecated</description>
</problem>
<problem>
<file>Test.java</file>
<line>12</line>
<problem_class severity="WARNING" attribute_key="DEPRECATED_ATTRIBUTES">Deprecated API usage</problem_class>
<description>'Test' is deprecated</description>
</problem>
</problems>
@@ -0,0 +1,14 @@
@Deprecated
class Test {
protected Test() {
}
public void foo(){}
}
class D {
static void foo(Test t) {
t.foo();
}
}
@@ -53,4 +53,10 @@ public class DeprecationInspectionTest extends InspectionTestCase {
doTest();
}
public void testMethodsOfDeprecatedClass() throws Exception {
final DeprecationInspection tool = new DeprecationInspection();
tool.IGNORE_METHODS_OF_DEPRECATED = false;
doTest("deprecation/" + getTestName(true), tool);
}
}