diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java index a490f81b1d65..9f238dcd8dfa 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java @@ -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("Ignore overrides of deprecated abstract methods from non-deprecated supers", "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; diff --git a/java/java-tests/testData/inspection/deprecation/methodsOfDeprecatedClass/expected.xml b/java/java-tests/testData/inspection/deprecation/methodsOfDeprecatedClass/expected.xml new file mode 100644 index 000000000000..e621cdc7f5ad --- /dev/null +++ b/java/java-tests/testData/inspection/deprecation/methodsOfDeprecatedClass/expected.xml @@ -0,0 +1,16 @@ + + + + Test.java + 11 + Deprecated API usage + 'Test' is deprecated + + + + Test.java + 12 + Deprecated API usage + 'Test' is deprecated + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/deprecation/methodsOfDeprecatedClass/src/Test.java b/java/java-tests/testData/inspection/deprecation/methodsOfDeprecatedClass/src/Test.java new file mode 100644 index 000000000000..c9a13d8cd20c --- /dev/null +++ b/java/java-tests/testData/inspection/deprecation/methodsOfDeprecatedClass/src/Test.java @@ -0,0 +1,14 @@ +@Deprecated +class Test { + protected Test() { + + } + + public void foo(){} +} + +class D { + static void foo(Test t) { + t.foo(); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java index 15b82e9382b0..a259f062983e 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java @@ -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); + } + }