Feature suggestion: Ensure uniqness if Enum final fields

16 views
Skip to first unread message

Jens Teglhus Møller

unread,
Mar 1, 2026, 11:13:26 AM (2 days ago) Mar 1
to Project Lombok
Hi

I often create enum's with one or more final fields where its a requirement that the field values are unique.

I have created a unit test assertion that will help verify this contract, but I feel it would be nice to have the check at compile time.

So my proposal is to have a @Unique (or similar named) annotation that you can put on any non static final field in an enum and give a compile time error if same value is discovered in more than one enum constant.

Example that should compile fine:
@Getter
@RequiredArgsConstructor
public enum Example1 {
  A("a"),
  B("b"),
  ;

  @Unique
  final String key;
}

Example that should fail due to duplicate key value:

@Getter
@RequiredArgsConstructor
public enum Example2 {
  A("a"),
  B("b"),
  C("b"),
  ;

  @Unique
  final String key;
}

Is this outside the scope of lombok?

Best regards Jens 

Jens Teglhus Møller

unread,
Mar 1, 2026, 3:15:28 PM (2 days ago) Mar 1
to Project Lombok
So after sleeping on it, I can of see that this will only make sense for fields that are set with constant arguments (native type, strings etc) if you pass a non deterministic function in the constructor then it cannot be determined at compile time.

It seemed like a good idea when I thought about it, but I have no idea how complex it would be to implement for constant values. It would also need some very good error messages.

Best regards Jens
Reply all
Reply to author
Forward
0 new messages