11

I have the following class.

// Adding Lombok's @Data / @Value gives an error
public class Sample {

    String id;

    String name;

    Dummy[] entries;

    @JsonCreator
    // @Builder --> This gives same error
    private Sample(
            @JsonProperty("id") final String id,
            @JsonProperty("name") final String name,
            @JsonProperty("entries") @NonNull final Dummy[] entries) {
        this.id = id;
        this.name = name;
        this.entries = entries;
    }
}

If I add Lomobok annotations, I get the following error. We heavily rely on Lombok and I am trying to figure out how I can ensure I don't return a mutable object.

EI_EXPOSE_REP: May expose internal representation by returning reference to mutable object

EI_EXPOSE_REP2: May expose internal representation by incorporating reference to mutable object

As an alternative, I could use List instead of Array and rely on @Singular annotation. But I would like to know if there is a workaround for Array with Lombok's annoations.

4
  • 1
    Does this answer your question? FindBugs raises a bug called EI_EXPOSE_REP caused by Array
    – asbachb
    Commented Jul 9, 2021 at 7:02
  • 1
    Thanks for your comment! However it doesn't solve my issue as I am relying on Lombok for @Builder, Getter and Setter. So I am trying to find if this is even possible with Lombok to use an array?
    – SyncMaster
    Commented Jul 9, 2021 at 7:20
  • 1
    Afaik there's no possibility in lombok to configure to generate immutable arrays. You might try to implement the getter for that field on your own by using Arrays.copyOf. But I'm unsure this will help.
    – asbachb
    Commented Jul 9, 2021 at 7:45
  • 2
    just write it by hand.
    – Eugene
    Commented Jul 10, 2021 at 2:04

2 Answers 2

11

One way to solve this is to add some lombok config. The way to do this is in the root of the project (the same place your pom.xml/build.gradle is) add a file called:

lombok.config.

In this file add the following lines:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
lombok.extern.findbugs.addSuppressFBWarnings = true
lombok.anyConstructor.addConstructorProperties = true

More info here: https://projectlombok.org/features/configuration

4

It's an old and well known problem with methods generated by Lombok that return or accept mutable objects or structures. For example this issue about Date was submitted in 2015.
It is unlikely that Lombok will ever fix this because it's non-trivial. Your options :

  1. Disable particular FindBugs/SpotBugs inspection via build tool plugin or IDE configuration.
  2. Place @SuppressFBWarnings with relevant error code on violating class.
  3. Use onX Lombok feature to let it place @SuppressFBWarnings on violating methods(setters,getters etc.) like in snippet below.
@Data
@Setter(onMethod_ = @SuppressFBWarnings({"EI_EXPOSE_REP2","EI_EXPOSE_REP"}))
@Getter(onMethod_ = @SuppressFBWarnings({"EI_EXPOSE_REP2","EI_EXPOSE_REP"}))
public class TheClass {
    private MutableClassOrStructure classOrStructure;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.