Skip to content

Commit

Permalink
Don't include non-exist values in KSAnnotation.defaultArguments
Browse files Browse the repository at this point in the history
Missing arguments can be detected by missing names.

This aligns the implementation of Java and Non-Java implementationss, as
well as KSP1.
  • Loading branch information
ting-yuan committed Oct 9, 2024
1 parent ca5f624 commit 834cd5d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import com.intellij.psi.PsiType
import com.intellij.psi.impl.compiled.ClsClassImpl
import org.jetbrains.kotlin.analysis.api.KaImplementationDetail
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseNamedAnnotationValue
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaUnsupportedAnnotationValueImpl
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken
import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin
import org.jetbrains.kotlin.analysis.api.types.KaType
Expand Down Expand Up @@ -136,7 +134,7 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o
}
}
} else {
symbol.valueParameters.map { valueParameterSymbol ->
symbol.valueParameters.mapNotNull { valueParameterSymbol ->
valueParameterSymbol.getDefaultValue().let { constantValue ->
KSValueArgumentImpl.getCached(
KaBaseNamedAnnotationValue(
Expand All @@ -145,10 +143,7 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o
// fallback to unsupported annotation value to indicate such use cases.
// when seeing unsupported annotation value we return `null` for the value.
// which might still be incorrect but there might not be a perfect way.
constantValue
?: KaUnsupportedAnnotationValueImpl(
KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
)
constantValue ?: return@let null
),
this@KSAnnotationJavaImpl,
Origin.SYNTHETIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.google.devtools.ksp.impl.symbol.kotlin
import com.google.devtools.ksp.common.IdKeyPair
import com.google.devtools.ksp.common.KSObjectCache
import com.google.devtools.ksp.common.impl.KSNameImpl
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.java.KSValueArgumentLiteImpl
import com.google.devtools.ksp.impl.symbol.java.calcValue
import com.google.devtools.ksp.symbol.*
Expand All @@ -31,8 +30,6 @@ import com.intellij.psi.impl.compiled.ClsClassImpl
import org.jetbrains.kotlin.analysis.api.KaImplementationDetail
import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotation
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseNamedAnnotationValue
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaUnsupportedAnnotationValueImpl
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*
import org.jetbrains.kotlin.psi.KtAnnotationEntry
Expand Down Expand Up @@ -106,15 +103,12 @@ class KSAnnotationImpl private constructor(
}
} else {
symbol.memberScope.constructors.singleOrNull()?.let {
it.valueParameters.map { valueParameterSymbol ->
it.valueParameters.mapNotNull { valueParameterSymbol ->
valueParameterSymbol.getDefaultValue().let { constantValue ->
KSValueArgumentImpl.getCached(
KaBaseNamedAnnotationValue(
valueParameterSymbol.name,
constantValue
?: KaUnsupportedAnnotationValueImpl(
KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
)
constantValue ?: return@let null
),
this@KSAnnotationImpl,
Origin.SYNTHETIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.google.devtools.ksp.impl.symbol.kotlin.resolved
import com.google.devtools.ksp.common.IdKeyPair
import com.google.devtools.ksp.common.KSObjectCache
import com.google.devtools.ksp.common.impl.KSNameImpl
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.java.KSValueArgumentLiteImpl
import com.google.devtools.ksp.impl.symbol.java.calcValue
import com.google.devtools.ksp.impl.symbol.kotlin.*
Expand All @@ -27,8 +26,6 @@ import com.intellij.psi.impl.compiled.ClsClassImpl
import org.jetbrains.kotlin.analysis.api.KaImplementationDetail
import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotation
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseNamedAnnotationValue
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaUnsupportedAnnotationValueImpl
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*

Expand Down Expand Up @@ -96,15 +93,12 @@ class KSAnnotationResolvedImpl private constructor(
}
} else {
symbol.memberScope.constructors.singleOrNull()?.let {
it.valueParameters.map { valueParameterSymbol ->
it.valueParameters.mapNotNull { valueParameterSymbol ->
valueParameterSymbol.getDefaultValue().let { constantValue ->
KSValueArgumentImpl.getCached(
KaBaseNamedAnnotationValue(
valueParameterSymbol.name,
constantValue
?: KaUnsupportedAnnotationValueImpl(
KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
)
constantValue ?: return@let null
),
this@KSAnnotationResolvedImpl,
Origin.SYNTHETIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
// EXPECTED:
// defaultInNested
// []
// null
// null
// null
// SomeClass$WithDollarSign
// Str
// 42
Expand Down

0 comments on commit 834cd5d

Please sign in to comment.