Custom prefixed getters cannot ... what, set the prefix?

36 views
Skip to first unread message

Morten Sabroe Mortensen

unread,
Jul 20, 2025, 4:01:28 PMJul 20
to Project Lombok
Getters with custom prefixes are too conservative!

We can declare an interface like this:

public interface Trait {
    boolean isRegular();
}

We can implement this too:

@Getter
@lombok.Builder(builderClassName="Builder")
@AllArgsConstructor(staticName="of")
final class SimpleTrait implements Trait {
    private final boolean regular;
}

This is GREAT, because we get the interface signature satisfied, implemented! 

Now, I may have 15x of these "characteristics", here an interface with two:

public interface Trait {
    boolean isRegular();
    boolean hasEntry();
}

I want this to be "has" instead of "is"/"get". Can I do this?

@Getter
@lombok.Builder(builderClassName="Builder")
@AllArgsConstructor(staticName="of")
final class SimpleTrait implements Trait {
    private final boolean regular;

    @Accessors(prefix="has")
    private final boolean entry;
}

But now I get this:

SimpleTrait.java:49: warning: Not generating getter for this field: It does not fit your @Accessors prefix list.
    private final boolean entry;
                          ^
error: warnings found and -Werror specified
1 error
1 warning

So, what, the prefix-setting wants to match e.g. a field "hasEntry" with a method "hasEntry()" ? But just as well as I do not want my field "regular" to become "isRegular" -- this is for the get-method, not the field -- I also do not want my "entry" field to be named "hasEntry" -- I want a get-method named "hasEntry()", the other get-method to be "isRegular()":

@Getter
@lombok.Builder(builderClassName="Builder")
@AllArgsConstructor(staticName="of")
final class SimpleTrait implements Trait {
    private final boolean regular;
    private final boolean entry;

    @Override
    public boolean hasEntry() {
        return entry;
    }
}

So, the outcome is -- I cannot have the getter generated??

How about a @Getter annotation with a "prefix" attribute and to be put on the "entry" field? --Instead of supporting awkward field-naming, how about supporting somewhat sane interfaces?

I guess I will return to ... I do not know, yet -- getters with "is" or me writing custom get-methods in the implementation.

Regards,
Morten Sabroe Mortensen

Reply all
Reply to author
Forward
0 new messages