How to get hold of KotlinCompiler?

val compiler = ToolProvider.getSystemJavaCompiler()
val diagnostics = DiagnosticCollector<JavaFileObject>()
val fileManager: StandardJavaFileManager = compiler.getStandardFileManager(diagnostics, null, null)
val compilationUnits = mutableListOf<JavaFileObject>()
val task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits)

Here, I can only process JAVA source files. The moment I add a `.kt` file in the mix, I get the below error.

java.lang.IllegalArgumentException: Compilation unit is not of SOURCE kind:

So how do I go about with Kotlin files. How do I handle the above case for Kotlin files?

Comment actions Permalink

What do you want to achieve? in what context is this code running?

Comment actions Permalink

I want to run an Annotation Processor from my plugin. So I am using:


Here `ComponentProcessor` is a 3rd party annotation processor, that I am using for "Validation". It doesn't generate code, when we invoke it with "ValidateGraph". It does validation by means of this. I am using the result of this validation.

Everything works fine for `.java` but I want to run it against `.kt` as well.

`compile.getTask()` gives the option to add either the Source Files (compilationUnits) or Class names (classes). Right now, I am using Class names since it handles both Java and Kotlin. But I wanted to know how to get access to KotlinCompiler, the way we can get access to JavaCompiler. 


Please sign in to leave a comment.