Highlight references to deprecated things accordingly

#KT-2925 Fixed
This commit is contained in:
Natalia.Ukhorskaya
2012-10-12 15:23:04 +04:00
parent 2f2941fe5b
commit 20352b3810
17 changed files with 625 additions and 0 deletions
@@ -0,0 +1,261 @@
/*
* Copyright 2010-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.jet.plugin.highlighter;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.openapi.editor.colors.CodeInsightColors;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.resolve.DescriptorRenderer;
import java.util.List;
public class DeprecatedAnnotationVisitor extends AfterAnalysisHighlightingVisitor {
private static final TokenSet PROPERTY_SET_OPERATIONS =
TokenSet.create(JetTokens.EQ, JetTokens.PLUSEQ, JetTokens.MINUSEQ, JetTokens.MULTEQ,
JetTokens.DIVEQ, JetTokens.PERCEQ, JetTokens.PLUSPLUS, JetTokens.MINUSMINUS);
protected DeprecatedAnnotationVisitor(AnnotationHolder holder, BindingContext bindingContext) {
super(holder, bindingContext);
}
@Override
public void visitSuperExpression(JetSuperExpression expression) {
// Deprecated for super expression. Unnecessary to mark it as Deprecated
}
@Override
public void visitReferenceExpression(JetReferenceExpression expression) {
super.visitReferenceExpression(expression);
ResolvedCall resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, expression);
if (resolvedCall != null && resolvedCall instanceof VariableAsFunctionResolvedCall) {
// Deprecated for invoke()
JetCallExpression parent = PsiTreeUtil.getParentOfType(expression, JetCallExpression.class);
if (parent != null && isDeprecated(resolvedCall.getResultingDescriptor().getAnnotations())) {
reportAnnotation(parent, resolvedCall.getResultingDescriptor(), true);
}
}
if (expression.getNode().getElementType() == JetNodeTypes.OPERATION_REFERENCE) {
// Deprecated for operations (mark as warning)
checkDeprecatedForOperations(expression);
}
else {
checkDeprecatedForReferenceExpression(expression);
}
}
private void checkDeprecatedForReferenceExpression(@NotNull JetReferenceExpression expression) {
DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
if (target != null) {
if (target instanceof ConstructorDescriptor) {
checkConstructorDescriptor(expression, target);
}
else if (target instanceof ClassDescriptor) {
checkClassDescriptor(expression, (ClassDescriptor) target);
}
else if (target instanceof PropertyDescriptor) {
checkPropertyDescriptor(expression, (PropertyDescriptor) target);
}
else if (target instanceof FunctionDescriptor) {
checkFunctionDescriptor(expression, target);
}
}
}
private void checkFunctionDescriptor(JetExpression expression, DeclarationDescriptor target) {
// Deprecated for Function
if (isDeprecated(target.getAnnotations())) {
reportAnnotation(expression, target, expression instanceof JetArrayAccessExpression);
}
}
private void checkConstructorDescriptor(@NotNull JetExpression expression, @NotNull DeclarationDescriptor target) {
// Deprecated for Class and for Constructor
DeclarationDescriptor containingDeclaration = target.getContainingDeclaration();
if (containingDeclaration != null) {
if (isDeprecated(containingDeclaration.getAnnotations()) || isDeprecated(target.getAnnotations())) {
reportAnnotation(expression, containingDeclaration);
}
}
}
private void checkClassDescriptor(@NotNull JetExpression expression, @NotNull ClassDescriptor target) {
// Deprecated for Class, for ClassObject (if reference isn't in UserType or in ModifierList (trait))
if (isDeprecated(target.getAnnotations())) {
reportAnnotation(expression, target);
}
else if (PsiTreeUtil.getParentOfType(expression, JetUserType.class) == null &&
PsiTreeUtil.getParentOfType(expression, JetModifierList.class) == null) {
ClassDescriptor classObjectDescriptor = target.getClassObjectDescriptor();
if (classObjectDescriptor != null && isDeprecated(classObjectDescriptor.getAnnotations())) {
reportAnnotation(expression, classObjectDescriptor);
}
}
}
private void checkPropertyDescriptor(
@NotNull JetExpression expression,
@NotNull PropertyDescriptor propertyDescriptor
) {
// Deprecated for Property
if (isDeprecated(propertyDescriptor.getAnnotations())) {
reportAnnotation(expression, propertyDescriptor, propertyDescriptor.isVar());
return;
}
// Deprecated for Getter (val, var), Setter (var)
if (!propertyDescriptor.isVar()) {
checkPropertyGetter(propertyDescriptor, expression);
}
else {
IElementType operation = null;
JetBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(expression, JetBinaryExpression.class);
if (binaryExpression != null) {
JetExpression left = binaryExpression.getLeft();
if (left == expression) {
operation = binaryExpression.getOperationToken();
}
else {
JetReferenceExpression[] jetReferenceExpressions = PsiTreeUtil.getChildrenOfType(left, JetReferenceExpression.class);
if (jetReferenceExpressions != null) {
for (JetReferenceExpression expr : jetReferenceExpressions) {
if (expr == expression) {
operation = binaryExpression.getOperationToken();
break;
}
}
}
}
}
else {
JetUnaryExpression unaryExpression = PsiTreeUtil.getParentOfType(expression, JetUnaryExpression.class);
if (unaryExpression != null) {
operation = unaryExpression.getOperationReference().getReferencedNameElementType();
}
}
if (operation != null && PROPERTY_SET_OPERATIONS.contains(operation)) {
checkPropertySetter(propertyDescriptor, expression);
}
else {
checkPropertyGetter(propertyDescriptor, expression);
}
}
}
private void checkPropertySetter(@NotNull PropertyDescriptor descriptor, @NotNull JetExpression expression) {
PropertySetterDescriptor setter = descriptor.getSetter();
if (setter != null) {
checkPropertyAccessor(setter, expression, true);
}
}
private void checkPropertyGetter(@NotNull PropertyDescriptor descriptor, @NotNull JetExpression expression) {
PropertyGetterDescriptor getter = descriptor.getGetter();
if (getter != null) {
checkPropertyAccessor(getter, expression, descriptor.isVar());
}
}
private void checkPropertyAccessor(
@NotNull PropertyAccessorDescriptor accessor,
@NotNull JetExpression expression, boolean isVar
) {
if (isDeprecated(accessor.getAnnotations())) {
reportAnnotation(expression, accessor, isVar);
}
}
private void checkDeprecatedForOperations(@NotNull JetReferenceExpression expression) {
DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
if (target != null) {
if (isDeprecated(target.getAnnotations())) {
reportAnnotation(expression, target, true);
}
}
}
private void reportAnnotation(@NotNull PsiElement element, @NotNull DeclarationDescriptor descriptor) {
reportAnnotation(element, descriptor, false);
}
private void reportAnnotation(@NotNull PsiElement element, @NotNull DeclarationDescriptor descriptor, boolean isWarning) {
if (isWarning) {
holder.createInfoAnnotation(element, "'" + renderName(descriptor) + "' is deprecated")
.setTextAttributes(CodeInsightColors.WARNINGS_ATTRIBUTES);
}
else {
holder.createInfoAnnotation(element, "'" + renderName(descriptor) + "' is deprecated")
.setTextAttributes(CodeInsightColors.DEPRECATED_ATTRIBUTES);
}
}
private static boolean isDeprecated(List<AnnotationDescriptor> list) {
for (AnnotationDescriptor annotation : list) {
ClassDescriptor descriptor = TypeUtils.getClassDescriptor(annotation.getType());
if (descriptor != null) {
if (DescriptorUtils.getFQName(descriptor).getFqName().equals(CommonClassNames.JAVA_LANG_DEPRECATED)) {
return true;
}
}
}
return false;
}
private static String renderName(DeclarationDescriptor descriptor) {
if (descriptor instanceof ClassDescriptor) {
return DescriptorUtils.getFQName(descriptor).getFqName();
}
else if (descriptor instanceof ConstructorDescriptor) {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
assert containingDeclaration != null;
return "constructor for " + containingDeclaration.getName();
}
else if (descriptor instanceof PropertyGetterDescriptor) {
return "getter for " + ((PropertyGetterDescriptor) descriptor).getCorrespondingProperty().getName();
}
else if (descriptor instanceof PropertySetterDescriptor) {
return "setter for " + ((PropertySetterDescriptor) descriptor).getCorrespondingProperty().getName();
}
else if (descriptor instanceof PropertyDescriptor) {
if (((PropertyDescriptor) descriptor).isVar()) {
return "var " + descriptor.getName();
}
return "val " + descriptor.getName();
}
else if (descriptor instanceof FunctionDescriptor) {
return "fun " + descriptor.getName() + DescriptorRenderer.TEXT.renderFunctionParameters((FunctionDescriptor) descriptor);
}
return DescriptorRenderer.TEXT.render(descriptor);
}
}
@@ -96,6 +96,7 @@ public class JetPsiChecker implements Annotator {
new FunctionsHighlightingVisitor(holder, bindingContext),
new VariablesHighlightingVisitor(holder, bindingContext),
new TypeKindHighlightingVisitor(holder, bindingContext),
new DeprecatedAnnotationVisitor(holder, bindingContext),
};
}
@@ -0,0 +1,15 @@
package test
<info>import</info> java.util.ArrayList
Deprecated <info>open</info> class MyClass {}
fun test() {
val a : <info descr="'test.MyClass' is deprecated">MyClass</info>? = null
val b = <info descr="'test.MyClass' is deprecated">MyClass</info>()
val c = ArrayList<<info descr="'test.MyClass' is deprecated">MyClass</info>>()
}
class Test(): <info descr="'test.MyClass' is deprecated">MyClass</info>() {}
class Test2(param: <info descr="'test.MyClass' is deprecated">MyClass</info>) {}
@@ -0,0 +1,19 @@
fun test() {
<info descr="'MyClass.<class-object-for-MyClass>' is deprecated">MyClass</info>.test
MyClass()
val a: MyClass? = null
val b: MyTrait? = null
<info descr="'MyTrait.<class-object-for-MyTrait>' is deprecated">MyTrait</info>.test
}
class MyClass(): MyTrait {
Deprecated class object {
val <info>test</info>: String = ""
}
}
trait MyTrait {
Deprecated class object {
val <info>test</info>: String = ""
}
}
@@ -0,0 +1,18 @@
fun test() {
<info descr="'fun test1()' is deprecated">test1</info>()
MyClass().<info descr="'fun test2()' is deprecated">test2</info>()
MyClass.<info descr="'fun test3()' is deprecated">test3</info>()
<info descr="'fun test4(x : jet.Int, y : jet.Int)' is deprecated">test4</info>(1, 2)
}
Deprecated fun test1() { }
Deprecated fun test4(x: Int, y: Int) { }
class MyClass() {
Deprecated fun test2() {}
class object {
Deprecated fun test3() {}
}
}
@@ -0,0 +1,10 @@
class MyClass {}
Deprecated fun MyClass.get(i: MyClass): MyClass { return MyClass() }
fun test() {
val x1 = MyClass()
val x2 = MyClass()
<info descr="'fun get(i : MyClass)' is deprecated">x1[x2]</info>
}
@@ -0,0 +1,19 @@
fun test() {
val c = MyClass()
c.<info descr="'getter for test1' is deprecated">test1</info>
c.<info descr="'getter for test2' is deprecated">test2</info>
c.test2 = ""
c.<info descr="'val test3' is deprecated">test3</info>
}
class MyClass() {
<info>public</info> val <info>test1</info>: String = ""
[Deprecated] <info descr="null">get</info>
<info>public</info> var <info>test2</info>: String = ""
[Deprecated] <info descr="null">get</info>
Deprecated <info>public</info> val <info>test3</info>: String = ""
[Deprecated] <info>get</info>
}
@@ -0,0 +1,8 @@
class MyClass {}
Deprecated fun MyClass.inc(): MyClass { return MyClass() }
fun test() {
var x3 = MyClass()
x3<info descr="'fun inc()' is deprecated">++</info>
}
@@ -0,0 +1,9 @@
class MyRunnable() {}
Deprecated fun MyRunnable.invoke() {
}
fun test() {
val m = MyRunnable()
<info descr="'fun invoke()' is deprecated">m()</info>
}
@@ -0,0 +1,36 @@
class MyClass {}
Deprecated fun MyClass.minus(i: MyClass) { }
Deprecated fun MyClass.div(i: MyClass) { }
Deprecated fun MyClass.times(i: MyClass) { }
Deprecated fun MyClass.not() { }
Deprecated fun MyClass.plus() { }
Deprecated fun MyClass.contains(i: MyClass): Boolean { return false }
Deprecated fun MyClass.plusAssign(i: MyClass) { }
Deprecated fun MyClass.equals(i: Any?): Boolean { return false }
Deprecated fun MyClass.compareTo(i: MyClass): Int { return 0 }
fun test() {
val x1 = MyClass()
val x2 = MyClass()
x1 <info descr="'fun minus(i : MyClass)' is deprecated">-</info> x2
x1 <info descr="'fun div(i : MyClass)' is deprecated">/</info> x2
x1 <info descr="'fun times(i : MyClass)' is deprecated">*</info> x2
<info descr="'fun not()' is deprecated">!</info>x1
<info descr="'fun plus()' is deprecated">+</info>x1
x1 <info descr="'fun contains(i : MyClass)' is deprecated">in</info> x2
x1 <info descr="'fun contains(i : MyClass)' is deprecated">!in</info> x2
x1 <info descr="'fun plusAssign(i : MyClass)' is deprecated">+=</info> x2
x1 <info descr="'fun equals(i : jet.Any?)' is deprecated">==</info> x2
x1 <info descr="'fun equals(i : jet.Any?)' is deprecated">!=</info> x2
x1 <info descr="'fun compareTo(i : MyClass)' is deprecated">></info> x2
}
@@ -0,0 +1,22 @@
fun test() {
<info descr="'val test1' is deprecated">test1</info>
MyClass().<info descr="'val test2' is deprecated">test2</info>
MyClass.<info descr="'val test3' is deprecated">test3</info>
<info descr="'var test4' is deprecated">test4</info>
MyClass().<info descr="'var test5' is deprecated">test5</info>
MyClass.<info descr="'var test6' is deprecated">test6</info>
}
Deprecated val <info>test1</info>: String = ""
Deprecated var <info>test4</info>: String = ""
class MyClass() {
Deprecated val <info>test2</info>: String = ""
Deprecated var <info>test5</info>: String = ""
class object {
Deprecated val <info>test3</info>: String = ""
Deprecated var <info>test6</info>: String = ""
}
}
@@ -0,0 +1,15 @@
class MyClass { }
Deprecated fun MyClass.rangeTo(i: MyClass): IntIterator {
throw Exception()
}
fun test() {
val x1 = MyClass()
val x2 = MyClass()
for (i in x1<info descr="'fun rangeTo(i : MyClass)' is deprecated">..</info>x2) {
}
}
@@ -0,0 +1,26 @@
fun test() {
MyClass().test1
MyClass().<info descr="'setter for test1' is deprecated">test1</info> = 0
MyClass().<info descr="'setter for test1' is deprecated">test1</info>++
MyClass().<info descr="'setter for test1' is deprecated">test1</info>--
++MyClass().<info descr="'setter for test1' is deprecated">test1</info>
--MyClass().<info descr="'setter for test1' is deprecated">test1</info>
MyClass().<info descr="'setter for test1' is deprecated">test1</info> += 1
MyClass().<info descr="'setter for test1' is deprecated">test1</info> -= 1
MyClass().<info descr="'setter for test1' is deprecated">test1</info> /= 1
MyClass().<info descr="'setter for test1' is deprecated">test1</info> *= 1
test2
<info descr="'setter for test2' is deprecated">test2</info> = 10
}
class MyClass() {
<info>public</info> var <info>test1</info>: Int = 0
[Deprecated] <info>set</info>
}
<info>public</info> var <info>test2</info>: Int = 0
[Deprecated] <info>set</info>
@@ -0,0 +1,9 @@
class MyClass(): Base() {
fun test2() {
super.test1()
}
}
<info>open</info> class Base() {
fun test1() {}
}
@@ -0,0 +1,10 @@
Deprecated trait MyTrait { }
fun test() {
val a: <info descr="'MyTrait' is deprecated">MyTrait</info>? = null
val b: List<<info descr="'MyTrait' is deprecated">MyTrait</info>>? = null
}
class Test(): <info descr="'MyTrait' is deprecated">MyTrait</info> { }
class Test2(param: <info descr="'MyTrait' is deprecated">MyTrait</info>) {}
@@ -0,0 +1,46 @@
package org.jetbrains.jet.plugin.highlighter;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.openapi.projectRoots.Sdk;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.test.generator.SimpleTestClassModel;
import org.jetbrains.jet.test.generator.TestGenerator;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
public abstract class AbstractDeprecatedHighlightingTest extends LightDaemonAnalyzerTestCase {
@Override
protected Sdk getProjectJDK() {
return PluginTestCaseBase.jdkFromIdeaHome();
}
protected void doTest(String filePath) throws Exception {
doTest(getTestName(false) + ".kt", false, true);
}
@NotNull
@Override
protected String getTestDataPath() {
return PluginTestCaseBase.getTestDataPathBase() + "/highlighter/deprecated/";
}
public static void main(String[] args) throws IOException {
String aPackage = "org.jetbrains.jet.plugin.highlighter";
Class<AbstractDeprecatedHighlightingTest> thisClass = AbstractDeprecatedHighlightingTest.class;
new TestGenerator(
"idea/tests/",
aPackage,
"DeprecatedHighlightingTestGenerated",
thisClass,
Arrays.asList(
new SimpleTestClassModel(new File("idea/testData/highlighter/deprecated"), true, "kt", "doTest")
),
thisClass
).generateAndSave();
}
}
@@ -0,0 +1,101 @@
/*
* Copyright 2010-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.jet.plugin.highlighter;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.plugin.highlighter.AbstractDeprecatedHighlightingTest;
/** This class is generated by {@link org.jetbrains.jet.plugin.highlighter.AbstractDeprecatedHighlightingTest}. DO NOT MODIFY MANUALLY */
@TestMetadata("idea/testData/highlighter/deprecated")
public class DeprecatedHighlightingTestGenerated extends AbstractDeprecatedHighlightingTest {
public void testAllFilesPresentInDeprecated() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.plugin.highlighter.AbstractDeprecatedHighlightingTest", new File("idea/testData/highlighter/deprecated"), "kt", true);
}
@TestMetadata("Class.kt")
public void testClass() throws Exception {
doTest("idea/testData/highlighter/deprecated/Class.kt");
}
@TestMetadata("ClassObject.kt")
public void testClassObject() throws Exception {
doTest("idea/testData/highlighter/deprecated/ClassObject.kt");
}
@TestMetadata("Function.kt")
public void testFunction() throws Exception {
doTest("idea/testData/highlighter/deprecated/Function.kt");
}
@TestMetadata("Get.kt")
public void testGet() throws Exception {
doTest("idea/testData/highlighter/deprecated/Get.kt");
}
@TestMetadata("Getter.kt")
public void testGetter() throws Exception {
doTest("idea/testData/highlighter/deprecated/Getter.kt");
}
@TestMetadata("Inc.kt")
public void testInc() throws Exception {
doTest("idea/testData/highlighter/deprecated/Inc.kt");
}
@TestMetadata("Invoke.kt")
public void testInvoke() throws Exception {
doTest("idea/testData/highlighter/deprecated/Invoke.kt");
}
@TestMetadata("Operation.kt")
public void testOperation() throws Exception {
doTest("idea/testData/highlighter/deprecated/Operation.kt");
}
@TestMetadata("Property.kt")
public void testProperty() throws Exception {
doTest("idea/testData/highlighter/deprecated/Property.kt");
}
@TestMetadata("RangeTo.kt")
public void testRangeTo() throws Exception {
doTest("idea/testData/highlighter/deprecated/RangeTo.kt");
}
@TestMetadata("Setter.kt")
public void testSetter() throws Exception {
doTest("idea/testData/highlighter/deprecated/Setter.kt");
}
@TestMetadata("SuperCall.kt")
public void testSuperCall() throws Exception {
doTest("idea/testData/highlighter/deprecated/SuperCall.kt");
}
@TestMetadata("Trait.kt")
public void testTrait() throws Exception {
doTest("idea/testData/highlighter/deprecated/Trait.kt");
}
}