From 9c8003c077bde2c78709631fd439012614d89ab4 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 7 Sep 2016 16:13:52 +0300 Subject: [PATCH] unused declaration: treat @deprecated members as entry points as they can't be deleted --- .../deadCode/DeprecatedEntryPoint.java | 67 +++++++++++++++++++ .../deprecatedAsEntryPoint/expected.xml | 4 ++ .../deprecatedAsEntryPoint/src/Test.java | 5 ++ .../codeInspection/UnusedDeclarationTest.java | 10 +-- resources/src/META-INF/IdeaPlugin.xml | 1 + 5 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInspection/deadCode/DeprecatedEntryPoint.java create mode 100644 java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/expected.xml create mode 100644 java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/src/Test.java diff --git a/java/java-impl/src/com/intellij/codeInspection/deadCode/DeprecatedEntryPoint.java b/java/java-impl/src/com/intellij/codeInspection/deadCode/DeprecatedEntryPoint.java new file mode 100644 index 000000000000..dae07f0d7934 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInspection/deadCode/DeprecatedEntryPoint.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInspection.deadCode; + +import com.intellij.codeInspection.reference.EntryPoint; +import com.intellij.codeInspection.reference.RefElement; +import com.intellij.openapi.util.InvalidDataException; +import com.intellij.openapi.util.WriteExternalException; +import com.intellij.psi.PsiDocCommentOwner; +import com.intellij.psi.PsiElement; +import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters; +import com.intellij.util.xmlb.XmlSerializer; +import org.jdom.Element; +import org.jetbrains.annotations.NotNull; + +public class DeprecatedEntryPoint extends EntryPoint { + public boolean DEPRECATED_ENTRY_POINT = true; + + @Override + public void readExternal(Element element) throws InvalidDataException { + XmlSerializer.deserializeInto(this, element); + } + + @Override + public void writeExternal(Element element) throws WriteExternalException { + XmlSerializer.serializeInto(this, element, new SkipDefaultValuesSerializationFilters()); + } + + @NotNull + @Override + public String getDisplayName() { + return "Deprecated members"; + } + + @Override + public boolean isEntryPoint(@NotNull RefElement refElement, @NotNull PsiElement psiElement) { + return isEntryPoint(psiElement); + } + + @Override + public boolean isEntryPoint(@NotNull PsiElement psiElement) { + return psiElement instanceof PsiDocCommentOwner && ((PsiDocCommentOwner)psiElement).isDeprecated(); + } + + @Override + public boolean isSelected() { + return DEPRECATED_ENTRY_POINT; + } + + @Override + public void setSelected(boolean selected) { + DEPRECATED_ENTRY_POINT = selected; + } +} diff --git a/java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/expected.xml b/java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/expected.xml new file mode 100644 index 000000000000..79e1a0fd0a57 --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/expected.xml @@ -0,0 +1,4 @@ + + + + diff --git a/java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/src/Test.java b/java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/src/Test.java new file mode 100644 index 000000000000..1f18826ace3f --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/deprecatedAsEntryPoint/src/Test.java @@ -0,0 +1,5 @@ +@Deprecated +public class Test {} + +/** @deprecated use {@link Test1} (to be removed in IDEA 17) */ +class Test1 {} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/UnusedDeclarationTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/UnusedDeclarationTest.java index bdf8ffdb95e4..e89ddce31c1c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/UnusedDeclarationTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/UnusedDeclarationTest.java @@ -15,13 +15,9 @@ */ package com.intellij.codeInspection; -import com.intellij.JavaTestUtil; -import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.codeInspection.ex.EntryPointsManagerBase; -import com.intellij.codeInspection.ex.GlobalInspectionToolWrapper; import com.intellij.openapi.roots.LanguageLevelProjectExtension; import com.intellij.pom.java.LanguageLevel; -import com.intellij.testFramework.InspectionTestCase; /** * @author max @@ -191,7 +187,11 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { doTest(); } - public void testClassUsedInMethodParameter() throws Exception { + public void testClassUsedInMethodParameter() { + doTest(); + } + + public void testDeprecatedAsEntryPoint() { doTest(); } } diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index a6c5732c032e..818e65e66702 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -590,6 +590,7 @@ groupKey="group.names.declaration.redundancy" enabledByDefault="true" level="WARNING" implementationClass="com.intellij.codeInspection.deadCode.UnusedDeclarationInspection" presentation="com.intellij.codeInspection.deadCode.UnusedDeclarationPresentation"/> +