1

I have a package name and a class name as Strings but I don't have the class specifically in my annotation processor. I need to use:

FunSpec.overriding(getOnlyElement(methodsIn(//stuck here)))

The stuck here should be a setOf ExecutableElements? How can I do this?

I've also checked here, but no so much luck.

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58

1 Answers1

2

First you need to obtain a TypeElement using the package and class name (you will need a ProcessingEnvironment instance for that).

fun getTypeElement(
        processingEnvironment: ProcessingEnvironment,
        packageName: String,
        className: String
): TypeElement {
    return processingEnvironment.elementUtils.getTypeElement("$packageName.$className")
}

Then you can simply access the enclosedElements in the TypeElement:

val typeElement = getTypeElement(processingEnv, packageName, className)

FunSpec.overriding(getOnlyElement(methodsIn(typeElement.enclosedElements)))
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • 1
    just a small note: that when using https://github.com/Takhion/kotlin-metadata there is no need to access the processenvironment at all just `elementUtils` directly for the win. this was the thing that was confusing me actually :) thnx – coroutineDispatcher Nov 22 '19 at 19:49