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] Custom nullable annotations are not copied to constructors and methods for Value/Data classes #3318

Open
zeldigas opened this issue Dec 27, 2022 · 5 comments · May be fixed by #3589
Open

Comments

@zeldigas
Copy link

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):

  • Lombok version - 1.18.24
  • Platform - javac temurin-11.0.17 on Windows 10
@zeldigas
Copy link
Author

I suppose the reason is because copyable, nullable and non-null annotations are mostly hardcoded:

BASE_COPYABLE_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(new String[] {
. :(

@burdoto
Copy link

burdoto commented Jan 11, 2024

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
Copy link
Collaborator

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
Copy link

burdoto commented Jan 14, 2024

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
Copy link
Collaborator

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).

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