projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.92k stars 2.4k forks source link

Kotlin Support #1169

Open aikar opened 8 years ago

aikar commented 8 years ago

Hello, I'm wanting to use Kotlin in a project that uses Lombok with Java.

However, while the IDE shows code as valid, the Kotlin compiler can not see Lombok generated code.

This makes mixing Java+Lombok with Kotlin impossible.

Would it be possible to also apply the Lombok processing to the Kotlin compiler so we can have harmony?

dodgex commented 7 years ago

For my Android App I'm currently playing with Kotlin and was able to let Kotlin see Lombok generated getters by adding the lombok processor to kapt.

provided "org.projectlombok:lombok:1.16.14"
annotationProcessor "org.projectlombok:lombok:1.16.14"
kapt "org.projectlombok:lombok:1.16.14"

This is from a gradle build but should be also possible with maven. :)

vojkny commented 7 years ago

How can this be done via Maven? Also how can I use kapt in IDE?

vojkny commented 7 years ago

I tried using kapt with lombok annotation processor as follows:

           <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>kapt</id>
                        <goals>
                            <goal>kapt</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/main/java</sourceDir>
                            </sourceDirs>
                            <annotationProcessors>
                                <annotationProcessor>lombok.core.AnnotationProcessor</annotationProcessor>
                            </annotationProcessors>
                        </configuration>
                    </execution>
    ...

With no effect.

ubaierbhat commented 7 years ago

I have the same problem. We are using Lombok in our Android apps and it looks like we can't use it along side with kotlin. @dodgex suggestion did not work for me.

taejungkim commented 7 years ago

@dodgex I added kapt plugin with kapt "org.projectlombok:lombok:1.16.14" . But impossible access to getter ( generated by lombok )

Help me!

faizanahemad commented 7 years ago

Any solution found to this? I have the same issue. We use Intellij with Java and lombok. and build with maven.

jtonic commented 7 years ago

Hi,

The only solution I found quite satisfactory was to move the lombok annotated java file in a separate maven module. But in our case lombok annotations were only used for DTOs (model), so it did make sense to approach this way.

Kind regards

Tony

rojiani commented 6 years ago

For anyone looking for a solution, I've created a demo app illustrating the separate module idea mentioned by @jtonic (except using Gradle rather than Maven).

mouyang commented 6 years ago

@dodgex do you have a working solution or a minimal proof-of-concept that you are able to post? All of the comments here suggest that this does not work.

dodgex commented 6 years ago

Sorry but currently I don't have a project using lombok & kotlin.

mouyang commented 6 years ago

Hmmm. I'm probably not understanding the issue but it seems like this is possible as of Kotlin 1.2.21. All I did was blindly follow the instructions on the Kotlin Web site ... https://kotlinlang.org/docs/reference/using-maven.html#compiling-kotlin-and-java-sources https://kotlinlang.org/docs/reference/using-gradle.html

and I was able to see Lombok-generated methods in Kotlin without creating any separate modules ... https://github.com/mouyang/kotlin-lombok/tree/master/gradle https://github.com/mouyang/kotlin-lombok/tree/master/maven

Again, I'm probably misunderstanding the issue completely so any clarification would be appreciated.

jtonic commented 6 years ago

Hi,

I am not a specialist but I am trying to shed a light on the problem.

  1. lombok is not a pure APT library, because it also uses javac to generate the bytecode. AFAIK APT generates java sources, but lombok generates bytecode.
  2. kotlin supports joint compilation in the sense that it can compile both java and kotlin files (with two-way dependencies between them) in the same module .
  3. kotlin also support the ATP through KAPT kotlin plugin. This allows the joint compiler to see also the ATP generated java files by the kotlin ones.

There are some popular ATP libraries to generate useful java sources (related to hibernate, spring boot properties, java bean mappers - MapStruct) and there is no problem with them in java/kotlin module, thanks to kapt. But on the other hand because lombok modifies the bytecode during the compilation kapt can do nothing about it.

This is the problem I came across and if, I remember well, there was a thread, I suppose on stackoverflow, where the kotlin language architect told about this issue, explaining things more in depth.

Kind regards.

mouyang commented 6 years ago

@jtonic, thank you. Your explanation makes a lot of sense.

@dodgex If you have time, can you take a look at a sample branch I have? It's an minimal unsuccessful attempt to use Lombok in Kotlin, and I was wondering what you did to make it work. I did not use "annotationProcessor" since I'm not working on Android, but I don't know if that actually matters or not. https://github.com/mouyang/kotlin-lombok/tree/broken-gradle-kapt

jtonic commented 6 years ago

@mouyang I looked a bit over your example (gradle based one) and I have some recommendations.

  1. I urge you not to use lombok on kotlin classes. Practically you have all the supports lombok have in the kotlin syntax/api.
  2. I would use lombok only for DTOs or model classes. I saw lombok-ized spring services only to include @Slf4j. It could bring a lot of issues
  3. if you lombok-ize only the (java) model then it makes sense to extract it into a new gradle subproject and and all is fine. And latter on incrementally migrate from java lombok-ized model to kotlin one. Of course in this new gradle model sub-project you cannot have kotlin classes referencing java lombok-ized ones.

Note: I remember I read on some stack overflow thread another approach - smth like put some generated lomboked classes in a gen folder and then refer it in the kotlin compilation one. This could work but for me it was an additional burden on the build tool.

I really hope all these help you figured out how to approach the kotlin/java/lombok issue.

ericytsang commented 6 years ago

I've been trying to look for a solution for this problem to try to get my ancient team to migrate to a new language. we're so ancient that the leads complain about using java8's lambdas. i ended up making a pom.xml that delomboks the code in src/main/java then compiles the delomboked code with the kotlin plugin:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ericytsang</groupId>
    <artifactId>playground</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>com.ericytsang playground</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.2.50</kotlin.version>
        <junit.version>4.12</junit.version>
        <lombok.version>1.16.8</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
                <version>${lombok.version}.0</version>
                <executions>
                    <execution>
                        <id>delombok</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>src/main/java</sourceDirectory>
                            <outputDirectory>${project.build.directory}/delombok-main</outputDirectory>
                            <addOutputDirectory>false</addOutputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>delombok-test</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>src/test/java</sourceDirectory>
                            <outputDirectory>${project.build.directory}/delombok-test</outputDirectory>
                            <addOutputDirectory>false</addOutputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>${project.build.directory}/delombok-main</sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>${project.build.directory}/delombok-test</sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

i usually use gradle..im not too familiar with maven. improvements are welcome. i don't think it supports incremental compilation...i also imagine that having to modify every plugin originally using src/main/java to now use ${project.build.directory}/delombok-main would be a pain. debugging also becomes odd since the compiled code is the delomboked code instead of the code in src/main/java.

https://stackoverflow.com/a/50914110/2898715

DRSchlaubi commented 3 years ago

Seems like Kotlin themselves is working on a Lombok integration: https://github.com/JetBrains/kotlin/tree/master/plugins/lombok/lombok-compiler-plugin

Rawi01 commented 3 years ago

That's fascinating, I had a quick look at the code and it seems like it adds some kind of method stubs to make them visible for Kotlin. If I have not missed anything that requires a basic reimplementation of all lombok handlers, seems to be a bunch of work :smile:

I also tried to understand the Kotlin compilation process and I think it still invokes javac to compile the Java files. If it would be possible to run an annotation processor at that moment lombok should work. IIRC I did something similar some time ago for an android project: https://github.com/projectlombok/lombok/issues/2412#issuecomment-662685863

zveznicht commented 3 years ago

kotlin+java projects are compiled like this:

  1. First kotlinc compiles kotlin code. It parses all java sources to resolve all links to it. And generates class files for kotlin files.
  2. After javac compiles all java sources, compiled kotlin classes are passed in classpath.

So to support lombok you need to provide all symbols lombok generates to kotlin on (1). Thats what new plugin does. You may call it stubs, they are basically declarations of parts lombok will generate later. During (2) you simply enable lombok as annotation processor and it generates everything as usual.

I also tried to understand the Kotlin compilation process and I think it still invokes javac to compile the Java files. If it would be possible to run an annotation processor at that moment lombok should work

it will work as long as you don't refer any lombok-generated symbols from kotlin code.

Rawi01 commented 3 years ago

@zveznicht Thanks for the additional details. Does kotlinc uses its own parser or does it use javac to generate the symbols/stubs? If it is the former the plugin is the only solution but if it is the latter it might be possible to invoke lombok.

zveznicht commented 3 years ago

Thanks for the additional details. Does kotlinc uses its own parser or does it use javac to generate the symbols/stubs

kotlinc uses its own parser

CharlyLafon37 commented 3 years ago

Check https://kotlinlang.org/docs/lombok.html. Seems to work for me.

rzwitserloot commented 2 years ago

We're still seeing some issue traffic on this. Does the kotlin plugin for lombok as linked by @CharlyLafon37 above work as expected? To be clear, this is what I expect, and this is what we will (attempt to) fix:

What we won't fix is:

rzwitserloot commented 2 years ago

Assuming that plugin works, can somebody tell me what happens if that plugin isn't around? Does lombok just silently not run, or is it more like #2428 describes: That lombok does run as annotation processor, fails to detect environment, says so in an error log, and throws in the towel?

Assuming the kotlinlang plugin works, we need to update our docs to refer to it. If lombok does run but fails to detect environment, we should make it detect environment and adjust our error message to refer to the plugin.

YASMINCOS commented 1 month ago

alguma atualização? estou tendo esse problema