Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] val keyword delombok result breaks with Java 7 compatibility #3025

Open
awhitford opened this issue Oct 31, 2021 · 4 comments · May be fixed by #3340
Open

[BUG] val keyword delombok result breaks with Java 7 compatibility #3025

awhitford opened this issue Oct 31, 2021 · 4 comments · May be fixed by #3340

Comments

@awhitford
Copy link
Contributor

This code after delombok builds fine with 1.18.20, but breaks with 1.18.22:

package org.projectlombok.test;

import lombok.val;
import org.junit.Assert;
import org.junit.Test;

public class DataExampleTest {
    @Test
    public void testDataExample() {
        val name = "MyData";
        val de = new DataExample(name);
        Assert.assertEquals(de.getName(), name);
    }
}

I have source & target set to 1.7 for the compiler plugin, but my Javac is 17. The delombok code looks like:

// Generated by delombok at Sun Oct 31 06:58:48 PDT 2021
package org.projectlombok.test;

import org.junit.Assert;
import org.junit.Test;

public class DataExampleTest {
    @Test
    public void testDataExample() {
        final var name = "MyData";
        final var de = new DataExample(name);
        Assert.assertEquals(de.getName(), name);
    }
}

The error is:

[ERROR] /Users/anthony/github/awhitford/lombok.maven/test-maven-lombok/target/generated-test-sources/delombok/org/projectlombok/test/DataExampleTest.java:[10,15] cannot find symbol
[ERROR]   symbol:   class var
[ERROR]   location: class org.projectlombok.test.DataExampleTest
[ERROR] /Users/anthony/github/awhitford/lombok.maven/test-maven-lombok/target/generated-test-sources/delombok/org/projectlombok/test/DataExampleTest.java:[11,15] cannot find symbol
[ERROR]   symbol:   class var
[ERROR]   location: class org.projectlombok.test.DataExampleTest

Previously the output would be:

// Generated by delombok at Fri Apr 02 06:02:09 UTC 2021
package org.projectlombok.test;

import org.junit.Assert;
import org.junit.Test;

public class DataExampleTest {
    @Test
    public void testDataExample() {
        final java.lang.String name = "MyData";
        final org.projectlombok.test.DataExample de = new DataExample(name);
        Assert.assertEquals(de.getName(), name);
    }
}

Based on the change log, I thought that the val conversion to final var was limited to when the compiler source option is 10 or higher.

Version info (please complete the following information):

  • Lombok version 1.18.22
  • Platform: OpenJDK 17
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/Cellar/maven/3.8.3/libexec
Java version: 17, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.6", arch: "x86_64", family: "mac"
@awhitford
Copy link
Contributor Author

This issue is now more than 3 months old, and I've been holding off on upgrading the lombok-maven-plugin as a result because this broke my unit test. By the 👍 from @rspilker, I'm left to believe that this is a legitimate issue, but there seems to be no traction on resolving it.

Will this be resolved?

@Rawi01
Copy link
Collaborator

Rawi01 commented Feb 20, 2022

I think setting the source version never worked like this for delombok. It does not use the normal compilation, it starts its own compiler task and only pass a subset of compiler flags. Unfortunately there is no way to set the compiler version at the moment.

A normal compilation and lombok tests work as expected.

@rzwitserloot
Copy link
Collaborator

I don't really see what we can meaningfully look at. Perhaps the version of the java runtime that delombok is running on top of, but that's merely a pretty good guess, not a surefire thing - perhaps a decent default, but it needs a setting even then.

It also doesn't feel like a formatting option (java -jar lombok.jar delombok --format-help).

Perhaps delombok should gain a --source option; perhaps in the future we'll make more features change what they generate based on the language features available.

@Rawi01
Copy link
Collaborator

Rawi01 commented Feb 21, 2022

We already have parameters for some compiler arguments, how do we decide which one will be added as delombok parameter?

@awhitford
Delombok stores the Context, you can use reflection to edit it and inject the source version.

Class<?> contextClass = Class.forName("com.sun.tools.javac.util.Context");
Field contextField = delombokClass.getDeclaredField("context");
contextField.setAccessible(true);
Object context = contextField.get(this.delombokInstance);

Class<?> optionsClass = Class.forName("com.sun.tools.javac.util.Options");
Method optionsInstance = optionsClass.getDeclaredMethod("instance", contextClass);
Object options = optionsInstance.invoke(null, context);

Method optionsPut = optionsClass.getDeclaredMethod("put", String.class, String.class);
optionsPut.invoke(options, "-source", "1.6");

@tamasvajk tamasvajk linked a pull request Feb 1, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants