question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to get Class<T> from KSClassDeclaration ?

See original GitHub issue

Hello, 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:closed
  • Created 2 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
DHosseinycommented, May 16, 2022

For Kotlin poet users: This library has an Extention to convert KSClassDeclaration to com.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:

val classDeclaration: KSClassDeclaration
TypeSpec.enumBuilder(enumClassName)
            .addAnnotation(
                classDeclaration.toClassName()
            )
            .build()
0reactions
sangyongchoicommented, May 23, 2022

@neetopia thank you. I’ll check.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found