projectlombok / lombok

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

[BUG] Custom nullable annotations are not copied to constructors and methods for Value/Data classes #3318

Open zeldigas opened 1 year ago

zeldigas commented 1 year ago

Describe the bug In my project and libs I use custom nullability annotations that are almost one-to-one copies of spring ones, the purpose is to avoid depending on spring-core. I wanted to configure lombok to add them from fields to constructors and methods (getters/setters) for Value/Data annotated classes, but for some reason annotations are not added.

At the same time, when lombok is configured to use spring or checkerframework annotations they are added correctly.

In other words, sample class

@Value
public class Sample {
    @Nullable
    String prop1;
    String prop2;
}

Produce different results depending on Nullable annotation.

For spring with (lombok.addLombokGeneratedAnnotation=true, lombok.addNullAnnotations=spring)

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

public final class Sample {
    @Nullable
    private final String prop1;
    private final String prop2;

    @Generated
    public Sample(@Nullable String prop1, String prop2) {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }

    @Nullable
    @Generated
    public String getProp1() {
        return this.prop1;
    }

For custom (lombok.addLombokGeneratedAnnotation=true, lombok.addNullAnnotations=CUSTOM:com.example.annotations.NonNull:com.example.annotations.Nullable)

import com.example.annotations.NonNull;
import com.example.annotations.Nullable;
import lombok.Generated;

public final class Sample {
    @Nullable
    private final String prop1;
    private final String prop2;

    @Generated
    public Sample(String prop1, String prop2) {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }

    @Generated
    public String getProp1() {
        return this.prop1;
    }

To Reproduce I've prepared a sample project that shows the problem: https://github.com/zeldigas/lombok-custom-nullable-problem. In spring module you can find usage of spring annotations and in custom - custom ones that are defined in this module as well (for simplicity)

Expected behavior Both modules in sample project are working the same way.

Version info (please complete the following information):

zeldigas commented 1 year ago

I suppose the reason is because copyable, nullable and non-null annotations are mostly hardcoded: https://github.com/projectlombok/lombok/blob/1473389f397579a224d57b496f6387f3b8cbb6a9/src/core/lombok/core/handlers/HandlerUtil.java#L130. :(

burdoto commented 9 months ago

This also applies to annotations added to the configuration explicitly like

lombok.copyableannotations += org.intellij.lang.annotations.Language

The generated constructor does not have this annotation present; source: image

compiled output: image

rzwitserloot commented 9 months ago

A contribution of a simple test case (it's just a matter of writing self-contained java files along with what you think lombok should do with em and putting them into the right dirs (test/transform/resource/before and test/transform/resource/after-javac and/or test/transform/resource/after-ecj).

burdoto commented 9 months ago

Sorry @rzwitserloot , I don't think I quite understand what you're asking of me to contribute or what's unclear, would you please elaborate?

I was expecting lombok to copy field annotations to the respective constructor parameters, not only for Nullability annotations but favorably also for copyable annotations.

rzwitserloot commented 9 months ago

Lombok has a copy system and there are a ton of places where annotations may or may not be copied. This isn't exactly a high traffic bug report and we have various tests about copying annotations that all pass.

So, exactly, which annotations are and aren't being copied?

You have posted a sample project - it sounds like all you'd really have to do is take the output of java -jar lombok.jar delombok TheOneFromSpringThatDoesWork.java, 'fix' the annotations to your custom ones, and contribute a PR that just has TheOneThatDoesNotWorkWithYourCustomAnnos.java in test/transform/resources/before, and the output of delombok from the spring example, adapted to be like what you'd have expected from your custom take to the after-delombok dir (which you do not currently get, hence, failing the test case). You can stick lombok.config primitives in a comment in the before source file (check other files that do this, just scan for .java files in before that start with a comment, various ones do this).