Is it possible to get Class<T> from KSClassDeclaration ?
See original GitHub issueHello, I have a question while using ksp. Is it possible to get Class<T> from KSClassDeclaration ? I want to know the Class<T> annotated with @CustomAnnotation.
For example, the following situation exists.
@Retention(AnnotationRetention.SOURCE)
annotation class CustomAnnotation
@CustomAnnotation
enum class MyEnum {
DEMO
}
I want to get Class<MyEnum> in CustomAnnotationVisitor.visitClassDeclaration.
class CustomAnnotationProcessor(
private val codeGenerator: CodeGenerator,
private val logger: KSPLogger,
): SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
val targetAnnotation = CustomAnnotation::class.java
val elements = resolver.getSymbolsWithAnnotation("${targetAnnotation.packageName}.${targetAnnotation.simpleName}")
val ret = elements.filter { !it.validate() }.toList()
elements
.filter { it is KSClassDeclaration && it.validate() }
.forEach {
it.accept(CustomAnnotationVisitor(), Unit)
}
return ret
}
inner class CustomAnnotationVisitor : KSVisitorVoid() {
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
// HERE!!!!!
}
}
}
I tried this but got an error.
class CustomAnnotationProcessor(
private val codeGenerator: CodeGenerator,
private val logger: KSPLogger,
): SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
val targetAnnotation = CustomAnnotation::class.java
val elements = resolver.getSymbolsWithAnnotation("${targetAnnotation.packageName}.${targetAnnotation.simpleName}")
val ret = elements.filter { !it.validate() }.toList()
elements
.filter { it is KSClassDeclaration && it.validate() }
.forEach {
it.accept(CustomAnnotationVisitor(), Unit)
}
return ret
}
inner class CustomAnnotationVisitor : KSVisitorVoid() {
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
Class.forName(classDeclaration.qualifiedName!!.asString())
}
}
}
thank you.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Retrieving Class Objects - The Reflection API
If the type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the...
Read more >The Class Declaration
The class declaration component declares the name of the class along with other attributes such as the class's superclass, and whether the class...
Read more >is it possible to declare a Java Class variable using getClass()?
No, what you are trying to do will not work. Variable types need to be declared with a known type at compile time....
Read more >Class declaration - cppreference.com
A class declaration can appear inside the body of a function, in which case it defines a local class. The name of such...
Read more >Classes - JavaScript - MDN Web Docs
One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For Kotlin poet users: This library has an Extention to convert
KSClassDeclaration
tocom.squareup.kotlinpoet.ClassName
. For example for classDeclaration with classKind of annotation you can convert KSClassDeclaration to com.squareup.kotlinpoet.ClassName and use it to add as annotation:@neetopia thank you. I’ll check.