What’s on the classpath used by the proxies return...
# ksp
p
What’s on the classpath used by the proxies returned from
getAnnotationsByType
? Is it expected that
KClass
annotation parameters that reference user-defined classes won’t work? Both of these examples fail with a
KSTypesNotPresentException
ultimately caused by a
ClassNotFoundException
if I try to access the
includes
property on the proxy:
Copy code
@Module class First
@Module(includes = [First::class]) Second

@Module(includes = [FromUpstreamModule::class]) Third
This works (it’s invalid for unrelated reasons, but the class lookup works):
Copy code
@Module(includes = [Unit::class]) Fourth
This fails with the same exception but for a slightly different reason - it’s trying to look up
kotlin.String
at runtime (presumably it should be translated to
java.lang.String
at some point for JVM projects):
Copy code
@Module(includes = [String::class]) Fifth
j
If you are trying to access a class from the source code in reflection, then it is kinda expected it will fail, since the class is not yet to be compiled to class files. But it seems in your case it is not the exact same issue, what do you mean by “access the
includes
property on the proxy”?
p
Something like this:
Copy code
annotated.getAnnotationsByType(Module::class).single().includes
j
I see, I will expect at least first case to fail since the class is not yet compiled.
👍 1
p
Where
@Module
is defined like this (my actual use case is slightly different but fundamentally the same):
Copy code
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
annotation class Module(val includes: Array<KClass<*>> = [])
j
is
Module
defined in another compile module as well?
p
Yeah
j
I will be interested in the failure of
Third
and
Fifth
, I will take a note.
❤️ 1
p
It’s not really a problem - using
KSAnnotation.arguments
works fine, I get a
List<KSType>
as the argument value. I guess I was wondering what the use case for
getAnnotationsByType
is if things like this are expected to blow up - is it more of a convenience for annotations with primitive/`String` etc. parameters?
j
I can’t recall the initial motivation add it, but I think it is related to be compatible to some common Java annotation processor utility functions that some processors are using, it is located in the
api
util module meaning it can be implemented by processor users themselves, and added there just for some convenience
👍 1
g
Just spend a couple of hours before finding this discussion. Could be good to have a warning or warn when allowing retrieval of annotation using KClass 😅
Just found that it's also an issue with Annotation that has another Annotation as parameter : if the default value is used, calling the member from getAnnotationsByType() generate a NullPointerException. ⚠️