gradle-retrolambda icon indicating copy to clipboard operation
gradle-retrolambda copied to clipboard

lombok.val within lambda breaks the build

Open DmitriyZaitsev opened this issue 9 years ago • 14 comments

I have a local value status in my method that relies on type that will be inferred from the initializer expression (lombok's val):

private MyCallBack<Result> foo(final Bar bar) {
    return new MyCallBack<Result>() { // <----------------------- anonymous class
        @Override public void apply(final Result result) {
            val status = result.getStatus(); // <----------------------- OK
            switch (status) {
                case Result.SUCCESS:
    ...
    }
}

This code compiles and runs good, but when I replace this with lambda:

private MyCallBack<Result> foo(final Bar bar) {
    return result -> { // <----------------------- lambda
        val status = result.getStatus(); // <----------------------- ERROR
          switch (status) {
            case Result.SUCCESS:
    ...
    }
}

it even doesn't compile at all. Console output:

:app:compileDebugJavaWithJavac
/path/to/my/activity/MainActivity.java:187: error: incompatible types: int cannot be converted to val
      val status = result.getStatus();
                                   ^
/path/to/my/activity/MainActivity.java:190: error: incompatible types: val cannot be converted to int
      switch (status) {
             ^

If I replace val with int, it works again.

DmitriyZaitsev avatar Oct 04 '15 21:10 DmitriyZaitsev

Just a sanity check, are you using a version of lombok that supports java 8?

evant avatar Oct 05 '15 00:10 evant

Yes, Project Lombok v1.16.6.

DmitriyZaitsev avatar Oct 06 '15 08:10 DmitriyZaitsev

We have the same issue here. Eclipse can compile it but maven can't:

LombokValTest.java:[31,36] incompatible types: java.lang.String cannot be converted to lombok.val
public class LombokValTest {

    public static void main(String[] args) {
        new LombokValTest().test();
    }

    public void test() {
        val data = new ArrayList<>(); // works
        data.add("test1");
        data.add("test2");

        data.forEach(input -> {
            val newValue = "VAL: " + input; // doesn't work (line 31)
            System.out.println(newValue);
        });
    }
}

Using lombok 1.16.6:

mvn dependency:tree -Dverbose |grep lombok
[INFO] +- org.projectlombok:lombok:jar:1.16.6:provided

bramklg avatar Jan 05 '16 15:01 bramklg

This doesn't happen always. Some of our projects are successfully building while others are not.

"org.projectlombok:lombok:1.16.6" Java 8

ghost avatar Jan 16 '16 18:01 ghost

Lombok does not support lambdas. It seems the build is ordered in a way that Lombok is run before RetroLambda. I am trying to figure out how to change the order.

NyanSten avatar Feb 26 '16 17:02 NyanSten

this also doesn't work (latest lombok + java 8): val amount = BigDecimal.valueOf(ingerValue).multiply(factor); // factor is BigDecimal

switched on vanilla java for now: final BigDecimal amount = BigDecimal.valueOf(ingerValue).multiply(factor);

daggerok avatar Apr 04 '16 13:04 daggerok

Same issue with lombok 1.16.6 + open jdk 1.8.0_45.

tarasevich avatar Jul 12 '16 13:07 tarasevich

This issue still exists with 1.16.10 and openjdk 1.8.0_102

blub0hr avatar Dec 23 '16 15:12 blub0hr

Seems fixed with Lombok 1.16.12 and RetroLambda 3.4.0 (with Oracle Java 1.8.0_121). Can anyone else confirm?

NyanSten avatar Jan 26 '17 20:01 NyanSten

I'm also having this issue using Lombok 1.16.16 and Java 1.8.0_131.

error: incompatible types: String cannot be converted to val
                val decoded = new String(decodeBase64(token));

cazacugmihai avatar May 11 '17 17:05 cazacugmihai

@cazacugmihai Which version of RetroLambda do you use?

NyanSten avatar May 11 '17 18:05 NyanSten

I'm not using RetroLambda. I have just a gradle project having compileOnly "org.projectlombok:lombok:1.16.16" in dependencies.

cazacugmihai avatar May 11 '17 22:05 cazacugmihai

This is an issue for RetroLambda that it did not convert lambdas to methods before Lombok was run. For the issue in Lombok, you need to report it to them.

NyanSten avatar May 12 '17 12:05 NyanSten

Sorry! I was thinking that I was in the lombok repo.

cazacugmihai avatar May 12 '17 13:05 cazacugmihai