当前位置:网站首页>Lombok -- simplify code
Lombok -- simplify code
2022-07-03 07:22:00 【Maple Leaf pear flower】
Currently encountered lombok.jar A plug-in for , It is good to use , You can eliminate setter、gettter Methods , Can make the code more concise .
lombok Its official website :https://projectlombok.org/
install
1、 Download... On the official website lombok.jar file
2、 Double-click the downloaded lombok.jar Will help you automatically install . In fact, this step is to lombok.jar Placed in eclipse In the path of , And in eclipse.ini Add... To the last line -javaagent:lombok.jar
annotation
@Getter: Comment on attribute , Provide getter Method ;
@Setter: Comment on attribute , Provide setter Method ;
@AllArgsConstructor: Annotation on class , Provide construction method , Parameters are all attributes ;
@NoArgsConstructor: Annotation on class , Provides a parameterless construction method ;
@Data: Annotation on class , Provide all properties getter Method 、setter Methods and equals、hashCode、toString Other methods ;
@Log: Annotation on class , Provide a name called log Properties of , The type is java.util.logging.Logger, You can also use @Log4j、@Log4j2、@Slf4j And other comments ;
@ToString: Annotation on class , Provide toString Method ;
@EqualsAndHashCode: Annotation on class , Provide equals、hashCode Method ;
@Synchronized: Comment on method , Provide synchronized, You can specify the name of the lock ;
@NonNull: Annotations on method parameters , Provide parameter verification , Prevent null pointer exception ;
@Cleanup: Annotations on local variables , Provide closure of resources , That is to call close Method ;
Simplified code
@NoArgsConstructor
@AllArgsConstructor
public @Data class Person {
private String name;
}
After compiling the above code
import java.beans.ConstructorProperties;
public class Person {
private String name;
public Person() {
}
@ConstructorProperties({ "name" })
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Person))
return false;
Person other = (Person) o;
if (!other.canEqual(this))
return false;
Object this$name = getName();
Object other$name = other.getName();
return this$name == null ? other$name == null : this$name.equals(other$name);
}
protected boolean canEqual(Object other) {
return other instanceof Person;
}
public int hashCode() {
int PRIME = 59;
int result = 1;
Object $name = getName();
return result * 59 + ($name == null ? 43 : $name.hashCode());
}
public String toString() {
return "Person(name=" + getName() + ")";
}
}
This plug-in simplifies a lot of style code , Make the code very clean , However, it will lead to poor readability , Personally, I think it's quite good to use .
边栏推荐
- Book recommendation~
- Recursion, Fibonacci sequence
- Advanced API (local simulation download file)
- [Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
- Thoughts on project development
- [most detailed] latest and complete redis interview book (50)
- Laravel Web Framework
- [solved] sqlexception: invalid value for getint() - 'Tian Peng‘
- [set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
- TypeScript let與var的區別
猜你喜欢
Common methods of file class
691. 立方体IV
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Map interface and method
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
你开发数据API最快多长时间?我1分钟就足够了
Selenium key knowledge explanation
4279. Cartesian tree
Flask Foundation
[HCAI] learning summary OSI model
随机推荐
Use of file class
Jeecg data button permission settings
Basic components and intermediate components
dataworks自定義函數開發環境搭建
Pat grade a real problem 1166
[set theory] partition (partition | partition example | partition and equivalence relationship)
Advanced API (multithreading)
Arduino Serial系列函数 有关print read 的总结
Hash table, generic
Interfaces and related concepts
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
Use of generics
Circuit, packet and message exchange
Advanced API (serialization & deserialization)
Jeecg menu path display problem
MySQL mistakenly deleted the root account and failed to log in
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
[set theory] order relation (partial order relation | partial order set | example of partial order set)
Understanding of class
Interview questions about producers and consumers (important)