UAST: make UDeclaration hasAnnotation conform to findAnnotation

...such that it can really findAnnotation when it hasAnnotation

^IDEA-353785 fixed

GitOrigin-RevId: a3e9becfbfc258e5474bdf873c5bd2ee9a577f7c
This commit is contained in:
Jinseong Jeon
2024-05-20 23:56:54 -07:00
committed by intellij-monorepo-bot
parent f9e3bd1baa
commit 2c69d30a89
2 changed files with 39 additions and 0 deletions

View File

@@ -49,6 +49,10 @@ interface UDeclaration : UElement, PsiJvmModifiersOwner, UAnnotated {
get() = UastVisibility[this]
override fun <D, R> accept(visitor: UastTypedVisitor<D, R>, data: D): R = visitor.visitDeclaration(this, data)
override fun hasAnnotation(fqName: String): Boolean {
return findAnnotation(fqName) != null
}
}
interface UDeclarationEx : UDeclaration {

View File

@@ -303,4 +303,39 @@ class JavaUastApiTest : AbstractJavaUastTest() {
)
TestCase.assertEquals(1, count)
}
@Test
fun testHasAndFindTypeUseAnnotation() {
val file = myFixture.configureByText(
"Test.java",
"""
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface MyNullable {}
class Test {
@MyNullable String test() {
return null;
}
}
""".trimIndent()
)
val uFile = file.toUElementOfType<UFile>()!!
var count = 0
uFile.accept(
object : AbstractUastVisitor() {
override fun visitMethod(node: UMethod): Boolean {
if (node.hasAnnotation("MyNullable")) {
val anno = node.findAnnotation("MyNullable")
TestCase.assertNotNull(anno)
count++
}
return super.visitMethod(node)
}
}
)
// IDEA-336319: TYPE_USE should not be applicable to UMethod
TestCase.assertEquals(0, count)
}
}