I'd like to configure Lombok to generate getters named as field(), rather than getField().
Is that possible to do? If so, how?
I'd like to configure Lombok to generate getters named as field(), rather than getField().
Is that possible to do? If so, how?
lombok.accessors.fluent = true
That fluent option gives us means of access that do not have a get or set prefix.
We will now take a look at the chain option , but since it is enabled by default, let's disable it explicitly for now:
@Accessors(fluent = true, chain = false)
@Getter
@Setter
public class FluentAccount {
private String name;
private BigDecimal balance;
}
@Test
public void givenFluentAccount_thenUseFluentAccessors() {
FluentAccount account = new FluentAccount();
account.name("Fluent Account");
account.balance(BigDecimal.TEN);
assertEquals("Fluent Account", account.name());
assertEquals(BigDecimal.TEN, account.balance());
}
Now let's take a look at the chain option :
@Accessors(chain = true)
The chain option gives us setters that return this . Note again that it is set to true by default, but we will set it explicitly for clarity.
This means that we can combine several set operations in one statement.
Let's build on our fluent accessors and change the chain parameter to true :
@Accessors(fluent = true, chain = true)
@Getter
@Setter
public class ChainedFluentAccount {
private String name;
private BigDecimal balance;
}
We get the same effect if we omit the chain and just point:
@Accessors(fluent = true)
Now let's see how this affects our test case:
@Test
public void givenChainedFluentAccount_thenUseChainedFluentAccessors() {
ChainedFluentAccount account = new ChainedFluentAccount()
.name("Fluent Account")
.balance(BigDecimal.TEN);
assertEquals("Fluent Account", account.name());
assertEquals(BigDecimal.TEN, account.balance());
}
Notice how the new statement gets longer with setters , tied together, removing some of the patterning.
This, of course, is how Lombok @Builder uses chain ed fluent accessors.