[KAPT4] KT-51982 Implement generation of @Metadata annotations in Java stubs

Also adds rendering of @Metadata annotations in Kapt3 and Kapt4 tests
(currently disabled for a few tests).

Co-authored-by: Alexander Udalov <alexander.udalov@jetbrains.com>
This commit is contained in:
strangepleasures
2023-07-31 23:09:08 +02:00
committed by Space Team
parent f40fc1b0c6
commit 2423b7b62f
250 changed files with 15438 additions and 81 deletions
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.psi.KtFile
*
* To create a [KtAnalysisSession], please use [analyze] or one of its siblings.
*/
@OptIn(KtAnalysisApiInternals::class)
@OptIn(KtAnalysisApiInternals::class, KtAnalysisNonPublicApi::class)
@Suppress("AnalysisApiMissingLifetimeCheck")
public abstract class KtAnalysisSession(final override val token: KtLifetimeToken) : KtLifetimeOwner,
KtSmartCastProviderMixIn,
@@ -67,7 +67,8 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
KtScopeSubstitutionMixIn,
KtSymbolProviderByJavaPsiMixIn,
KtResolveExtensionInfoProviderMixIn,
KtCompilerFacilityMixIn {
KtCompilerFacilityMixIn,
KtMetadataCalculatorMixIn {
public abstract val useSiteModule: KtModule
@@ -186,6 +187,8 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
@KtAnalysisApiInternals
protected abstract val symbolProviderByJavaPsiImpl: KtSymbolProviderByJavaPsi
internal val metadataCalculator: KtMetadataCalculator get() = metadataCalculatorImpl
protected abstract val metadataCalculatorImpl: KtMetadataCalculator
@PublishedApi
internal val typesCreator: KtTypeCreator
@@ -195,4 +198,4 @@ public abstract class KtAnalysisSession(final override val token: KtLifetimeToke
public fun KtAnalysisSession.getModule(element: PsiElement): KtModule {
return ProjectStructureProvider.getModule(useSiteModule.project, element, useSiteModule)
}
}
@@ -8,6 +8,9 @@ package org.jetbrains.kotlin.analysis.api
@RequiresOptIn("Internal Analysis API component which should not be used outside the Analysis API implementation modules as it does not have any compatibility guarantees")
public annotation class KtAnalysisApiInternals
@RequiresOptIn("Internal Analysis API component which is used from other modules in the Kotlin project and is not supposed to be used anywhere else since it has no compatibility guarantees")
public annotation class KtAnalysisNonPublicApi
@RequiresOptIn("Analysis should not be allowed to be ran from EDT thread, otherwise it may cause IDE freezes")
public annotation class KtAllowAnalysisOnEdt
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2023 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.analysis.api.components
import com.google.common.collect.Multimap
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.api.KtAnalysisNonPublicApi
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
@KtAnalysisNonPublicApi
public abstract class KtMetadataCalculator : KtAnalysisSessionComponent() {
public abstract fun calculateMetadata(ktClass: KtClassOrObject, mapping: Multimap<KtElement, PsiElement>): Metadata
public abstract fun calculateMetadata(ktFile: KtFile, mapping: Multimap<KtElement, PsiElement>): Metadata
}
@KtAnalysisNonPublicApi
public interface KtMetadataCalculatorMixIn : KtAnalysisSessionMixIn {
/**
* Calculates metadata that would be generated by the compiler in case this class was compiled to the JVM class file.
*
* @param mapping map containing the light elements ([KtLightElement]) for each callable declaration in this class.
*/
public fun KtClassOrObject.calculateMetadata(mapping: Multimap<KtElement, PsiElement>): Metadata =
withValidityAssertion { analysisSession.metadataCalculator.calculateMetadata(this, mapping) }
/**
* Calculates metadata that would be generated by the compiler in case this file was compiled to the JVM class file.
*
* @param mapping map containing the light elements ([KtLightElement]) for each callable declaration in this file.
*/
public fun KtFile.calculateMetadata(mapping: Multimap<KtElement, PsiElement>): Metadata =
withValidityAssertion { analysisSession.metadataCalculator.calculateMetadata(this, mapping) }
}