Is there a better way to check that the class of e...
# ksp
j
Is there a better way to check that the class of each element of a list is annotated?
Copy code
return (argument as Array<KClass<*>>)
    .toList()
    .all {
        resolver.getClassDeclarationByName(it::class.java.simpleName)
            ?.filterAnnotations<CrossViewEvent>()
            ?.toList()
            ?.isNotEmpty() ?: false
    }
is it possible to convert a
KClass
to a
KSType
without using the resolver?
d
Where did we get the
Array<KClass<*>>
from? How are the annotations you are using in your processor defined?
t
If all the classes are defined in sources, instead of classpath, this seems what
getSymbolsWithAnnotation
is about.
j
I have the following code :
Copy code
@CrossViewEvent
data class LoadStudents(val offset: Int, val limit: Int)

@CrossViewEvent
data class LoadTeachers(val offset: Int, val limit: Int)

--------------

@ViewEventsBuilder(
    crossViewEvents = [
        LoadStudents::class,
        LoadTeachers::class,
    ]
)
sealed class SchoolViewEventsBuilder
Now I want to check that every class listed inside the
crossViewEvents
parameter is annotated with
CrossViewEvent
. In the previous code block I shared, the code resides in a validator class that doesn’t directly have a reference to a
Resolver
. So I pass it from the processor to the validator and don’t know if that’s a bad practice or not. Then
argument
refers to
crossViewEvents
. I get the variable with this code:
Copy code
val argumentType = (viewEventsBuilderAnnotation?.arguments?.first()?.value as? KSType)
val argument = argumentType?.declaration as? KSClassDeclaration
d
If I do something quick in the debugger:
Copy code
resolver.getSymbolsWithAnnotation("ViewEventsBuilder")
   .first() // SchoolViewEventsBuilder
   .annotations
   .first() // ViewEventsBuilder
   .arguments
   .first() // crossViewEvents
I am seeing the
crossViewEvents
argument of the
@ViewEventsBuilder
is a
List<KSType>
rather than an
Array<KClass<*>>
. Since the elements in the list are already resolved to a
KSType
, this would mean you wouldn’t have to resolve them again
j
I did not though of casting the list as
List<KSType>
, nice!