As it is, it is a series that summarizes the tools etc. learned (used) in the field. I write it with the feeling that it is also for myself and that it should be for someone. We look forward to your suggestions and comments regarding mistakes and description methods!
It is a library that can eliminate Java boilerplate code [^ 1] by adding annotations. Just by annotating it, it will ** implement getters and setters ** for all fields, and ** implement constructors **!
I personally think that ** Spring-Boot is very compatible ** because it makes data class creation and constructor injection easier! It is also useful when you want to use an immutable object [^ 2], so Please use it!
By the way, the reading seems to be Lombok
or Lombok
. I am a Lombok
sect.
From here, we will explain each annotation.
Getter、Setter
You can implement getters and setters by giving @ Getter
and @Setter
to the class or field.
For example, suppose you implement the following Sample.java
.
Sample.java
@Getter
@Setter
public class Sample {
private String id;
private String name;
private Integer age:
}
If you compile the above and check the contents of Sample.class
, it will be as follows.
Sample.class
public class Sample {
private String id;
private String name;
private Integer age:
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Integer getAge() {
return this.name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
}
… The getters and setters are nicely implemented for all fields. The more fields you have, the less description you can make!
In this example, it is given to class, If you don't want to give it to all fields, give it to each field!
This is an annotation that automatically generates a constructor just by adding it. There are three types.
@NoArgsConstructor
… Automatically generate a default constructor.@AllArgsConstructor
… Generates a constructor that takes initialization values for all fields as arguments.@RequiredArgsConstructor
… Generates a constructor that takes an initialization value for the final field as an argument.RequiredArgsConstructor
Assuming that the implemented Sample.java
is as follows
Sample.java
@RequiredArgsConstructor
public class Sample {
private final String id;
private final String name;
private Integer age;
}
The following Sample.class
will be created.
Sample.class
public class Sample {
private final String id;
private final String name;
private Integer age;
public Sample(String id, String name) {
this.id = id;
this.name = name;
}
}
NoArgsConstructor + AllArgsConstructor
If you give more than one, the constructor will be generated accordingly.
Due to the java specification, if you implement the constructor clearly, the default constructor will not be implemented, so
@NoArgsConstructor
also has a lot of turns.
Sample.java
@NoArgsConstructor
@AllArgsConstructor
public class Sample {
private String id;
private String name;
private Integer age;
}
Sample.class
public class Sample {
private String id;
private String name;
private Integer age;
public Sample() {
};
public Sample (String id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
This combination works well with MyBatis Entity class.
ToString
As the name implies, @ToString
implements (overrides) toString
.
You can also use ʻexclude` to exclude the target field.
Sample.java
@ToString(exclude = "age")
public class Sample {
private String id;
private String name;
private Integer age;
}
Sample.class
public class Sample {
private String id;
private String name;
private Integer age;
@Override
public String toString() {
return "Sample(id=" + this.id + ", name=" + this.name + ")");
}
}
EqualsAndHashCode
As the name implies, @EqualsAndHashCode
also implements (overrides) ʻequals and
hashCode`.
The reason why these two are a set is that they are [mechanically related and must not contradict](https://qiita.com/yoshi389111/items/9e34fe297bd908a36065#hashcode-%E3%81%AE% This is because E5% 9F% BA% E6% 9C% AC).
Sample.java
@EqualsAndHashCode
public class Sample {
private String name;
private int age;
}
Sample.class
public class Sample {
private String name;
private int age;
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Person)) return false;
final Person other = (Person) o;
if (!other.canEqual((Object) this)) return false;
final Object this$name = this.name;
final Object other$name = other.name;
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
if (this.age != other.age) return false;
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $name = this.name;
result = result * PRIME + ($name == null ? 0 : $name.hashCode());
result = result * PRIME + this.age;
return result;
}
protected boolean canEqual(Object other) {
return other instanceof Person;
}
}
The convenience is unknown because I didn't use it at all in the field.
Value
When @Value
is added, the following annotation is added and it becomes an immutable object.
@Getter
@ToString
@EqualsAndHashCode
@AllArgsConstructor
final
is given to the class and each fieldprivate
The value is set by the constructor at the time of generation, and the value cannot be changed thereafter.
It's an annotation that you want to use when you want to make it a truly immutable object.
You can generate static factory methods by setting the staticConstructor
option.
In that case, the constructor will be changed to private
, so you will not be able to create an instance without passing through the factory method.
Data
If @Data
is added, it will be the same as when the following annotations are added.
@Getter
@Setter
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
It is an annotation that summarizes the functions of annotation similar to @Value
.
This is similar to the image of creating a simple bean class.
I've written a lot, but I can't master it. ..
Even though it has @Value
, it has final
. (There is no problem because it works normally just because it is redundant)
This time, I introduced the annotations that are likely to be used frequently.
Some annotations haven't been introduced yet
There are many options like staticConstructor
, so if you are interested, please check it out! !!
Thank you for reading to the end!
[^ 1]: Boilerplate code is a standard code that cannot be omitted due to language specifications. Redundant code that must be written even though it is not the essence of logic. [^ 2]: An object whose value does not change after it is created.
Recommended Posts