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
Bug: ClassNotFoundException, but it does exist in the project #1038
Comments
Ahhh, it seems that one MR to fix this already exists, #951 Sorry for the issue report. |
Even after the merge of #1039 the code in my sample does not work well. |
here we have an updated project using a local build
and the build.gradle pointing to it
|
Did you resolve it? |
No. I am still waiting the ksp developers to take a look at the sample project. Cc: @neetopia |
The problem with your project is that you are trying to get |
Is this a planned feature for the future? IMO there are totally valid use cases would be very convenient to reflect on elements in the classpath while using KSP. My particular use case is injecting permission policies defined by the developer into generated code. |
As I said, it is more like a limit of reflection itself (class has to be on classpath), is it possible to avoid using reflection for your use case? Or alternative way like having some interfaces in your library and reflect on the interface instead of the actual implementation generated? |
Not really, in this case I have an interface, which is implemented But it sounds like I will need to rearchitect that, since it's a limitation of the language more than anything. |
By inspection do you mean generated code will need some logic that looks into the implementation provided by end users? It still sounds to me that reflection is not necessary, unless you need to access the object of the implementation at processing time. KSP already offers inspection into source code and generated code via KSP APIs, but if your use case is beyond the inspection provided by KSP, it might not work. |
personally, i don't need to access the object, I just need to be able yoink the fully qualified name, but the processor errors out when trying to access any information of the class |
Me too, I just need to class name, I think this is super odd, because the ClassNotFoundException message that we receive contains the class name. |
This is a deprecated answer and a better way for this issue: #1038 (comment) Me too, but I found that I could get the corresponding class qualifiedName from the Exception, howerve It's not a perfect solution;
KSTypeNotPresentException is thrown from here
|
Is there any updates for this issue? |
Recently, I adapted KSP for WMRouter project. During the adaptation process, I found a more suitable method to solve issue1038 (at least I think so) We should adopt the solution proposed by bennyhuo issue694 to solve the problem of class parsing; The operations of parsing the class object in the annotation and obtaining the fully qualified name of its class can be divided into the following types: 1、 Class[]: Sample project:JailedBird/WMRouterKspCompiler
solution:
2、 Class (A little different from Class[]) Sample project: JailedBird/AutoService
solution:
Maybe help🤞 |
Perhaps you can try this method:#1038 (comment) |
This looks like a work around, not a fix or a reliable solution. |
@JailedBird Great explanation, thank you! At least this work around helped me! |
In addition, here is adapted solution to work with KotlinPoet: @OptIn(KspExperimental::class)
fun parseAnnotationClassParameter(block: () -> List<KClass<*>>): List<ClassName> {
return try {
block.invoke().map { ClassName(it.java.`package`.name, it.simpleName.orEmpty()) }
} catch (e: KSTypesNotPresentException) {
val res = mutableListOf<ClassName>()
val ksTypes = e.ksTypes
for (ksType in ksTypes) {
val declaration = ksType.declaration
if (declaration is KSClassDeclaration) {
res.add(declaration.toClassName())
}
}
res
}
} |
Hello there, I am trying to read my annotation using
and getting this error
This is a bit odd since the full qualified name in the exception seems to correct.
I tried this, but I still got the same error.
A project to reproduce it is attached here
To my specific needs I don't need to instantiate the annotation, but to get the String with the qualified name for
ActivityReference.activityName
, would it be possible with KSP ?The text was updated successfully, but these errors were encountered: