From 2c69d30a89b8333ea3606277d0a2453484fe2daf Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Mon, 20 May 2024 23:56:54 -0700 Subject: [PATCH] UAST: make UDeclaration hasAnnotation conform to findAnnotation ...such that it can really findAnnotation when it hasAnnotation ^IDEA-353785 fixed GitOrigin-RevId: a3e9becfbfc258e5474bdf873c5bd2ee9a577f7c --- .../uast/declarations/UDeclaration.kt | 4 +++ .../uast/test/java/JavaUastApiTest.kt | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/uast/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt b/uast/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt index d9d3a7f95eb3..60ff15a706cd 100644 --- a/uast/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt +++ b/uast/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt @@ -49,6 +49,10 @@ interface UDeclaration : UElement, PsiJvmModifiersOwner, UAnnotated { get() = UastVisibility[this] override fun accept(visitor: UastTypedVisitor, data: D): R = visitor.visitDeclaration(this, data) + + override fun hasAnnotation(fqName: String): Boolean { + return findAnnotation(fqName) != null + } } interface UDeclarationEx : UDeclaration { diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt index 587bc723652c..6bcb99da0539 100644 --- a/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt @@ -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()!! + 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) + } }