-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AA: support java annotation argument evaluation
(cherry picked from commit c1feea0)
- Loading branch information
Showing
8 changed files
with
346 additions
and
39 deletions.
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
180
...ysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/java/KSAnnotationJavaImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package com.google.devtools.ksp.impl.symbol.java | ||
|
||
import com.google.devtools.ksp.KSObjectCache | ||
import com.google.devtools.ksp.getClassDeclarationByName | ||
import com.google.devtools.ksp.impl.ResolverAAImpl | ||
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeImpl | ||
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeReferenceImpl | ||
import com.google.devtools.ksp.impl.symbol.kotlin.KSValueArgumentImpl | ||
import com.google.devtools.ksp.impl.symbol.kotlin.analyze | ||
import com.google.devtools.ksp.impl.symbol.kotlin.classifierSymbol | ||
import com.google.devtools.ksp.impl.symbol.kotlin.getDefaultValue | ||
import com.google.devtools.ksp.impl.symbol.kotlin.toKSDeclaration | ||
import com.google.devtools.ksp.impl.symbol.kotlin.toKtClassSymbol | ||
import com.google.devtools.ksp.impl.symbol.kotlin.toLocation | ||
import com.google.devtools.ksp.processing.impl.KSNameImpl | ||
import com.google.devtools.ksp.symbol.AnnotationUseSiteTarget | ||
import com.google.devtools.ksp.symbol.ClassKind | ||
import com.google.devtools.ksp.symbol.KSAnnotation | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSName | ||
import com.google.devtools.ksp.symbol.KSNode | ||
import com.google.devtools.ksp.symbol.KSTypeReference | ||
import com.google.devtools.ksp.symbol.KSValueArgument | ||
import com.google.devtools.ksp.symbol.KSVisitor | ||
import com.google.devtools.ksp.symbol.Location | ||
import com.google.devtools.ksp.symbol.Origin | ||
import com.intellij.lang.jvm.JvmClassKind | ||
import com.intellij.psi.JavaPsiFacade | ||
import com.intellij.psi.PsiAnnotation | ||
import com.intellij.psi.PsiAnnotationMemberValue | ||
import com.intellij.psi.PsiArrayInitializerMemberValue | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiField | ||
import com.intellij.psi.PsiLiteralValue | ||
import com.intellij.psi.PsiReference | ||
import com.intellij.psi.PsiType | ||
import com.intellij.psi.impl.source.PsiAnnotationMethodImpl | ||
import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue | ||
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol | ||
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin | ||
import org.jetbrains.kotlin.analysis.api.types.KtType | ||
import org.jetbrains.kotlin.name.ClassId | ||
|
||
class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation) : KSAnnotation { | ||
companion object : KSObjectCache<PsiAnnotation, KSAnnotationJavaImpl>() { | ||
fun getCached(psi: PsiAnnotation) = | ||
KSAnnotationJavaImpl.cache.getOrPut(psi) { KSAnnotationJavaImpl(psi) } | ||
} | ||
|
||
private val type: KtType by lazy { | ||
(ResolverAAImpl.instance.getClassDeclarationByName(psi.qualifiedName!!)!!.asStarProjectedType() as KSTypeImpl) | ||
.type | ||
} | ||
|
||
override val annotationType: KSTypeReference by lazy { | ||
KSTypeReferenceImpl.getCached(type, this) | ||
} | ||
|
||
override val arguments: List<KSValueArgument> by lazy { | ||
val annotationConstructor = analyze { | ||
(type.classifierSymbol() as KtClassOrObjectSymbol).getMemberScope().getConstructors().singleOrNull() | ||
} | ||
val presentArgs = psi.parameterList.attributes.mapIndexed { index, it -> | ||
val name = it.name ?: annotationConstructor?.valueParameters?.getOrNull(index)?.name?.asString() | ||
val value = it.value | ||
val calculatedValue: Any? = if (value is PsiArrayInitializerMemberValue) { | ||
value.initializers.map { | ||
calcValue(it) | ||
} | ||
} else { | ||
calcValue(it.value) | ||
} | ||
KSValueArgumentLiteImpl.getCached( | ||
name?.let { KSNameImpl.getCached(it) }, | ||
calculatedValue, | ||
Origin.JAVA | ||
) | ||
} | ||
val presentValueArgumentNames = presentArgs.map { it.name?.asString() ?: "" } | ||
presentArgs + defaultArguments.filter { it.name?.asString() !in presentValueArgumentNames } | ||
} | ||
|
||
override val defaultArguments: List<KSValueArgument> by lazy { | ||
analyze { | ||
(type.classifierSymbol() as KtClassOrObjectSymbol).getMemberScope().getConstructors().singleOrNull() | ||
?.let { symbol -> | ||
if (symbol.origin == KtSymbolOrigin.JAVA && symbol.psi != null) { | ||
(symbol.psi as PsiClass).allMethods.filterIsInstance<PsiAnnotationMethodImpl>() | ||
.mapNotNull { annoMethod -> | ||
annoMethod.defaultValue?.let { | ||
KSValueArgumentLiteImpl.getCached( | ||
KSNameImpl.getCached(annoMethod.name), | ||
calcValue(it), | ||
Origin.SYNTHETIC | ||
) | ||
} | ||
} | ||
} else { | ||
symbol.valueParameters.mapNotNull { valueParameterSymbol -> | ||
valueParameterSymbol.getDefaultValue()?.let { constantValue -> | ||
KSValueArgumentImpl.getCached( | ||
KtNamedAnnotationValue( | ||
valueParameterSymbol.name, constantValue, | ||
), | ||
Origin.SYNTHETIC | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} ?: emptyList() | ||
} | ||
|
||
override val shortName: KSName by lazy { | ||
KSNameImpl.getCached(psi.qualifiedName!!.split(".").last()) | ||
} | ||
|
||
override val useSiteTarget: AnnotationUseSiteTarget? = null | ||
|
||
override val origin: Origin = Origin.JAVA | ||
|
||
override val location: Location | ||
get() = psi.toLocation() | ||
|
||
override val parent: KSNode? | ||
get() = TODO("Not yet implemented") | ||
|
||
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R { | ||
return visitor.visitAnnotation(this, data) | ||
} | ||
|
||
override fun toString(): String { | ||
return "@${shortName.asString()}" | ||
} | ||
} | ||
|
||
fun calcValue(value: PsiAnnotationMemberValue?): Any? { | ||
if (value is PsiAnnotation) { | ||
return KSAnnotationJavaImpl.getCached(value) | ||
} | ||
val result = when (value) { | ||
is PsiReference -> value.resolve()?.let { resolved -> | ||
JavaPsiFacade.getInstance(value.project).constantEvaluationHelper.computeConstantExpression(value) | ||
?: resolved | ||
} | ||
else -> value?.let { | ||
JavaPsiFacade.getInstance(value.project).constantEvaluationHelper.computeConstantExpression(value) | ||
} | ||
} | ||
return when (result) { | ||
is PsiType -> { | ||
analyze { | ||
(ClassId.fromString(result.canonicalText).toKtClassSymbol()?.toKSDeclaration() as? KSClassDeclaration) | ||
?.asStarProjectedType() | ||
} | ||
} | ||
is PsiLiteralValue -> { | ||
result.value | ||
} | ||
is PsiField -> { | ||
// manually handle enums as constant expression evaluator does not seem to be resolving them. | ||
val containingClass = result.containingClass | ||
if (containingClass?.classKind == JvmClassKind.ENUM) { | ||
// this is an enum entry | ||
containingClass.qualifiedName?.let { | ||
ResolverAAImpl.instance!!.getClassDeclarationByName(it) | ||
}?.declarations?.find { | ||
it is KSClassDeclaration && it.classKind == ClassKind.ENUM_ENTRY && | ||
it.simpleName.asString() == result.name | ||
}?.let { (it as KSClassDeclaration).asStarProjectedType() } | ||
?.let { | ||
return it | ||
} | ||
} else { | ||
null | ||
} | ||
} | ||
else -> result | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...s-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/java/KSValueArgumentLiteImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.google.devtools.ksp.impl.symbol.java | ||
|
||
import com.google.devtools.ksp.IdKeyPair | ||
import com.google.devtools.ksp.KSObjectCache | ||
import com.google.devtools.ksp.symbol.KSAnnotation | ||
import com.google.devtools.ksp.symbol.KSName | ||
import com.google.devtools.ksp.symbol.KSNode | ||
import com.google.devtools.ksp.symbol.KSValueArgument | ||
import com.google.devtools.ksp.symbol.KSVisitor | ||
import com.google.devtools.ksp.symbol.Location | ||
import com.google.devtools.ksp.symbol.Origin | ||
|
||
class KSValueArgumentLiteImpl private constructor( | ||
override val name: KSName?, | ||
override val value: Any?, | ||
override val origin: Origin | ||
) : KSValueArgument { | ||
companion object : KSObjectCache<IdKeyPair<KSName?, Any?>, KSValueArgumentLiteImpl>() { | ||
fun getCached(name: KSName?, value: Any?, origin: Origin) = | ||
KSValueArgumentLiteImpl.cache | ||
.getOrPut(IdKeyPair(name, value)) { KSValueArgumentLiteImpl(name, value, origin) } | ||
} | ||
override val isSpread: Boolean = false | ||
|
||
override val annotations: Sequence<KSAnnotation> = emptySequence() | ||
|
||
override val location: Location | ||
get() = TODO("Not yet implemented") | ||
|
||
override val parent: KSNode? | ||
get() = TODO("Not yet implemented") | ||
|
||
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R { | ||
return visitor.visitValueArgument(this, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.