(UltraLightClasses) fast-path for Deprecated.HIDDEN annotation

This commit is contained in:
Vladimir Ilmov
2020-06-08 21:32:32 +02:00
parent 852e860743
commit 40ec794c66
2 changed files with 95 additions and 4 deletions
@@ -311,11 +311,38 @@ private fun packMethodFlags(access: Int, isInterface: Boolean): Int {
}
internal fun KtModifierListOwner.isHiddenByDeprecation(support: KtUltraLightSupport): Boolean {
val jetModifierList = this.modifierList ?: return false
if (jetModifierList.annotationEntries.isEmpty()) return false
if (annotationEntries.isEmpty()) return false
val annotations = annotationEntries.filter { annotation ->
annotation.looksLikeDeprecated()
}
if (annotations.isNotEmpty()) { // some candidates found
val deprecated = support.findAnnotation(this, KotlinBuiltIns.FQ_NAMES.deprecated)?.second
return (deprecated?.argumentValue("level") as? EnumValue)?.enumEntryName?.asString() == "HIDDEN"
} else {
return false
}
}
val deprecated = support.findAnnotation(this, KotlinBuiltIns.FQ_NAMES.deprecated)?.second
return (deprecated?.argumentValue("level") as? EnumValue)?.enumEntryName?.asString() == "HIDDEN"
fun KtAnnotationEntry.looksLikeDeprecated(): Boolean {
val arguments = valueArguments.filterIsInstance<KtValueArgument>().filterIndexed { index, valueArgument ->
index == 2 || valueArgument.looksLikeLevelArgument() // for named/not named arguments
}
for (argument in arguments) {
val hiddenByDotQualifiedCandidates = argument.children.filterIsInstance<KtDotQualifiedExpression>().filter {
val lastChild = it.children.last()
lastChild.text == "HIDDEN"
}
val hiddenByNameReferenceExpressionCandidates = argument.children.filterIsInstance<KtNameReferenceExpression>().filter {
it.text == "HIDDEN"
}
if (hiddenByDotQualifiedCandidates.isNotEmpty() || hiddenByNameReferenceExpressionCandidates.isNotEmpty())
return true
}
return false
}
fun KtValueArgument.looksLikeLevelArgument(): Boolean {
return children.filterIsInstance<KtValueArgumentName>().any { it.asName.asString() == "level" }
}
internal fun KtAnnotated.isJvmStatic(support: KtUltraLightSupport): Boolean =
@@ -33,6 +33,70 @@ class KtLightAnnotationTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor =
KotlinJdkAndLibraryProjectDescriptor(ForTestCompileRuntime.runtimeJarForTests())
fun testIsHiddenByDeprecated() {
myFixture.configureByText(
"test.kt", """
import kotlin.DeprecationLevel.WARNING
import kotlin.DeprecationLevel.HIDDEN
import java.lang.annotation.ElementType
import kotlin.DeprecationLevel
import kotlin.annotation.AnnotationTarget
@kotlin.annotation.Target(kotlin.annotation.AnnotationTarget.FUNCTION)
annotation class Dep(
val message: String = "",
val message1: String = "",
val level: DeprecationLevel = DeprecationLevel.WARNING
)
typealias LOL = Deprecated
typealias DL = DeprecationLevel
class A {
@Deprecated("", ReplaceWith("a"), HIDDEN)
fun a() {}
@Deprecated(message = "", level = HIDDEN)
fun b() {}
@Deprecated(message = "", replaceWith = ReplaceWith(""), level = DeprecationLevel.HIDDEN)
fun c() {}
@Deprecated(message = "", replaceWith = ReplaceWith(""), level = DeprecationLevel.WARNING)
fun d() {}
@Deprecated(message = "", replaceWith = ReplaceWith(""), level = WARNING)
fun e() {}
@Deprecated(message = "", replaceWith = ReplaceWith(""))
fun f() {}
@Deprecated("")
fun g() {}
@Deprecated(message = "", level = WARNING)
fun h() {}
@Dep(level = DeprecationLevel.HIDDEN)
fun i() {}
@Dep("", "", DeprecationLevel.HIDDEN)
fun j() {}
@LOL(level = HIDDEN, message="")
fun k() {}
@Deprecated(level = DL.HIDDEN, message="")
fun l() {}
}
""".trimIndent()
)
myFixture.testHighlighting("test.kt")
val methods = myFixture.findClass("A").methods.map { it.name }.sorted()
TestCase.assertEquals(listOf("A","d","e","f","g","h","i","j"), methods)
}
fun testBooleanAnnotationDefaultValue() {
myFixture.addClass(
"""