From 8b5d6827c9a9fa4eb9022bf9f69dff1f98f83cdc Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Tue, 7 Sep 2021 17:07:05 +0700 Subject: [PATCH] [ULC] fix compatibility with java analyzer for repeatable annotation * imitate `getReferenceName` to provide interop with existed java analyzer [AnnotationsHighlightUtil.java:238](https://github.com/JetBrains/intellij-community/blob/ad7c664356b2f21af1fa204d5b7b014d8af10fd3/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java#L238) ^KTIJ-19318 --- .../asJava/classes/ultraLightAnnotations.kt | 6 +++--- .../KtLightPsiJavaCodeReferenceElement.kt | 5 +++-- .../elements/KtToJvmAnnotationsConverter.kt | 16 ++++++++++++++-- .../kotlin/asJava/elements/lightAnnotations.kt | 18 ++++-------------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt index f4b6970c8ea..7d9859e6c40 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt @@ -9,7 +9,6 @@ import com.intellij.psi.* import com.intellij.psi.impl.PsiImplUtil import com.intellij.psi.impl.light.LightIdentifier import org.jetbrains.kotlin.asJava.elements.KtLightAbstractAnnotation -import org.jetbrains.kotlin.asJava.elements.KtLightAnnotationForSourceEntry import org.jetbrains.kotlin.asJava.elements.KtLightElementBase import org.jetbrains.kotlin.asJava.elements.KtLightNullabilityAnnotation import org.jetbrains.kotlin.descriptors.DeclarationDescriptor @@ -45,9 +44,10 @@ class KtUltraLightSimpleAnnotation( private val annotationFqName: String?, private val argumentsList: List>>, private val ultraLightSupport: KtUltraLightSupport, - parent: PsiElement + parent: PsiElement, + private val nameReferenceElementProvider: (() -> PsiJavaCodeReferenceElement?)? = null, ) : KtLightAbstractAnnotation(parent, computeDelegate = null) { - override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = null + override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = nameReferenceElementProvider?.invoke() private val parameterList = ParameterListImpl() diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightPsiJavaCodeReferenceElement.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightPsiJavaCodeReferenceElement.kt index ec86fb95482..c863cbab736 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightPsiJavaCodeReferenceElement.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightPsiJavaCodeReferenceElement.kt @@ -14,7 +14,8 @@ import org.jetbrains.kotlin.asJava.classes.lazyPub class KtLightPsiJavaCodeReferenceElement( private val ktElement: PsiElement, reference: () -> PsiReference?, - clsDelegateProvider: () -> PsiJavaCodeReferenceElement? + clsDelegateProvider: () -> PsiJavaCodeReferenceElement?, + private val customReferenceName: String? = null, ) : PsiElement by ktElement, PsiReference by LazyPsiReferenceDelegate(ktElement, reference), @@ -29,7 +30,7 @@ class KtLightPsiJavaCodeReferenceElement( override fun getTypeParameters(): Array = delegate?.typeParameters ?: emptyArray() - override fun getReferenceName(): String? = delegate?.referenceName + override fun getReferenceName(): String? = customReferenceName ?: delegate?.referenceName override fun isQualified(): Boolean = delegate?.isQualified ?: false diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt index d96a9c395e1..b4752c81383 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtModifierListOwner import org.jetbrains.kotlin.resolve.constants.ArrayValue @@ -145,10 +146,21 @@ internal fun PsiAnnotation.tryConvertAsRepeatable( JAVA_LANG_ANNOTATION_REPEATABLE, listOfNotNull(value), support, - parent - ) + parent, + ) { + safeAs()?.kotlinOrigin?.safeAs()?.let { + KtLightPsiJavaCodeReferenceElement( + ktElement = it, + reference = { null }, + clsDelegateProvider = { null }, + customReferenceName = JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME, + ) + } + } } +internal val JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME: String get() = FqNames.repeatable.shortName().asString() + internal fun PsiAnnotation.tryConvertAsRepeatableContainer(support: KtUltraLightSupport): KtLightAbstractAnnotation? = tryConvertAs( support, FqNames.repeatable, diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt index f9d1502f409..d3108f633fd 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2016 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. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.asJava.elements @@ -127,7 +116,8 @@ class KtLightAnnotationForSourceEntry( (kotlinOrigin as? KtAnnotationEntry)?.typeReference?.reference ?: (kotlinOrigin.calleeExpression?.nameReference)?.references?.firstOrNull() }, - { lazyClsDelegate?.value?.nameReferenceElement } + { lazyClsDelegate?.value?.nameReferenceElement }, + if (qualifiedName == CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE) JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME else null, ) private val ktLightAnnotationParameterList by lazyPub { KtLightAnnotationParameterList() }