projectlombok / lombok

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

[FEATURE] Replace `lombok.val` with `final var` in source code #3207

Closed timtebeek closed 10 months ago

timtebeek commented 2 years ago

Describe the feature As discussed here, it would be nice to either have a new command valToVar or mode of delombok to replace existing usage of lombok.val with final var in the source code. This is intended to move away from lombok.val specifically, leaving all other code, comments and lombok features intact.

public String example() {
-   val example = new ArrayList<String>();
+   final var example = new ArrayList<String>();
    example.add("Hello, World!");
-   val foo = example.get(0);
+   final var foo = example.get(0);
    return foo.toLowerCase();
}

Describe the target audience Anyone using Java 10/11+, and looking to move away from lombok.val specifically, towards final var, while leaving all other usage of lombok features intact.

Additional context lombok.val was added to reduce boilerplate, and up to Java 10 that was indeed true. With the arrival of final var there is less of a need to use lombok.val, and it can be troublesome for other tools to work with.

To keep things simple support could be limited to Java 11+; that way there's no need to replace val with the actual type on Java 8.

At present delombok produces the code to a separate target folder; instructions can be provided to move the altered files back into the source folder to complete the phase out of lombok.val. Even better if replacement could be done in place, but that's optional.

timtebeek commented 2 years ago

@rzwitserloot from an end user perspective this has been covered through a new OpenRewrite recipe. The documentation is not yet up, but is it's now possible to phase out lombok.val with their Maven or Gradle plugin.

Question then becomes what you want to do with the issue on this project. You can for instance refer to the implementation at OpenRewrite from the lombok documentation, or keep this issue open if you still plan to implement this yourselves. Either approach could open the door to eventual deprecation (with optional removal), if that's the way you want to go.

timtebeek commented 10 months ago

Unlikely to be picked up here; users interested in this change can execute this recipe: https://docs.openrewrite.org/recipes/java/migrate/lombok/lombokvaltofinalvar

Closing issue.