Skip to content

typeArguments returns an empty list #2622

@Scogun

Description

@Scogun

In my library I need to inspect generic type arguments from an annotation.
Until version 2.0.21-1.0.28 this worked fine, but starting with version 2.2.0-* it seems broken.

Example

private fun associateWithFor(item: KSAnnotation): List<ClassName> {
    if (item.annotationType.toString() == MapConvert::class.simpleName) {
        return item.annotationType.element!!
            .typeArguments
            .take(2)
            .map { it.type!!.resolve().toClassName() }
    }
    return emptyList()
}

Annotation definition

@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.SOURCE)
@Repeatable
annotation class MapConvert<S: Any, D: Any, C : KOMMConverter<S, *, D, *>>(
    val converter: KClass<C>, 
    val name: String = ""
)

Usage

@MapConvert<SourceObject, JvmDestinationObject, JvmDestinationCostConverter>(
    converter = JvmDestinationCostConverter::class
)

Problem

  • On 2.0.21-1.0.28: item.annotationType.element!!.typeArguments contains the expected generic arguments (SourceObject, JvmDestinationObject).

  • On 2.2.0-* (and newer): item.annotationType.element!!.typeArguments is always empty.

Expected behavior
Generic type arguments provided to an annotation should be accessible via annotationType.element!!.typeArguments, just like before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2affects usability but not blocks users

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions