From 75a6f22f936d80aaee28d2507fc8fa5cd24b1088 Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Mon, 26 Mar 2012 17:30:28 +0400 Subject: [PATCH] IDEA-83355 Deprecated objects do not show as strikethrough in groovy code. --- .../GrDeprecatedAPIUsage.html | 5 + plugins/groovy/src/META-INF/plugin.xml | 3 + .../plugins/groovy/GroovyBundle.properties | 1 + .../GroovyInspectionBundle.properties | 1 + .../GrDeprecatedAPIUsageInspection.java | 104 ++++++++++++++++++ .../groovy/lang/GroovyHighlightingTest.groovy | 23 +++- 6 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 plugins/groovy/resources/inspectionDescriptions/GrDeprecatedAPIUsage.html create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/GrDeprecatedAPIUsageInspection.java diff --git a/plugins/groovy/resources/inspectionDescriptions/GrDeprecatedAPIUsage.html b/plugins/groovy/resources/inspectionDescriptions/GrDeprecatedAPIUsage.html new file mode 100644 index 000000000000..f946eea6ddee --- /dev/null +++ b/plugins/groovy/resources/inspectionDescriptions/GrDeprecatedAPIUsage.html @@ -0,0 +1,5 @@ + + +This inspection reports usages of deprecated code in Groovy + + \ No newline at end of file diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 956a30ddf366..6226695b0628 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -599,6 +599,9 @@ + diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties index 57c820df5558..af9e9170000a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/GroovyBundle.properties @@ -304,3 +304,4 @@ primitive.bound.types.are.not.allowed=Primitive bound types are not allowed ellipsis.type.is.not.allowed.here=Ellipsis type is not allowed here method.0.is.too.complex.too.analyze=Method ''{0}'' is too complex to analyze.\nTypes of local variables are not inferred. closure.is.too.complex.to.analyze=Closure is complex to analyze.\nTypes of local variables are not inferred. +0.is.deprecated=''{0}'' is deprecated diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties index 28f439a39a95..a2b7cb92f819 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties @@ -83,3 +83,4 @@ unused.0=Unused {0} remove.0=Remove {0} replace.postfix.0.with.prefix.0=Replace postfix {0} with prefix {0} replace.0.with.1=Replace {0} with binary {1} +gr.deprecated.api.usage=Deprecated API inspection diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/GrDeprecatedAPIUsageInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/GrDeprecatedAPIUsageInspection.java new file mode 100644 index 000000000000..a70da482cccb --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/confusing/GrDeprecatedAPIUsageInspection.java @@ -0,0 +1,104 @@ +/* + * Copyright 2000-2012 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 org.jetbrains.plugins.groovy.codeInspection.confusing; + +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.psi.PsiDocCommentOwner; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiModifierListOwner; +import com.intellij.psi.impl.PsiImplUtil; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyBundle; +import org.jetbrains.plugins.groovy.codeInspection.BaseInspection; +import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor; +import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle; +import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement; + +/** + * @author Max Medvedev + */ +public class GrDeprecatedAPIUsageInspection extends BaseInspection { + @Override + public boolean isEnabledByDefault() { + return true; + } + + @Nls + @NotNull + public String getGroupDisplayName() { + return CONFUSING_CODE_CONSTRUCTS; + } + + @Nls + @NotNull + public String getDisplayName() { + return GroovyInspectionBundle.message("gr.deprecated.api.usage"); + } + + @NonNls + @NotNull + public String getShortName() { + return "GrDeprecatedAPIUsage"; + } + + @Override + protected BaseInspectionVisitor buildVisitor() { + return new BaseInspectionVisitor() { + @Override + public void visitReferenceExpression(GrReferenceExpression ref) { + super.visitReferenceExpression(ref); + checkRef(ref); + } + + @Override + public void visitCodeReferenceElement(GrCodeReferenceElement ref) { + super.visitCodeReferenceElement(ref); + checkRef(ref); + } + + private void checkRef(GrReferenceElement ref) { + PsiElement resolved = ref.resolve(); + if (isDeprecated(resolved)) { + PsiElement toHighlight = getElementToHighlight(ref); + registerError(toHighlight, GroovyBundle.message("0.is.deprecated", ref.getReferenceName()), LocalQuickFix.EMPTY_ARRAY, + ProblemHighlightType.LIKE_DEPRECATED); + } + } + + @NotNull + public PsiElement getElementToHighlight(@NotNull GrReferenceElement refElement) { + final PsiElement refNameElement = refElement.getReferenceNameElement(); + return refNameElement != null ? refNameElement : refElement; + } + + + private boolean isDeprecated(PsiElement resolved) { + if (resolved instanceof PsiDocCommentOwner && PsiImplUtil.isDeprecatedByDocTag((PsiDocCommentOwner)resolved)) { + return true; + } + if (resolved instanceof PsiModifierListOwner && PsiImplUtil.isDeprecatedByAnnotation((PsiModifierListOwner)resolved)) { + return true; + } + return false; + } + }; + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy index e0ba3cd80dad..575ad2e4d3c1 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy @@ -36,10 +36,6 @@ import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspec import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyResultOfAssignmentUsedInspection import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection -import org.jetbrains.plugins.groovy.codeInspection.confusing.ClashingGettersInspection -import org.jetbrains.plugins.groovy.codeInspection.confusing.GrUnusedIncDecInspection -import org.jetbrains.plugins.groovy.codeInspection.confusing.GroovyOctalIntegerInspection -import org.jetbrains.plugins.groovy.codeInspection.confusing.GroovyResultOfIncrementOrDecrementUsedInspection import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialConditionalInspection import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspection import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryReturnInspection @@ -50,6 +46,7 @@ import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.Groov import org.jetbrains.plugins.groovy.codeInspection.unusedDef.UnusedDefInspection import org.jetbrains.plugins.groovy.util.TestUtils import org.jetbrains.plugins.groovy.codeInspection.bugs.* +import org.jetbrains.plugins.groovy.codeInspection.confusing.* /** * @author peter @@ -710,4 +707,22 @@ public class CorrectImplementor implements ActionListener { public void testReassignedHighlighting() { myFixture.testHighlighting(true, true, true, getTestName(false) + ".groovy"); } + + public void testDeprecated() { + myFixture.configureByText('_a.groovy', '''\ +/** + @deprecated +*/ +class X { + @Deprecated + def foo(){} + + public static void main() { + new X().foo() + } +}''') + + myFixture.enableInspections(GrDeprecatedAPIUsageInspection) + myFixture.testHighlighting(true, false, false) + } } \ No newline at end of file