Lombok @NoArgsConstructor, @RequiredArgsConstructor @AllArgsConstructor Annotations

This is a sample application of Lombok ‘s constructor generation below annotation.

  • @NoArgsConstructor(lombok.NoArgsConstructor)
  • @AllArgsConstructor(lombok.AllArgsConstructor)
  • @RequiredArgsConstructor(lombok.RequiredArgsConstructor)

Generate default constructor

@NoArgsConstructorYou can automatically generate the default constructor by giving it to the class.

■ Class that gave @ NoArgsConstructor

import lombok.NoArgsConstructor;@ NoArgsConstructor public  class Person1 {    private  long id;    private String name;    private  int age;}

■ Actually generated source code

The source code actually generated is as follows ( confirmed by delombok ) 
You can see that the default constructor is generated.

Public  Class Person1 {     Private  Long Id;     Private String Name;     Private  Int Age;    public Person 1 () {    }}

Generate a constructor for setting all members

@AllArgsConstructorBy assigning a class to a class, you can automatically generate a constructor with arguments to set values ​​for all members.

■ Class that granted @ AllArgsConstructor

import lombok.AllArgsConstructor;@ AllArgsConstructor public  class Person2 {    private  long id;    private String name;    private  int age;}

■ Actually generated source code

The source code actually generated is as follows ( confirmation with delombok ) 
It turns out that a constructor with argument for setting all members is generated.

Public  Class Person2 {     Private  Long Id;     Private String Name;     Private  Int Age;    Public Person2 ( Final  Long Id, Final String Name, Final  Int Age) {         This .Id = Id;         This .Name = Name;         This .Age = Age;    }}

@ NoArgsConstructor + @ AllArgsConstructor

@NoArgsConstructor@AllArgsConstructorBy assigning and to the class, it is possible to automatically generate both the default constructor and the constructor with arguments for setting values ​​to all members.

■ Classes with @ NoArgsConstructor and @AllArgsConstructor

import lombok.AllArgsConstructor; import lombok.NoArgsConstructor;@ NoArgsConstructor @ AllArgsConstructor public  class Person3 {    private  long id;    private String name;    private  int age;}

■ Actually generated source code

The source code actually generated is as follows ( check with delombok ) 
You can see that the constructor with argument for setting the default constructor and all members is generated.

Public  Class Person3 {     Private  Long Id;     Private String Name;     Private  Int Age;    public Person 3 () {    }    Public Person3 ( Final  Long Id, Final String Name, Final  Int Age) {         This .Id = Id;         This .Name = Name;         This .Age = Age;    }}

Generate a constructor that sets values ​​to mandatory members

@RequiredArgsConstructorBy assigning a class to a class, you can automatically generate a constructor with arguments to set the value to a required member (final member).

■ Class with @ RequiredArgsConstructor

import lombok.RequiredArgsConstructor;@ RequiredArgsConstructor public  class Person4 {    private  final  long id;    private String name;    private  int age;}

■ Actually generated source code

The actual source code generated is as follows ( confirmed by delombok ) 
A constructor for setting the value to final member (id) is generated.

Public  Class Person4 {     Private  Final  Long Id;     Private String Name;     Private  Int Age;    public Person 4 ( final  long id) {         this. id = id;    }}

@ RequiredArgsConstructor + @ AllArgsConstructor

@RequiredArgsConstructorBy @AllArgsConstructorassigning and to the class, it is possible to automatically generate both the constructor for setting the value to the mandatory member and the constructor for setting the value to all the members.

■ Class with @ RequiredArgsConstructor and @AllArgsConstructor

import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor;@ RequiredArgsConstructor @ AllArgsConstructor public  class Person 5 {    private  final  long id;    private String name;    private  int age;}

■ Actually generated source code

The source code actually generated is as follows ( confirmed by delombok ) 
You can see that the constructor that sets the value to the mandatory member and the constructor to set all the members are generated.

Public  Class Person5 {     Private  Final  Long Id;     Private String Name;     Private  Int Age;    public Person 5 ( final  long id) {         this. id = id;    }    Public Person5 ( Final  Long Id, Final String Name, Final  Int Age) {         This .Id = Id;         This .Name = Name;         This .Age = Age;    }}

Control access level of constructor

If you use annotations to generate a constructor, by default the access level of the constructor is public. If you want to control this access level, accessuse the parameter.

■ Sample code

Import Lombok.AccessLevel; Import Lombok.NoArgsConstructor; Import Lombok.RequiredArgsConstructor;public  class Person 6 {    @ NoArgsConstructor (access = AccessLevel.PRIVATE)     public  static  class Person6a {        private  long id;        private String name;        private  int age;    }    @ RequiredArgsConstructor (access = AccessLevel.PROTECTED)     public  static  class Person 6b {        private  final  long id;        private String name;        private  int age;    }}

■ Actually generated source code

In fact the source code that is generated is less than ( Delombok check) 
is the private constructor of the access level of Person6a, constructor of the access level of Person6b you can see that has become protected.

public  class Person 6 {    public  static  class Person6a {         private  long id;         private String name;         private  int age;        private Person 6 a () {        }    }    public  static  class Person6b {         private  final  long id;         private String name;         private  int age;        protected Person 6b ( final  long id) {             this. id = id;        }    }}

Define a factory method

Sometimes you may want to call the factory method without calling the constructor directly. 
In such cases, you staticNamecan define factory methods by using parameters.

■ Sample code

Import Lombok.AllArgsConstructor; Import Lombok.NoArgsConstructor; Import Lombok.RequiredArgsConstructor;public  class Person 7 {    @ RequiredArgsConstructor (staticName = "of" )     public  static  class Person7a {        private  final  long id;        private String name;        private  int age;    }    @ NoArgsConstructor (staticName = "create" )     @ AllArgsConstructor (staticName = "create" )     public  static  class Person 7 b {        private  long id;        private String name;        private  int age;    }}

■ Actually generated source code

You can see that the factory method with the name you specified is defined in the source code actually generated ( confirmation with delombok ) 
staticName. You can also see that the constructor is private.

public  class Person 7 {    public  static  class Person7a {         private  final  long id;         private String name;         private  int age;        private Person 7 a ( final  long id) {             this. id = id;        }        public  static Person 7 a of ( final  long id) {             return  new Person 7 a (id);        }    }    public  static  class Person7b {         private  long id;         private String name;         private  int age;        private Person 7b () {        }        public  static Person 7 b create () {             return  new Person 7 b ();        }        Private Person7b ( Final  Long Id, Final String Name, Final  Int Age) {             This .Id = Id;             This .Name = Name;             This .Age = Age;        }        public  static Person 7 b create ( final  long id, final String name, final  int age) {             return  new Person 7 b (id, name, age);        }    }}

Combine with @ NonNull

If you simply grant @RequiredArgsConstructoror @AllArgsConstructorto a class that has a required member (final member), you can allow null to be set for the target member. 
If you can not tolerate null members, @NonNulllet’s combine annotations.

■ Sample code

Import Lombok.AllArgsConstructor; Import Lombok.NonNull; Import Lombok.RequiredArgsConstructor;public  class Person 8 {    @ RequiredArgsConstructor     public  static  class Person8a {        private  long id;        @ NonNull         private  final String name;        private  int age;    }    @ AllArgsConstructor     public  static  class Person 8 b {        private  final  long id;        @ NonNull         private  final String name;        @ NonNull         private  final String description;    }}

■ Actually generated source code

The source code actually generated is as follows ( confirmed with delombok ) 
You can see that null is guarded in the constructor’s processing.

import lombok.NonNull;public  class Person 8 {    public  static  class Person8a {         private  long id;         @NonNull         private  final String name;         private  int age;        public Person 8 a ( @ NonNull  final String name) {             if (name == null ) {                 throw  new NullPointerException ( "name" );            }            this .name = name;        }    }     @ public static  class Person8b {         private  final  long id;         @ NonNull         private  final String name;         @ NonNull         private  final String description;        Public Person8b ( Final  Long Id, AttoNonNull  Final String Name, AttoNonNull  Final String Description) {             If (Name == Null ) {                 Throw  New NullPointerException ( "Name" );            }            if (description == null ) {                 throw  new NullPointerException ( "description" );            }            This .Id = Id;             This .Name = Name;             This .Description = Description;        }    }}

Annotate the constructor

If you are using a DI container and want to do constructor injection, @Injectannotate the constructor . 
In this way, if you want to add some annotation to the constructor, onConstructoruse the parameter. 
Parameters are described as follows.

onConstructor=Atto__ ({ @Inject })

■ Sample code

onConstructorIt specifies that @Injectannotation should be given by parameter .

import javax.inject.Inject;import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor;public  class Person 9 {    AttoRequiredArgsConstructor (OnConstructor = Atto__ ({ @Inject }))     public  Static  class Person9a {        private  long id;        private  final String name;        private  int age;    }    AttoAllArgsConstructor (OnConstructor = Atto__ ({ @Inject }))     public  Static  class Person9b {        private  final  long id;        private  final String name;        private  int age;    }}

■ Actually generated source code

The source code actually generated is as follows ( confirmed with delombok ) You can see that the annotation is given to the 
constructor @Inject.

import javax.inject.Inject;public  class Person 9 {    public  static  class Person9a {         private  long id;         private  final String name;         private  int age;        AttoInject         Public Person9a ( Final String Name) {             This .Name = Name;        }    }    public  static  class Person 9 b {         private  final  long id;         private  final String name;         private  int age;        @Inject         public Person9b ( final  long id, final String name, final  int age) {             this .id = id;             this .name = name; name;             this .age = age;        }    }}