En Java, par exemple, si vous créez des JavaBeans comme ↓, la quantité de code sera assez importante en fonction du nombre de champs.
public class Book {
private int id;
private String name;
private int price;
private String publishDate;
public Book() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", publishDate='" + publishDate + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return id == book.id &&
price == book.price &&
Objects.equals(name, book.name) &&
Objects.equals(publishDate, book.publishDate);
}
@Override
public int hashCode() {
return Objects.hash(id, name, price, publishDate);
}
}
Si vous utilisez Lombok, ce sera le code qui a la même méthode que ↑ juste en écrivant comme ↓.
import lombok.Data;
@Data
public class Book {
private int id;
private String name;
private int price;
private String publishDate;
}
C'est pratique, je vais donc l'examiner un peu.
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"
$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
Tout le monde aime IntelliJ IDEA
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.example -DartifactId=try-java-lombok
<dependencies>
〜〜〜
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
https://projectlombok.org/download
Toutes les sources sont ↓ https://github.com/sky0621/try-java-lombok
@ Getter
et @ Setter
Comme son nom l'indique, un setter getter est automatiquement généré lorsqu'il est ajouté à une variable.
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class GetterSetter {
private String text;
}
public class GetterSetter {
private String text;
@java.lang.SuppressWarnings("all")
public String getText() {
return this.text;
}
@java.lang.SuppressWarnings("all")
public void setText(final String text) {
this.text = text;
}
}
@ToString
comme son nom l'indique.
@lombok.ToString
public class ToString {
private String text;
}
public class ToString {
private String text;
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "ToString(text=" + this.text + ")";
}
}
@EqualsAndHashCode
Générez la méthode "equals" et la méthode "hashCode".
@lombok.EqualsAndHashCode
public class EqualsAndHashCode {
private String text;
}
public class EqualsAndHashCode {
private String text;
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCode)) return false;
final EqualsAndHashCode other = (EqualsAndHashCode) o;
if (!other.canEqual((java.lang.Object) this)) return false;
final java.lang.Object this$text = this.text;
final java.lang.Object other$text = other.text;
if (this$text == null ? other$text != null : !this$text.equals(other$text)) return false;
return true;
}
@java.lang.SuppressWarnings("all")
protected boolean canEqual(final java.lang.Object other) {
return other instanceof EqualsAndHashCode;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 59;
int result = 1;
final java.lang.Object $text = this.text;
result = result * PRIME + ($text == null ? 43 : $text.hashCode());
return result;
}
}
@NoArgsConstructor
Générer un constructeur sans argument
@lombok.NoArgsConstructor
public class NoArgsConstructor {
}
public class NoArgsConstructor {
@java.lang.SuppressWarnings("all")
public NoArgsConstructor() {
}
}
@AllArgsConstructor
Générer un constructeur de spécification d'argument complet
@lombok.AllArgsConstructor
public class AllArgsConstructor {
private int id;
private String name;
private Date created;
}
public class AllArgsConstructor {
private int id;
private String name;
private Date created;
@java.lang.SuppressWarnings("all")
public AllArgsConstructor(final int id, final String name, final Date created) {
this.id = id;
this.name = name;
this.created = created;
}
}
@RequiredArgsConstructor
Générer un constructeur avec un paramètre pour chaque champ nécessitant un traitement spécial
@lombok.RequiredArgsConstructor
public class RequiredArgsConstructor {
@NonNull
private int id;
@NonNull
private String text;
private Data created;
}
import lombok.Data;
import lombok.NonNull;
public class RequiredArgsConstructor {
@NonNull
private int id;
@NonNull
private String text;
private Data created;
@java.lang.SuppressWarnings("all")
public RequiredArgsConstructor(@NonNull final int id, @NonNull final String text) {
if (text == null) {
throw new java.lang.NullPointerException("text is marked @NonNull but is null");
}
this.id = id;
this.text = text;
}
}
@Data
Parmi les annotations présentées ci-dessus, «@ ToString», «@ EqualsAndHashCode» et «@ Getter» sont ajoutés à tous les champs, et «@ Setter» est ajouté à tous les champs «final». De plus, «@ RequiredArgsConstructor» est ajouté.
@lombok.Data
public class Data {
private int id;
private String name;
private int price;
private String publishDate;
}
public class Data {
private int id;
private String name;
private int price;
private String publishDate;
@java.lang.SuppressWarnings("all")
public Data() {
}
@java.lang.SuppressWarnings("all")
public int getId() {
return this.id;
}
@java.lang.SuppressWarnings("all")
public String getName() {
return this.name;
}
@java.lang.SuppressWarnings("all")
public int getPrice() {
return this.price;
}
@java.lang.SuppressWarnings("all")
public String getPublishDate() {
return this.publishDate;
}
@java.lang.SuppressWarnings("all")
public void setId(final int id) {
this.id = id;
}
@java.lang.SuppressWarnings("all")
public void setName(final String name) {
this.name = name;
}
@java.lang.SuppressWarnings("all")
public void setPrice(final int price) {
this.price = price;
}
@java.lang.SuppressWarnings("all")
public void setPublishDate(final String publishDate) {
this.publishDate = publishDate;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof Data)) return false;
final Data other = (Data) o;
if (!other.canEqual((java.lang.Object) this)) return false;
if (this.getId() != other.getId()) return false;
final java.lang.Object this$name = this.getName();
final java.lang.Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
if (this.getPrice() != other.getPrice()) return false;
final java.lang.Object this$publishDate = this.getPublishDate();
final java.lang.Object other$publishDate = other.getPublishDate();
if (this$publishDate == null ? other$publishDate != null : !this$publishDate.equals(other$publishDate)) return false;
return true;
}
@java.lang.SuppressWarnings("all")
protected boolean canEqual(final java.lang.Object other) {
return other instanceof Data;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getId();
final java.lang.Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
result = result * PRIME + this.getPrice();
final java.lang.Object $publishDate = this.getPublishDate();
result = result * PRIME + ($publishDate == null ? 43 : $publishDate.hashCode());
return result;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "Data(id=" + this.getId() + ", name=" + this.getName() + ", price=" + this.getPrice() + ", publishDate=" + this.getPublishDate() + ")";
}
}
Il y a encore des annotations utiles, mais aujourd'hui, c'est hors du temps. Le reste viendra plus tard.
Recommended Posts