How to get the value of a Constant used in an annotation from KtAnnotated?

Answered

I have an annotation as below
@MyAnnotation(name="foo", value="bar") but in some files I have @MyAnnotation(name=<ClassName>.FOO, value="bar"). I am trying to develop a plugin that will get “foo” for both cases. I can do it for the first case by getting KtAnnotationEntry, and then finding the argument expression text. How do I evaluate a class constant in the second case?

0
1 comment

Hi Kushal,

You can try:

ConstantExpressionEvaluator expressionEvaluator = LanguageConstantExpressionEvaluator.INSTANCE.forLanguage(expression.getLanguage());
if (expressionEvaluator != null) {
  return expressionEvaluator.computeConstantExpression(expression, true);
}
0

Please sign in to leave a comment.