HomeLombokLombok @Accessors Examples

Lombok @Accessors Examples

In this article we will look into Lombok @Accessors annotation and how it works with fluent, chain, and prefix options to generate accessors.

By default Lombok @Getter / @Setter generates standard accessors and lombok follows the bean specification for getters and setters. Lombok’s @Accessors does nothing it self alone, it is used with @Getter/@Setter annotations to configure how lombok generates and looks for getters and setters.

Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.

1. Generate fluent accessors

The fluent option gives us accessors that don’t have a get or set prefix. By default fluent option is false.

Lomboked LombokAccessorsDemo1.java

@Getter @Setter
@Accessors(fluent = true)
public class LombokAccessorsDemo1 {

	private Long id;
	private String name;
}

Testing Lomboked LombokAccessorsDemo1.java

public class LombokAccessorsDemo1Test {

	public static void main(String[] args) {
		
         LombokAccessorsDemo1 lad1 = new LombokAccessorsDemo1()
        		 .id(Long.valueOf(1))
        		 .name("Peter");
         
         System.out.println("id => "+lad1.id());
         System.out.println("name => "+lad1.name());
	}
}

Output Results :

id => 1
name => Peter

DeLomboked LombokAccessorsDemo1.java

public class LombokAccessorsDemo1 {

	private Long id;
	private String name;

	public Long id() {
		return this.id;
	}

	public String name() {
		return this.name;
	}

	public LombokAccessorsDemo1 id(final Long id) {
		this.id = id;
		return this;
	}

	public LombokAccessorsDemo1 name(final String name) {
		this.name = name;
		return this;
	}

}

2. Generate chained accessors

If chain option true, setters return this instead of void. default value false, unless fluent=true, then default true

Lomboked LombokAccessorsDemo2.java

@Getter @Setter
@Accessors(chain = true)
public class LombokAccessorsDemo2 {

	private Long id;
	private String name;
}

Testing Lomboked LombokAccessorsDemo2.java

public class LombokAccessorsDemo2Test {

	public static void main(String[] args) {
		
         LombokAccessorsDemo2 lad2 = new LombokAccessorsDemo2()
        		 .setId(Long.valueOf(1))
        		 .setName("Peter");
         
         System.out.println("id => "+lad2.getId());
         System.out.println("name => "+lad2.getName());
	}
}

Output Results :

id => 1
name => Peter

DeLomboked LombokAccessorsDemo2.java

public class LombokAccessorsDemo2 {
	
	private Long id;
	private String name;

	public Long getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public LombokAccessorsDemo2 setId(final Long id) {
		this.id = id;
		return this;
	}

	public LombokAccessorsDemo2 setName(final String name) {
		this.name = name;
		return this;
	}

}

3. Generate prefix accessors

The value of prefix attribute of Lombok’s @Accessors annotation is a list of strings, Lombok only applies the fields are prefixed with any of these prefixes and most not followed by lower case after prefix except in case the prefix value is not ended with alphabet.

Lomboked LombokAccessorsDemo3.java

@Getter @Setter
@Accessors(prefix = {"user", "is_", "auth", "hash$"})
public class LombokAccessorsDemo3 {

	private Long id;
	private String userName;
	private boolean active;
	private boolean is_admin;
	private String authcode;
	private String hash$key;	
}

Testing Lomboked LombokAccessorsDemo3.java

public class LombokAccessorsDemo3Test {

	public static void main(String[] args) {

		LombokAccessorsDemo3 lad3 = new LombokAccessorsDemo3();
		lad3.setName("Peter");
		lad3.setAdmin(true);
		lad3.setKey("xajc$2nac_acdpn$_kt");

		System.out.println("userName => " + lad3.getName());
		System.out.println("is_Admin => " + lad3.isAdmin());
		System.out.println("hash$key => " + lad3.getKey());
	}
}

Output Results :

userName => Peter
is_Admin => true
hash$key => xajc$2nac_acdpn$_kt

DeLomboked LombokAccessorsDemo3.java

public class LombokAccessorsDemo3 {

	private Long id;
	private String userName;
	private boolean active;
	private boolean is_admin;
	private String authcode;
	private String hash$key;

	public String getName() {
		return this.userName;
	}

	public boolean isAdmin() {
		return this.is_admin;
	}

	public String getKey() {
		return this.hash$key;
	}

	public void setName(final String userName) {
		this.userName = userName;
	}

	public void setAdmin(final boolean is_admin) {
		this.is_admin = is_admin;
	}

	public void setKey(final String hash$key) {
		this.hash$key = hash$key;
	}

}

4. @Accessors at field level

Following example demonstrates how Lombok’s @Accessors annotation works on field level.

Lomboked LombokAccessorsDemo4.java

@Getter @Setter
public class LombokAccessorsDemo4 {

	@Accessors(chain = true)
	private Long id;
	
	@Accessors(fluent = true)
	private String userName;
	
	private boolean active;
	
	@Accessors(prefix = "is_")
	private boolean is_admin;
	
	private String authcode;
	
	@Accessors(prefix = "hash$")
	private String hash$key;
	
}

Testing Lomboked LombokAccessorsDemo4.java

public class LombokAccessorsDemo4Test {

	public static void main(String[] args) {

		LombokAccessorsDemo4 lad4 = new LombokAccessorsDemo4()
		.setId(Long.valueOf(1))
		.userName("Peter");
		lad4.setActive(true);
		lad4.setAdmin(true);
		lad4.setAuthcode("dfjksldkf$bdb_$");
		lad4.setKey("xajc$2nac_acdpn$_kt");

		System.out.println("id => " + lad4.getId());
		System.out.println("userName => " + lad4.userName());
		System.out.println("active => " + lad4.isActive());
		System.out.println("is_Admin => " + lad4.isAdmin());
		System.out.println("authcode => " + lad4.getAuthcode());
		System.out.println("hash$key => " + lad4.getKey());
	}
}

Output Results :

id => 1
userName => Peter
active => true
is_Admin => true
authcode => dfjksldkf$bdb_$
hash$key => xajc$2nac_acdpn$_kt

DeLomboked LombokAccessorsDemo4.java

public class LombokAccessorsDemo4 {
	private Long id;
	private String userName;
	private boolean active;
	private boolean is_admin;
	private String authcode;
	private String hash$key;

	public Long getId() {
		return this.id;
	}

	public String userName() {
		return this.userName;
	}

	public boolean isActive() {
		return this.active;
	}

	public boolean isAdmin() {
		return this.is_admin;
	}

	public String getAuthcode() {
		return this.authcode;
	}

	public String getKey() {
		return this.hash$key;
	}

	public LombokAccessorsDemo4 setId(final Long id) {
		this.id = id;
		return this;
	}

	public LombokAccessorsDemo4 userName(final String userName) {
		this.userName = userName;
		return this;
	}

	public void setActive(final boolean active) {
		this.active = active;
	}

	public void setAdmin(final boolean is_admin) {
		this.is_admin = is_admin;
	}

	public void setAuthcode(final String authcode) {
		this.authcode = authcode;
	}

	public void setKey(final String hash$key) {
		this.hash$key = hash$key;
	}
}

5. Conclusion

In this article we have lokked into Lombok @Accessors annotation and how it works with fluent, chain, and prefix options to generate accessors. You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.

You can checkout source code at github.

You might be interested in our other following Lombok Tutorials :

  1. Lombok @Data
  2. Lombok @Builder
  3. Lombok @Value
  4. Lombok @Slf4j
  5. Lombok @NonNull
  6. Lombok Spring Boot example

LEAVE A REPLY

Please enter your comment!
Please enter your name here