-
Notifications
You must be signed in to change notification settings - Fork 264
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
Synthetic classes for top level kotlin files #396
Comments
a bit more information on this. I don't think snythesizing a class in KSP is the right way to go because the KSType does not really exist and it is bound to explode in some cases (at least that is what happened when i tried to do it in Room). I "think" a nicer solution might be to provide a method in KspResolver to get the qName for that synthetic class. |
This PR adds a new Resolver API to get the binary class name for the owner of a function / property declaration. (resolver.getOwnerJvmClassName) For top level functions/properties; this value is necessary to be able to generate Java code. An alternative solution would be to synthesize KSClassDeclaration for these top level members but that carries over JVM limitations to KSP which is not great in the long term. This solution instead provides an API to unblock Java code generating KSP Processors without taking much of technical debt for KMP. Fixes: google#396 Test: topLevelMembers.kt
This PR adds a new Resolver API to get the binary class name for the owner of a function / property declaration. (resolver.getOwnerJvmClassName) For top level functions/properties; this value is necessary to be able to generate Java code. An alternative solution would be to synthesize KSClassDeclaration for these top level members but that carries over JVM limitations to KSP which is not great in the long term. This solution instead provides an API to unblock Java code generating KSP Processors without taking much of technical debt for KMP. Fixes: #396 Test: topLevelMembers.kt
This CL adds support for top level functions/properties in KSP. KSP does not generate synthetic class for files that include top level methods or properites. see: google/ksp#396 For XProcessing, i decided the follow through and not create a fake XTypeElement for them. Instead, this CL introduces a new XElement type called XMemberContainer. This interface defines two properties: className:ClassName and type:XType. ClassName is mandatory whereas XType is optional (to denote that it does not have an accessible type in KSP but ClassName can be accessed for code generation). This was enough to make the rest of the codebase happy. For Room, I also removed `XMethod/FieldElement.requireEnclosingTypeElement` as we didn't really need it once we have className. XMemberContainer might be extended to provide access to more declarations but we don't yet have a use case for that. By not modeling fake XTypeElements, we avoid creating a fake XType for it, which prevents lots of pitfalls since the type does not really exist in kotlin world. Bug: 160322705 Fixes: 184526463 Test: XRoundEnvTest, TopLevelMembersTest Change-Id: Iea60d2587c9373a96a56f7721d5ea2782b66ba2e
I'm trying to figure out how to model top level properties in kotlin, specifically the synthetic classes generated for them.
From my testing, it seems like KSP's behavior is a bit inconsistent between pre-compiled code and source core.
Moreover, it fails to resolve the type of a java field if the type of it is inferred from a kotlin file.
see the sample test below:
This prints:
Basically, when a Java class in source references a KSFile synthetic class, it works if it is coming from a compiled dependency whereas KSP fails to resolve it if it is in
source
.In both cases,
getClassDeclarationByName
fails to resolve the synthetic class.It is possible that not finding it in
getClassDeclarationByName
can be considered a feature, not a bug. On the other hand, KSP should still be able to resolve those types when java files in source references them.Moreover, supporting
getClassDeclarationByName
might also make sense to ease KAPT -> KSP transitions.cc: @gfreivasc, @elihart
The text was updated successfully, but these errors were encountered: