kotlinannotation-processingkotlinpoet

Kotlin poet filet not getting generated


I tried to create a class with annotation processor and Kotlin Poet. This is my code:

@AutoService(Processor::class)
class TailProcessor : AbstractProcessor() {

    override fun process(elementTypeSet: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean {
        roundEnvironment?.getElementsAnnotatedWith(Tail::class.java)?.forEach {
            if (it.javaClass.kotlin.isData) {
                print("You are doing it right")
                val className = it.simpleName.toString()
                val pack = processingEnv.elementUtils.getPackageOf(it).toString()
                val variables = ElementFilter.fieldsIn(elementTypeSet)
                startClassGeneration(className, pack, variables)
            } else {
                return false
            }
        }
        return false
    }

    override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latest()

    override fun getSupportedAnnotationTypes(): MutableSet<String> = mutableSetOf(Tail::class.java.name)

    private fun startClassGeneration(
        className: String,
        pack: String,
        variables: MutableSet<VariableElement>
    ) {
        val fileName = "Tail$className"
        val stringToBePrinted = generateStringFromIncommingValues(variables)
        val printFunction = FunSpec.builder("print").addCode("print($stringToBePrinted)").build()
        val generatedClass = TypeSpec.objectBuilder(fileName).addFunction(printFunction).build()
        val file = FileSpec.builder(pack, fileName).addType(generatedClass).build()
        val kaptKotlinGeneratedDir = processingEnv.options[KOTLIN_DIRECTORY_NAME]
        file.writeTo(File(kaptKotlinGeneratedDir, "$fileName.kt"))
    }

    private fun generateStringFromIncommingValues(variables: MutableSet<VariableElement>): Any {
        val stringBuilder = StringBuilder()
        variables.forEach {
            if (it.constantValue == null) {
                stringBuilder.append("null\n ")
            } else {
                stringBuilder.append("${it.constantValue}\n")
            }
        }
        return stringBuilder.toString()
    }

    companion object {
        const val KOTLIN_DIRECTORY_NAME = "sxhardha.tail"
    }
}

The problem, directory and file not generating. I tried to rebuild, invalidate cache + restart, clean but none of them works. The build goes successful without any errors but I see no changes. Can you check what is wrong?


Solution

  • I actually found the issue. I wasn't checking right if that class is a data class or not and the condition was never met.

    Instead of:

    it.javaClass.kotlin.isData
    

    Should have been:

    it.kotlinMetadata as KotlinClassMetadata).data.classProto.isDataClass
    

    But it can only be achieved by using this library here.