Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class annotation values with $ in name are null when used in Kotlin source #1671

Closed
bcorso opened this issue Dec 27, 2023 · 1 comment · Fixed by #1681
Closed

Class annotation values with $ in name are null when used in Kotlin source #1671

bcorso opened this issue Dec 27, 2023 · 1 comment · Fixed by #1681
Assignees
Milestone

Comments

@bcorso
Copy link

bcorso commented Dec 27, 2023

This issue was brought up in Dagger (google/dagger#4197), but looks like the underlying cause is a bug in KSP.

Say we have the following classes:

class `SomeClass$WithDollarSign`

annotation class MyAnnotation(val classes: Array<KClass<*>>)

@MyAnnotation(classes = [String::class, `SomeClass$WithDollarSign`::class, Number::class])
class Foo

The issue is that if we use KSP to inspect the annotation values of MyAnnotation.classes then the SomeClass$WithDollarSign type is returned as null. For example, say we have the following KSP processor:

class MyProcessor(
  private val codeGenerator: CodeGenerator,
  private val logger: KSPLogger,
  private val options: Map<String, String>
) : SymbolProcessor {

  @OptIn(KspExperimental::class)
  override fun process(resolver: Resolver): List<KSAnnotated> {
    val fooName = resolver.getKSNameFromString("Foo")
    val fooClass = resolver.getClassDeclarationByName(fooName)
    fooClass?.annotations?.forEach { annotation ->
      println("${annotation.annotationType}: ${
          annotation.arguments.map { "${it.name?.asString()}: ${it.value}" }.toList() 
      }")
    }
    return listOf()
  }
}

This will print the following:

MyAnnotation: [classes: [String, null, Number]]

Note: The above issue only happens when the annotation is used in a kotlin source. If the annotation is used in java source then things work as expected and I get:

MyAnnotation: [classes: [String, SomeClass$WithDollarSign, Number]]
@bcorso
Copy link
Author

bcorso commented Dec 28, 2023

Edit:

I mentioned above that the issue didn't happen when the annotation is used in a java source, but I realize now that that's only if the source is defined in the current compilation unit. If the Java source is defined in a dependency of the current compilation unit then the same issue of getting a null annotation value type happens for both Java and Kotlin sources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants