gradle-retrolambda
gradle-retrolambda copied to clipboard
lombok.val within lambda breaks the build
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.
Just a sanity check, are you using a version of lombok that supports java 8?
Yes, Project Lombok v1.16.6.
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
This doesn't happen always. Some of our projects are successfully building while others are not.
"org.projectlombok:lombok:1.16.6" Java 8
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.
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);
Same issue with lombok 1.16.6 + open jdk 1.8.0_45.
This issue still exists with 1.16.10 and openjdk 1.8.0_102
Seems fixed with Lombok 1.16.12 and RetroLambda 3.4.0 (with Oracle Java 1.8.0_121). Can anyone else confirm?
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 Which version of RetroLambda do you use?
I'm not using RetroLambda. I have just a gradle project having compileOnly "org.projectlombok:lombok:1.16.16"
in dependencies.
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.
Sorry! I was thinking that I was in the lombok repo.