Create a TypeSpec from an existing file with kotlinpoet

233 views Asked by At

I need to make a copy of the class marked with my annotation, and then add my own code to it.

Class marked with my annotation:

@MyAnnotation
class Test {
    private name = "Ann"

    fun method1() {
        println("Test")
    }
}

Generated class:

class Test {
    private name = "Ann"

    fun method1() {
        println("Test")
    }

    fun method2() {
        println("This method was generated")
    }
}

So far, I see only one way: go through all the fields, methods, etc. in an existing class and add them to TypeSpec. ClassBuilder(), but this is too difficult, I think.

Is there an easier way, maybe a library?

0

There are 0 answers