@Data to de-boilerplate them all

Do you remember writing each and every time the same part of code? Generating getters, setters, constructors, hashCode(), equals() and toString()? There is simple solution to avoid that! You will never need to write these parts again on your own, nor generate it using IDE tools. The answer is simple – Lombok.

Lombok is annotation based library, that generates code in the pre-compilation phase. It means, that you will see boilerplate no more, but it will still exist in the compiled projects. To enable Lombok support in IntelliJ IDEA you have to install plugin in Plugins window like below.

Zrzut ekranu 2017-01-29 17.38.07.png

When it’s done, you have to reboot IDE, to make it work. Of course you need to also add Lombok as a dependency in your pom.xml in tag:


Now it should not be underlined nor marked as error, when you use annotations from library like:


Let’s focus on two, which are the most useful in day to day coding:

  • @Data
  • @Log

@Data is equivalent to five other annotations used one by another on the class scope:  @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor. It generates as the following:

  • toString() by default for all non-static fields
  • equals() by default for all non-static, non-transient fields
  • hashCode() by default for all non-static, non-transient fields
  • getters and setters by default for all non-static fields
  • constructor for final fields and fields with constraints such as @NonNull

Using @Data annotation like:

class Person {
    private String name = "Seba";

is equivalent to many more lines generated or written by developer on his own:

class Person {
    private String name = "Seba";

    public Person(String name) {
        this.name = name;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        return name != null ? name.equals(person.name) : person.name == null;

    public int hashCode() {
        return name != null ? name.hashCode() : 0;

    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +

@Log on the other hand is equivalent to declaration and definition of Logger, but it eliminates adding the same line in each class, that demands logging mechanism:

private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());

Now you can call logging using “invisible” field like:

log.error(“Something’s wrong here”);

I, personally, use @Log4j2 to generate Apache Log4j2 logger:

private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);

I will introduce you in one of future posts, how to configure this library for your demands.

We finally came to the end. I hope, that I encouraged you to discover whole library on your own. Moreover, I hope and believe, that Lombok will become one of the must haves in your programmer’s arsenal.


One thought on “@Data to de-boilerplate them all

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s