Kapt and generating kotlin (rather than java)

Hi,
I am using kapt with maven and a java annotation processor and that is good.

However, ideally instead of the annotation processor generating Java source I’d like it to generate Kotlin (to make use of Kotlin properties rather than java methods). To play around with this I’ve done a little hack to modify my annotation processor to generate Kotlin code rather than Java and it “sort of works”.

That is:

  • The Java processingContext.createWriter() … creates a .java file
  • My annotation processor generates Kotlin source into that .java file
  • I rename the .java file to be .kt after the source has been written

… yes this is a bit hacky but worth a try right.

In the IDE is seems to be all fine - things all compile etc. However, in maven command like I see the kapt compile reporting errors - the xx.java file can’t be found which sounds reasonable given I renamed the file to be .kt.

[INFO] --- kotlin-maven-plugin:1.1.2:kapt (kapt) @ example-kotlin ---
[INFO] Kotlin Compiler version 1.1.2
[INFO] Compiling Kotlin sources from [/home/rob/github/ebean-dir/examples/example-kotlin/src/main/java]
[INFO] Module name is example-kotlin

... I log that I am rename the generated file from .java to .kt 

[INFO] Note: rename from file:/.../example-kotlin/target/generated-sources/kapt/compile/org/example/domain/query/QOrder.java partial:QOrder to file /.../examples/example-kotlin/target/generated-sources/kapt/compile/org/example/domain/query/QOrder.kt

I see the error:

[ERROR] error: error reading /.../examples/example-kotlin/target/generated-sources/kapt/compile/org/example/domain/query/QOrder.java; /.../examples/example-kotlin/target/generated-sources/kapt/compile/org/example/domain/query/QOrder.java (No such file or directory)

… which is reasonable given I renamed the generated file to be .kt

A this point I can run in the IDE fine - build all compiles fine, run tests using the generated kotlin code etc.

I’d really like to be able to generate Kotlin code with my annotation processor rather than Java code. The generated code in this case are “query beans” for an ORM and it is much nicer to use Kotlin property support compared to java methods in this case (much much nicer, that is another story).

Are there any plans for supporting kapt with generated Kotlin code?

If kapt could look for .kt generated files as well as .java it looks like it would just work?

Thanks, Rob.

So I have seen this very interesting video from Andrey The Future of Kotlin: How agile can language development be? — Andrey Breslav — MiXiT 2017 on Vimeo … which has a little point regarding annotation processing being potentially superseded by compiler/ide plugins.

Effectively my question / query is … is there a plan for “annotation processing” that supports generating Kotlin source code (rather than Java source code) and that video kind of suggests the plan might be “write a compiler / ide plugin”. I feel that isn’t quite accurate though as I felt Andrey was more talking about macro type extensions rather than “source code generation”.

Any thoughts?

The generated query beans allows building fluent orm queries with auto-completion and compile time checking of paths, expressions, paths and argument types like:

val customers = Customer
    .where()
      .name.istartsWith("Rob")
      .billingAddress.country.name.startsWith("New Zea")
      .findList()

These query beans can be generated as Java but it is nicer if they are instead Kotlin (with property support).

I have logged this as an enhancement request - https://youtrack.jetbrains.com/issue/KT-17883

In case people are following this - yes, absolutely this can be done.

Basic example at:
Kotlin annotation-processor-example

The annotation processing I’m working on at:
Ebean ORM - kotlin-querybean-generator

I added a simple annotation processor that generates Kotlin code to kotlin-examples: https://github.com/JetBrains/kotlin-examples/tree/master/gradle/kotlin-code-generation

3 Likes

@yanex Thank you for you example, unfortunately it doesn’t work with Kotlin 1.1.3, I’ve filed this bug: https://youtrack.jetbrains.com/issue/KT-18758

Does anyone know how to speed up kapt? The kaptGenerateStubsKotlin gradle task takes about 30 seconds on each build (latest Kotlin, latest Gradle).

Same problem here than @fab1an kapt kotlin is taking almost 30 secs :disappointed_relieved:

tried what is described here but no luck https://youtrack.jetbrains.com/issue/KT-17523#tab=Comments

I’m having the exact same issue (Kotlin 1.1.50, Grale 4.2). Haven’t been able to find any performance optimizations. I’m running Windows 10.