Java Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

1. Overview

It's pretty typical to have get and set methods in our domain objects, but there are other ways that we may find more expressive.

In this tutorial, we'll learn about Project Lombok‘s @Accessors annotation and its support for fluent, chained, and custom accessors.

Before continuing, though, our IDE will need Lombok installed.

2. Standard Accessors

Before we look at the @Accessors annotation, let's review how Lombok treats the @Getter and @Setter annotations by default.

First, let's create our class:

@Getter
@Setter
public class StandardAccount {
    private String name;
    private BigDecimal balance;
}

And now let's create a test case. We can see in our test that Lombok has added typical getter and setter methods:

@Test
public void givenStandardAccount_thenUseStandardAccessors() {
    StandardAccount account = new StandardAccount();
    account.setName("Basic Accessors");
    account.setBalance(BigDecimal.TEN);

    assertEquals("Basic Accessors", account.getName());
    assertEquals(BigDecimal.TEN, account.getBalance()); 
}

We'll see how this test case changes as we look at @Accessor‘s options.

3. Fluent Accessors

Let's begin with the fluent option:

@Accessors(fluent = true)

The fluent option gives us accessors that don't have a get or set prefix.

We'll take a look at the chain option in a moment, but since it's 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;
}

Now, our test still behaves the same, but we've changed the way we access and mutate state:

@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());
}

Notice how the get and set prefixes have disappeared.

4. Chained Accessors

Now let's take a look at the chain option:

@Accessors(chain = true)

The chain option gives us setters that return thisAgain note that it defaults to true, but we'll set it explicitly for clarity.

This means we can chain multiple set operations together in one statement.

Let's build on our fluent accessors and change the chain option 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 leave out chain and just specify:

@Accessors(fluent = true)

And 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 becomes longer with the setters chained together, removing some boilerplate.

This, of course, is how Lombok's @Builder makes use of chained fluent accessors.

5. Prefix Accessors

And finally, at times our fields may have a different naming convention than we'd like to expose via getters and setters.

Let's consider the following class that uses Hungarian Notation for its fields:

public class PrefixedAccount { 
    private String sName; 
    private BigDecimal bdBalance; 
}

If we were to expose this with @Getter and @Setter, we'd get methods like getSName, which isn't quite as readable.

The prefix option allows us to tell Lombok which prefixes to ignore:

@Accessors(prefix = {"s", "bd"})
@Getter
@Setter
public class PrefixedAccount {
    private String sName;
    private BigDecimal bdBalance;
}

So, let's see how that affects our test case:

@Test
public void givenPrefixedAccount_thenRemovePrefixFromAccessors() {
    PrefixedAccount account = new PrefixedAccount();
    account.setName("Prefixed Fields");
    account.setBalance(BigDecimal.TEN);

    assertEquals("Prefixed Fields", account.getName()); 
    assertEquals(BigDecimal.TEN, account.getBalance());
}

Notice how the accessors for our sName field (setName, getName) omit the leading s and the accessors for bdBalance omit the leading bd.

However, Lombok only applies prefixes when a prefix is followed by something other than a lowercase letter.

This makes sure that if we have a field that isn't using Hungarian Notation, such as state, but starts with one of our prefixes, s, we don't end up with getTate()!

Lastly, let's say we want to use underscores in our notation but also want to follow it with a lowercase letter.

Let's add a field s_notes with prefix s_:

@Accessors(prefix = "s_")
private String s_notes;

Following the lowercase letter rule we'd get methods like getS_Notes(), so Lombok also applies prefixes when a prefix itself ends in something that isn't a letter.

6. Configuration Properties

We can set a project- or directory-wide default for our favorite combination of settings by adding configuration properties to a lombok.config file:

lombok.accessors.chain=true
lombok.accessors.fluent=true

See the Lombok Feature Configuration Guide for further details.

7. Conclusion

In this article, we used the fluent, chain, and prefix options of Lombok's @Accessors annotation in various combinations to see how it affected the generated code.

To learn more, be sure to take a look at the Lombok Accessors JavaDoc and Experimental Feature Guide.

As usual, the source for this article is available over on GitHub.

Java bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

Leave a Reply

avatar
  Subscribe  
Notify of