Skip to content

Commit

Permalink
Kapt: Add ability to get constant value from KtLightAnnotation
Browse files Browse the repository at this point in the history
(cherry picked from commit 7180961)
  • Loading branch information
yanex committed Aug 29, 2016
1 parent 0181dd0 commit 884e653
Showing 1 changed file with 9 additions and 0 deletions.
Expand Up @@ -24,11 +24,13 @@ import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.TypeUtils

class KtLightAnnotation(
override val clsDelegate: PsiAnnotation,
Expand Down Expand Up @@ -70,6 +72,13 @@ class KtLightAnnotation(
else -> null
}
}

fun getConstantValue(): Any? {
val expression = originalExpression as? KtExpression ?: return null
val annotationEntry = this@KtLightAnnotation.kotlinOrigin
val context = LightClassGenerationSupport.getInstance(project).analyze(annotationEntry)
return context[BindingContext.COMPILE_TIME_VALUE, expression]?.getValue(TypeUtils.NO_EXPECTED_TYPE)
}

private fun unwrapArray(arguments: List<ValueArgument>): PsiElement? {
val arrayInitializer = parent as? LightArrayInitializerValue ?: return null
Expand Down

0 comments on commit 884e653

Please sign in to comment.