[JAVA] Lombok ② Object generation

Past articles about Lombok

Lombok ① Introduction

Annotations related to object creation

When using the New operator

Annotation used

Name.java


@NoArgsConstructor
@AllArgsConstructor
@Data
public class Name
{
    private String value = "";
}

Age.java


@NoArgsConstructor
@AllArgsConstructor
@Data
public class Age
{
    private Integer value;
}

Person.java


@AllArgsConstructor
@NoArgsConstructor
@Data
public class Person
{
    private Name name = new Name();
    private Age age = new Age();
}

PersonFactory.java


public class PersonFactory
{
    public static Person empty()
    {
        return new Person();
    }

    public static Person of( String name, Integer age)
    {
        return new Person(
                   new Name( name ),
                   new Age( age )
               );//Constructor arguments are in field definition order
    }


}

It can be generated like this.

When using static methods

Annotation used

Name.java


@NoArgsConstructor(staticName="empty")//Specify static method name
@AllArgsConstructor(staticName="of")//Specify static method name
@Data
public class Name
{
    private String value = "";
}

Age.java


@NoArgsConstructor(staticName="empty")//Specify static method name
@AllArgsConstructor(staticName="of")//Specify static method name
@Data
public class Age
{
    private Integer value;
}

Person.java


@NoArgsConstructor(staticName="empty")//Specify static method name
@AllArgsConstructor(staticName="of")//Specify static method name
@Data
public class Person
{
    private Name name = Name.empty();
    private Age age = Age.empty();
}

PersonFactory.java


public class PersonFactory
{
    public static Person empty()
    {
        return Person.empty();
    }

    public static Person of( String name, Integer age)
    {
        return Person.of(
                  Name.of( name ),
                  Age.of( age )
               );//Method arguments are in field definition order
    }


}

It can be generated like this.

When using the builder method

Annotation used

If the Person class grows as follows,

Person.java


@NoArgsConstructor(staticName="empty")
@AllArgsConstructor(staticName="of")
@Data
public class Person
{
    private Name name = Name.empty();
    private Age age = Age.empty();
    private GenderType genderType = GenderType.UNDEFINED;
    private PhoneNumber phoneNumber = PhoneNumber.empty();
    private MailAddress mailAddress = MailAddress.empty();
    ...
}

The above method makes the Factory class a little more complicated.

PersonFactory.java


public class PersonFactory
{
    public static Person empty()
    {
        return Person.empty();
    }

    public static Person of( String name, Integer age, GenderType genderType, String phoneNumber, String mailAddress )
    {
        return Person.of(
                  Name.of( name ),
                  Age.of( age ),
                  genderType,
                  PhoneNumber.of( phoneNumber ),
                  MailAddress.of( mailAddress )
               );//An error occurs every time the order or number of fields changes
    }
}

In this way, the argument of the of method depends on the field order and number of the class.

So, if you add @Builder, it will change as follows.

Person.java


@Builder//add to
@AllArgsConstructor(access=AccessLevel.PRIVATE)//Control of constructor with normal arguments
@NoArgsConstructor(staticName="empty")
@Data
public class Person
{
    private Name name = Name.empty();
    private Age age = Age.empty();
    private GenderType genderType = GenderType.UNDEFINED;
    private PhoneNumber phoneNumber = PhoneNumber.empty();
    private MailAddress mailAddress = MailAddress.empty();
    ...
}

PersonFactory.java


public class PersonFactory
{
    public static Person empty()
    {
        return Person.empty();
    }

    public static Person of( String name, Integer age, GenderType genderType, String phoneNumber, String mailAddress )
    {
        return Person.builder()//start bullder
                     .name( Name.of( name ) )//Set the value for the field name method
                     .phoneNumber( PhoneNumber.of( phoneNumber ) )//There is no problem even if the fields are switched because the order is random.
                     .mailAddress( MailAddress.of( mailAddress ) )
                     .age( Age.of( age ) )
                     ...
                     .build();//Generate

    }
}

As mentioned above, it will be possible to respond flexibly as it is. However, if you change the field name, the lombok method will not be modified, so you need to modify the source manually **.

Also, you need to be careful because ** Null will be entered ** for fields for which you did not set a value here.

next time

Next, I will summarize the detailed usage of each annotation.

Well then.

Recommended Posts

Lombok ② Object generation
Lombok ① Introduction
Lombok memo
Try Lombok
Java starting from beginner, class declaration / object generation
[Personal memorandum] Eclipse button placement control object generation