Kotlin doesn't see Java Lombok accessors?

前端 未结 4 1683
野性不改
野性不改 2020-12-02 21:48

Using Kotlin 1.0.0 release (compiling in IntelliJ 15).

println(myPojoInstance.foo)

When it tries to compile code (in IntelliJ or Gradle) th

相关标签:
4条回答
  • 2020-12-02 22:28

    As it was mentioned in comments above, delombok helps. In case of maven build it would be:

    <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>
                    <formatPreferences>
                        <javaLangAsFQN>skip</javaLangAsFQN>
                    </formatPreferences>
                    <verbose>true</verbose>
                </configuration>
            </execution>
            <execution>
                <id>test-delombok</id>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>testDelombok</goal>
                </goals>
                <configuration>
                    <verbose>true</verbose>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-12-02 22:39

    To add to Sergey Mashkov's response (adding here I don't have enough rep points to comment on it), here's an example app of a Gradle multi-project setup where Kotlin can see the Lombok-generated code (without kapt or delomboking. Caveats do apply - namely, Kotlin can call the Java code, but Java can't call the Kotlin code in that particular module (as this would create a circular dependency). This kind of build might be suitable if you have an existing Java codebase and all new code is written in Kotlin, though.

    I would love to see full Lombok/Kotlin support, however. While Kotlin is fully interoperable with Java, the reality is that Lombok is very widely used, and this issue may prevent a large number of developers who would like to switch to Kotlin from doing so.

    0 讨论(0)
  • 2020-12-02 22:47

    Generally, no, it doesn't. The reason of that behavior is that Lombok is an annotation processor for javac but when the kotlin compiler runs it uses javac as well but with no annotation processing so this is why kotlin don't see declarations that wasn't yet generated.

    The only workaround for now is to define strict compilation order: Java first and after that kotlin. Unfortunately this approach has great disadvantage: you can't use Kotlin code from Java in this case. To workaround it you may need multimodule project that may cause a lot of pain

    0 讨论(0)
  • 2020-12-02 22:50

    Looks like it works if you use delombok according to site and add the target/generated-sources/delombok folder in the pom.xml under build > plugins > plugin > kotlin-maven-plugin

    0 讨论(0)
提交回复
热议问题