Lombok使用示例详情

浅浅的花香味﹌ 2022-05-12 10:04 307阅读 0赞

简介

Lombok是一个可以通过注解来帮助我们简化消除一些必须有但显得很臃肿的Java代码的一种工具,通过使用对应的注解,可以在编译源码的时候动态添加源码。

例如在实体中经常见到一堆Getter和Setter方法,这些方法是必要的不可缺少的,但是这些代码感觉却像是“垃圾”,看起来重复而臃肿,看起来也不美观,也不简洁清爽,可以使用lombok,在类上直接使用@Getter @Setter 这两个注解,那么代码在编译的时候会自动帮你生成这个类下的所有字段对应的Getter和Setter方法,实体中只有一些属性,看起来实体类变得简洁很多,虽然IDE可以很快的生成出来,但是生成之后的实体还是那么的臃肿,而且如果修改了字段的名称或者字段的类型还要重新生成,比较麻烦,使用Lombok就是消除一些含金量不高却必须要有的代码,使程序员看起来代码更加简洁、清爽。

Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!

lombok的官方地址:https://projectlombok.org/
lombok的Github地址:https://github.com/rzwitserloot/lombok

Lombok plugin 插件

Intellij idea 使用Lombok需要安装插件:Lombok plugin: Preferences —> Plugins —> 搜索 Lombok plugin — > Install
同时设置 Preferences -> Compiler -> Annotation Processors -> Enable annotation processing勾选。

70

使用示例

首先引入lombok依赖

  1. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  2. <dependency>
  3. <groupId>org.projectlombok</groupId>
  4. <artifactId>lombok</artifactId>
  5. <version>1.16.20</version>
  6. <scope>provided</scope>
  7. </dependency>

1. @Getter/@Setter

为字段生成Getter和Setter方法,可以注解到字段或者类上(注解在类上会为类中的所有字段生成Getter和Setter方法),默认是public类型的,如果需要的话可以修改方法的访问级别。

  1. public class User {
  2. @Getter @Setter
  3. private Long id;
  4. @Getter(AccessLevel.PROTECTED)
  5. private String phone;
  6. private String password;
  7. }

编译后的代码:

  1. public class User {
  2. private Long id;
  3. private String phone;
  4. private String password;
  5. public User() {
  6. }
  7. public Long getId() {
  8. return this.id;
  9. }
  10. public void setId(Long id) {
  11. this.id = id;
  12. }
  13. protected String getPhone() {
  14. return this.phone;
  15. }
  16. }

结果解释:

id字段生成了Getter&Setter,访问修饰符是public
phone只生成了Getter方法,因为只使用了@Getter而没有使用@Setter, 并且访问修饰符是protected
password 上并没有注解,所以什么都不生成
注意:Lombok中的注解一般都会包含一个无参构造函数注解@NoArgsConstructor(用于生成无参构造函数的) ,所以还会额外生成一个无参构造函数

@Getter @Setter 注解在类上,表示为类中的所有字段生成Getter&Setter方法。

  1. @Getter @Setter
  2. public class User {
  3. private Long id;
  4. private String phone;
  5. private String password;
  6. }
  7. public class User {
  8. private Long id;
  9. private String phone;
  10. private String password;
  11. public User() {
  12. }
  13. public Long getId() {
  14. return this.id;
  15. }
  16. public String getPhone() {
  17. return this.phone;
  18. }
  19. public String getPassword() {
  20. return this.password;
  21. }
  22. public void setId(Long id) {
  23. this.id = id;
  24. }
  25. public void setPhone(String phone) {
  26. this.phone = phone;
  27. }
  28. public void setPassword(String password) {
  29. this.password = password;
  30. }
  31. }

2. @NonNull

为字段赋值时(即调用字段的setter方法时),如果传的参数为null,则会抛出空异常NullPointerException,生成setter方法时会对参数是否为空检查

  1. @Getter
  2. @Setter
  3. public class User {
  4. private Long id;
  5. @NonNull
  6. private String phone;
  7. }
  8. public class User {
  9. private Long id;
  10. @NonNull
  11. private String phone;
  12. public User() {
  13. }
  14. public Long getId() {
  15. return this.id;
  16. }
  17. public void setId(Long id) {
  18. this.id = id;
  19. }
  20. @NonNull
  21. public String getPhone() {
  22. return this.phone;
  23. }
  24. public void setPhone(@NonNull String phone) {
  25. if(phone == null) {
  26. throw new NullPointerException("phone");
  27. } else {
  28. this.phone = phone;
  29. }
  30. }
  31. }

3. @NoArgsConstructor

生成一个无参构造方法。当类中有final字段没有被初始化时,编译器会报错,此时可用@NoArgsConstructor(force = true),然后就会为没有初始化的final字段设置默认值 0 / false / null, 这样编译器就不会报错。对于具有约束的字段(例如@NonNull字段),不会生成检查或分配,因此请注意,正确初始化这些字段之前,这些约束无效。

  1. @NoArgsConstructor(force = true)
  2. public class User {
  3. private Long id;
  4. @NonNull
  5. private String phone;
  6. private final Integer age;
  7. }
  8. public class User {
  9. private Long id;
  10. @NonNull
  11. private String phone;
  12. private final Integer age = null;
  13. public User() {
  14. }
  15. }

4. @RequiredArgsConstructor

生成构造方法(可能带参数也可能不带参数),如果带参数,这参数只能是以final修饰的未经初始化的字段,或者是以@NonNull注解的未经初始化的字段。

@RequiredArgsConstructor(staticName = “of”)会生成一个of()的静态方法,并把构造方法设置为私有的

  1. @RequiredArgsConstructor
  2. public class User {
  3. private Long id;
  4. @NonNull
  5. private String phone;
  6. @NotNull
  7. private Integer status = 0;
  8. private final Integer age;
  9. private final String country = "china";
  10. }
  11. public class User {
  12. private Long id;
  13. @NonNull
  14. private String phone;
  15. @NotNull
  16. private Integer status = Integer.valueOf(0);
  17. private final Integer age;
  18. private final String country = "china";
  19. public User(@NonNull String phone, Integer age) {
  20. if(phone == null) {
  21. throw new NullPointerException("phone");
  22. } else {
  23. this.phone = phone;
  24. this.age = age;
  25. }
  26. }
  27. }

必要的构造函数只会生成final修饰的未经初始化的字段或者是以@NonNull注解的未经初始化的字段, 所以生成了public User(@NonNull String phone, Integer age)构造函数

  1. @RequiredArgsConstructor(staticName = "of")
  2. public class User {
  3. private Long id;
  4. @NonNull
  5. private String phone;
  6. @NotNull
  7. private Integer status = 0;
  8. private final Integer age;
  9. private final String country = "china";
  10. }
  11. public class User {
  12. private Long id;
  13. @NonNull
  14. private String phone;
  15. @NotNull
  16. private Integer status = Integer.valueOf(0);
  17. private final Integer age;
  18. private final String country = "china";
  19. private User(@NonNull String phone, Integer age) {
  20. if(phone == null) {
  21. throw new NullPointerException("phone");
  22. } else {
  23. this.phone = phone;
  24. this.age = age;
  25. }
  26. }
  27. public static User of(@NonNull String phone, Integer age) {
  28. return new User(phone, age);
  29. }
  30. }

5. @AllArgsConstructor

生成一个全参数的构造方法

  1. @AllArgsConstructor
  2. public class User {
  3. private Long id;
  4. @NonNull
  5. private String phone;
  6. @NotNull
  7. private Integer status = 0;
  8. private final Integer age;
  9. private final String country = "china";
  10. }
  11. public class User {
  12. private Long id;
  13. @NonNull
  14. private String phone;
  15. @NotNull
  16. private Integer status = Integer.valueOf(0);
  17. private final Integer age;
  18. private final String country = "china";
  19. public User(Long id, @NonNull String phone, Integer status, Integer age) {
  20. if(phone == null) {
  21. throw new NullPointerException("phone");
  22. } else {
  23. this.id = id;
  24. this.phone = phone;
  25. this.status = status;
  26. this.age = age;
  27. }
  28. }
  29. }

6. @ToString

生成toString()方法,默认情况下它会按顺序(以逗号分隔)打印你的类名称以及每个字段。可以这样设置不包含哪些字段,可以指定一个也可以指定多个@ToString(exclude = “id”) / @ToString(exclude = {“id”,”name”})
如果继承的有父类的话,可以设置callSuper 让其调用父类的toString()方法,例如:@ToString(callSuper = true)

  1. @ToString(exclude = {"password", "salt"})
  2. public class User {
  3. private Long id;
  4. private String phone;
  5. private String password;
  6. private String salt;
  7. }
  8. public class User {
  9. private Long id;
  10. private String phone;
  11. private String password;
  12. private String salt;
  13. public User() {
  14. }
  15. public String toString() {
  16. return "User(id=" + this.id + ", phone=" + this.phone + ")";
  17. }
  18. }
  19. @ToString(exclude = {"password", "salt"}, callSuper = true)
  20. public class User {
  21. private Long id;
  22. private String phone;
  23. private String password;
  24. private String salt;
  25. }
  26. public class User {
  27. private Long id;
  28. private String phone;
  29. private String password;
  30. private String salt;
  31. public User() {
  32. }
  33. public String toString() {
  34. return "User(super=" + super.toString() + ", id=" + this.id + ", phone=" + this.phone + ")";
  35. }
  36. }

7. @EqualsAndHashCode

生成hashCode()和equals()方法,默认情况下,它将使用所有非静态,非transient字段。但可以通过在可选的exclude参数中来排除更多字段。或者,通过在of参数中命名它们来准确指定希望使用哪些字段。

// exclude 排除字段
@EqualsAndHashCode(exclude = {“password”, “salt”})

// of 指定要包含的字段
@EqualsAndHashCode(of = {“id”, “phone”, “password”})

  1. @EqualsAndHashCode
  2. public class User implements Serializable{
  3. private static final long serialVersionUID = 6569081236403751407L;
  4. private Long id;
  5. private String phone;
  6. private transient int status;
  7. }
  8. public class User implements Serializable {
  9. private static final long serialVersionUID = 6569081236403751407L;
  10. private Long id;
  11. private String phone;
  12. private transient int status;
  13. public User() {
  14. }
  15. public boolean equals(Object o) {
  16. if(o == this) {
  17. return true;
  18. } else if(!(o instanceof User)) {
  19. return false;
  20. } else {
  21. User other = (User)o;
  22. if(!other.canEqual(this)) {
  23. return false;
  24. } else {
  25. Long this$id = this.id;
  26. Long other$id = other.id;
  27. if(this$id == null) {
  28. if(other$id != null) {
  29. return false;
  30. }
  31. } else if(!this$id.equals(other$id)) {
  32. return false;
  33. }
  34. String this$phone = this.phone;
  35. String other$phone = other.phone;
  36. if(this$phone == null) {
  37. if(other$phone != null) {
  38. return false;
  39. }
  40. } else if(!this$phone.equals(other$phone)) {
  41. return false;
  42. }
  43. return true;
  44. }
  45. }
  46. }
  47. protected boolean canEqual(Object other) {
  48. return other instanceof User;
  49. }
  50. public int hashCode() {
  51. boolean PRIME = true;
  52. byte result = 1;
  53. Long $id = this.id;
  54. int result1 = result * 59 + ($id == null?43:$id.hashCode());
  55. String $phone = this.phone;
  56. result1 = result1 * 59 + ($phone == null?43:$phone.hashCode());
  57. return result1;
  58. }
  59. }

生成了 equals 、hashCode 和 canEqual 无参构造函数 四个方法

8. @Data

@Data 包含了 @ToString、@EqualsAndHashCode、@Getter / @Setter和@RequiredArgsConstructor的功能

  1. @Data
  2. public class User {
  3. private Long id;
  4. private String phone;
  5. private Integer status;
  6. }
  7. public class User {
  8. private Long id;
  9. private String phone;
  10. private Integer status;
  11. public User() {
  12. }
  13. public Long getId() {
  14. return this.id;
  15. }
  16. public String getPhone() {
  17. return this.phone;
  18. }
  19. public Integer getStatus() {
  20. return this.status;
  21. }
  22. public void setId(Long id) {
  23. this.id = id;
  24. }
  25. public void setPhone(String phone) {
  26. this.phone = phone;
  27. }
  28. public void setStatus(Integer status) {
  29. this.status = status;
  30. }
  31. public boolean equals(Object o) {
  32. if(o == this) {
  33. return true;
  34. } else if(!(o instanceof User)) {
  35. return false;
  36. } else {
  37. User other = (User)o;
  38. if(!other.canEqual(this)) {
  39. return false;
  40. } else {
  41. label47: {
  42. Long this$id = this.getId();
  43. Long other$id = other.getId();
  44. if(this$id == null) {
  45. if(other$id == null) {
  46. break label47;
  47. }
  48. } else if(this$id.equals(other$id)) {
  49. break label47;
  50. }
  51. return false;
  52. }
  53. String this$phone = this.getPhone();
  54. String other$phone = other.getPhone();
  55. if(this$phone == null) {
  56. if(other$phone != null) {
  57. return false;
  58. }
  59. } else if(!this$phone.equals(other$phone)) {
  60. return false;
  61. }
  62. Integer this$status = this.getStatus();
  63. Integer other$status = other.getStatus();
  64. if(this$status == null) {
  65. if(other$status != null) {
  66. return false;
  67. }
  68. } else if(!this$status.equals(other$status)) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. }
  74. }
  75. protected boolean canEqual(Object other) {
  76. return other instanceof User;
  77. }
  78. public int hashCode() {
  79. boolean PRIME = true;
  80. byte result = 1;
  81. Long $id = this.getId();
  82. int result1 = result * 59 + ($id == null?43:$id.hashCode());
  83. String $phone = this.getPhone();
  84. result1 = result1 * 59 + ($phone == null?43:$phone.hashCode());
  85. Integer $status = this.getStatus();
  86. result1 = result1 * 59 + ($status == null?43:$status.hashCode());
  87. return result1;
  88. }
  89. public String toString() {
  90. return "User(id=" + this.getId() + ", phone=" + this.getPhone() + ", status=" + this.getStatus() + ")";
  91. }
  92. }

9. @Value

@Value 将字段都变成不可变类型:使用final修饰, 同时还包含@ToString、@EqualsAndHashCode、@AllArgsConstructor 、@Getter(注意只有Getter没有Setter)

  1. @Value
  2. public class User {
  3. private Long id;
  4. private String username;
  5. }
  6. public final class User {
  7. private final Long id;
  8. private final String username;
  9. public User(Long id, String username) {
  10. this.id = id;
  11. this.username = username;
  12. }
  13. public Long getId() {
  14. return this.id;
  15. }
  16. public String getUsername() {
  17. return this.username;
  18. }
  19. public boolean equals(Object o) {
  20. if(o == this) {
  21. return true;
  22. } else if(!(o instanceof User)) {
  23. return false;
  24. } else {
  25. User other = (User)o;
  26. Long this$id = this.getId();
  27. Long other$id = other.getId();
  28. if(this$id == null) {
  29. if(other$id != null) {
  30. return false;
  31. }
  32. } else if(!this$id.equals(other$id)) {
  33. return false;
  34. }
  35. String this$username = this.getUsername();
  36. String other$username = other.getUsername();
  37. if(this$username == null) {
  38. if(other$username != null) {
  39. return false;
  40. }
  41. } else if(!this$username.equals(other$username)) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. }
  47. public int hashCode() {
  48. boolean PRIME = true;
  49. byte result = 1;
  50. Long $id = this.getId();
  51. int result1 = result * 59 + ($id == null?43:$id.hashCode());
  52. String $username = this.getUsername();
  53. result1 = result1 * 59 + ($username == null?43:$username.hashCode());
  54. return result1;
  55. }
  56. public String toString() {
  57. return "User(id=" + this.getId() + ", username=" + this.getUsername() + ")";
  58. }
  59. }

10. @Log

生成log对象,用于记录日志,可以通过topic属性来设置getLogger(String name)方法的参数 例如 @Log4j(topic = “com.xxx.entity.User”),默认是类的全限定名,即 类名.class,log支持以下几种:

  • @Log java.util.logging.Logger
  • @Log4j org.apache.log4j.Logger
  • @Log4j2 org.apache.logging.log4j.Logger
  • @Slf4j org.slf4j.Logger
  • @XSlf4j org.slf4j.ext.XLogger
  • @CommonsLog org.apache.commons.logging.Log
  • @JBossLog org.jboss.logging.Logger

    @Log

    1. private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());

    @Log4j

    1. private static final Logger log = org.apache.log4j.Logger.Logger.getLogger(UserService.class);

    @Log4j2

    1. private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);

    @Slf4j

    1. private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);

    @XSlf4j

    1. private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

    @CommonsLog

    1. private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);

    @JBossLog

    1. private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);

    @Log
    public class UserService {

    1. public void addUser(){
    2. log.info("add user");
    3. }

    }

  1. import java.util.logging.Logger;
  2. public class UserService {
  3. private static final Logger log = Logger.getLogger(UserService.class.getName());
  4. public UserService() {
  5. }
  6. }

11. @SneakyThrows

使用try catch 来捕获异常, 默认捕获的是Throwable异常,也可以设置要捕获的异常

  1. public class User {
  2. @SneakyThrows
  3. public void sleep(){
  4. Thread.sleep(1000);
  5. }
  6. @SneakyThrows(InterruptedException.class)
  7. public void sleep2() {
  8. Thread.sleep(1000);
  9. }
  10. }
  11. public class User {
  12. public User() {
  13. }
  14. public void sleep() {
  15. try {
  16. Thread.sleep(1000L);
  17. } catch (Throwable var2) {
  18. throw var2;
  19. }
  20. }
  21. public void sleep2() {
  22. try {
  23. Thread.sleep(1000L);
  24. } catch (InterruptedException var2) {
  25. throw var2;
  26. }
  27. }
  28. public static void main(String[] args) {
  29. }
  30. }

12. @Synchronized

给方法加上同步锁

  1. public class User {
  2. private final Object readLock = new Object();
  3. @Synchronized
  4. public static void foo(){
  5. System.out.println();
  6. }
  7. @Synchronized
  8. public void bar(){
  9. System.out.println();
  10. }
  11. @Synchronized("readLock")
  12. public void test() {
  13. System.out.println();
  14. }
  15. }
  16. public class User {
  17. private static final Object $LOCK = new Object[0];
  18. private final Object $lock = new Object[0];
  19. private final Object readLock = new Object();
  20. public User() {
  21. }
  22. public static void foo() {
  23. Object var0 = $LOCK;
  24. synchronized($LOCK) {
  25. System.out.println();
  26. }
  27. }
  28. public void bar() {
  29. Object var1 = this.$lock;
  30. synchronized(this.$lock) {
  31. System.out.println();
  32. }
  33. }
  34. public void test() {
  35. Object var1 = this.readLock;
  36. synchronized(this.readLock) {
  37. System.out.println();
  38. }
  39. }
  40. }

13. @Cleanup

主要用来修饰 IO 流相关类, 会在 finally 代码块中对该资源进行 close();

  1. public class CleanupExample {
  2. public static void main(String[] args) throws IOException {
  3. @Cleanup InputStream in = new FileInputStream(args[0]);
  4. @Cleanup OutputStream out = new FileOutputStream(args[1]);
  5. byte[] b = new byte[10000];
  6. while (true) {
  7. int r = in.read(b);
  8. if (r == -1) break;
  9. out.write(b, 0, r);
  10. }
  11. }
  12. }
  13. public class CleanupExample {
  14. public static void main(String[] args) throws IOException {
  15. InputStream in = new FileInputStream(args[0]);
  16. try {
  17. OutputStream out = new FileOutputStream(args[1]);
  18. try {
  19. byte[] b = new byte[10000];
  20. while (true) {
  21. int r = in.read(b);
  22. if (r == -1) break;
  23. out.write(b, 0, r);
  24. }
  25. } finally {
  26. if (out != null) {
  27. out.close();
  28. }
  29. }
  30. } finally {
  31. if (in != null) {
  32. in.close();
  33. }
  34. }
  35. }
  36. }

14. @Getter(lazy = true)

@Getter(lazy = true)
标注字段为懒加载字段,懒加载字段在创建对象时不会进行初始化,而是在第一次访问的时候才会初始化,后面再次访问也不会重复初始化

  1. public class User {
  2. private final List<String> cityList = getCityFromCache();
  3. private List<String> getCityFromCache() {
  4. System.out.println("get city from cache ...");
  5. return new ArrayList<>();
  6. }
  7. }
  8. public static void main(String[] args) {
  9. User user = new User(); // 出事化对象时会会执行getCityFromCache()方法
  10. }
  11. public class User {
  12. @Getter(lazy = true)
  13. private final List<String> cityList = getCityFromCache();
  14. private List<String> getCityFromCache() {
  15. System.out.println("get city from cache ...");
  16. return new ArrayList<>();
  17. }
  18. }
  19. public class User {
  20. private final AtomicReference<Object> cityList = new AtomicReference();
  21. public User() {
  22. }
  23. private List<String> getCityFromCache() {
  24. System.out.println("get city from cache ...");
  25. return new ArrayList();
  26. }
  27. public List<String> getCityList() {
  28. Object value = this.cityList.get();
  29. if(value == null) {
  30. AtomicReference var2 = this.cityList;
  31. synchronized(this.cityList) {
  32. value = this.cityList.get();
  33. if(value == null) {
  34. List actualValue = this.getCityFromCache();
  35. value = actualValue == null?this.cityList:actualValue;
  36. this.cityList.set(value);
  37. }
  38. }
  39. }
  40. return (List)((List)(value == this.cityList?null:value));
  41. }
  42. }

@Getter(lazy = true):为懒加载字段生成一个Getter方法

  1. public static void main(String[] args) {
  2. User user = new User(); // 创建对象时不会初始化懒加载的字段
  3. List<String> cityList = user.getCityList(); // 只有第一次访问属性时才会去初始化
  4. cityList = user.getCityList(); // 第二次就不会再次初始化了
  5. }

15. @Wither

提供了给final字段赋值的一种方法

  1. public class User {
  2. @Wither
  3. private final String country;
  4. public User(String country) {
  5. this.country = country;
  6. }
  7. }
  8. public class User {
  9. private final String country;
  10. public User(String country) {
  11. this.country = country;
  12. }
  13. public User withCountry(String country) {
  14. return this.country == country?this:new User(country);
  15. }
  16. }

16. @Builder

@Builder注释为你的类生成复杂的构建器API。

  1. @Builder
  2. public class User {
  3. private Long id;
  4. private String phone;
  5. }
  6. public class User {
  7. private Long id;
  8. private String phone;
  9. User(Long id, String phone) {
  10. this.id = id;
  11. this.phone = phone;
  12. }
  13. public static User.UserBuilder builder() {
  14. return new User.UserBuilder();
  15. }
  16. public static class UserBuilder {
  17. private Long id;
  18. private String phone;
  19. UserBuilder() {
  20. }
  21. public User.UserBuilder id(Long id) {
  22. this.id = id;
  23. return this;
  24. }
  25. public User.UserBuilder phone(String phone) {
  26. this.phone = phone;
  27. return this;
  28. }
  29. public User build() {
  30. return new User(this.id, this.phone);
  31. }
  32. public String toString() {
  33. return "User.UserBuilder(id=" + this.id + ", phone=" + this.phone + ")";
  34. }
  35. }
  36. }

17. @Delegate

为List类型的字段生成一大堆常用的方法,其实这些方法都是List中的方法
注意:一个类中只能使用一个@Delegate注解,因为使用多个会生成多个size()方法,从而会编译报错。

  1. public class User {
  2. @Delegate
  3. private List<String> address;
  4. }
  5. public class User {
  6. private List<String> address;
  7. public User() {
  8. }
  9. public int size() {
  10. return this.address.size();
  11. }
  12. public boolean isEmpty() {
  13. return this.address.isEmpty();
  14. }
  15. public boolean contains(Object arg0) {
  16. return this.address.contains(arg0);
  17. }
  18. public Iterator<String> iterator() {
  19. return this.address.iterator();
  20. }
  21. public Object[] toArray() {
  22. return this.address.toArray();
  23. }
  24. public <T> T[] toArray(T[] arg0) {
  25. return this.address.toArray(arg0);
  26. }
  27. public boolean add(String arg0) {
  28. return this.address.add(arg0);
  29. }
  30. public boolean remove(Object arg0) {
  31. return this.address.remove(arg0);
  32. }
  33. public boolean containsAll(Collection<?> arg0) {
  34. return this.address.containsAll(arg0);
  35. }
  36. public boolean addAll(Collection<? extends String> arg0) {
  37. return this.address.addAll(arg0);
  38. }
  39. public boolean addAll(int arg0, Collection<? extends String> arg1) {
  40. return this.address.addAll(arg0, arg1);
  41. }
  42. public boolean removeAll(Collection<?> arg0) {
  43. return this.address.removeAll(arg0);
  44. }
  45. public boolean retainAll(Collection<?> arg0) {
  46. return this.address.retainAll(arg0);
  47. }
  48. public void replaceAll(UnaryOperator<String> arg0) {
  49. this.address.replaceAll(arg0);
  50. }
  51. public void sort(Comparator<? super String> arg0) {
  52. this.address.sort(arg0);
  53. }
  54. public void clear() {
  55. this.address.clear();
  56. }
  57. public String get(int arg0) {
  58. return (String)this.address.get(arg0);
  59. }
  60. public String set(int arg0, String arg1) {
  61. return (String)this.address.set(arg0, arg1);
  62. }
  63. public void add(int arg0, String arg1) {
  64. this.address.add(arg0, arg1);
  65. }
  66. public String remove(int arg0) {
  67. return (String)this.address.remove(arg0);
  68. }
  69. public int indexOf(Object arg0) {
  70. return this.address.indexOf(arg0);
  71. }
  72. public int lastIndexOf(Object arg0) {
  73. return this.address.lastIndexOf(arg0);
  74. }
  75. public ListIterator<String> listIterator() {
  76. return this.address.listIterator();
  77. }
  78. public ListIterator<String> listIterator(int arg0) {
  79. return this.address.listIterator(arg0);
  80. }
  81. public List<String> subList(int arg0, int arg1) {
  82. return this.address.subList(arg0, arg1);
  83. }
  84. public Spliterator<String> spliterator() {
  85. return this.address.spliterator();
  86. }
  87. }

lombok.config

lombok.config配置文件是通过一些设置来控制代码生成的规则或者称之为习惯,配置文件的位置应放在src/mian/java,不要放置在src/main/resources。

注意配置文件和要使用注解的类要在同一套代码中,要么同时在src/main/java 要么同时在 src/test/java中

lombok.config

  1. #lombok 默认对boolean类型字段生成的get方法使用is前缀, 通过此配置则使用get前缀,默认: false
  2. lombok.getter.noIsPrefix=true
  3. #默认的set方法返回void设置为true返回调用对象本身,这样方便使用链式来继续调用方法,默认: false
  4. lombok.accessors.chain=true
  5. #如果设置为true get和set方法将不带get,set前缀, 直接以字段名为方法名, 默认: false
  6. lombok.accessors.fluent=true
  7. #设置log类注解返回的字段名称,默认: log
  8. lombok.log.fieldName=logger
  9. @Log4j
  10. @Getter @Setter
  11. public class User {
  12. private String username;
  13. private boolean vip;
  14. private boolean isOldUser;
  15. }
  16. public class User {
  17. private static final Logger logger = Logger.getLogger(User.class);
  18. private String username;
  19. private boolean vip;
  20. private boolean isOldUser;
  21. public User() {
  22. }
  23. public String username() {
  24. return this.username;
  25. }
  26. public boolean vip() {
  27. return this.vip;
  28. }
  29. public boolean isOldUser() {
  30. return this.isOldUser;
  31. }
  32. public User username(String username) {
  33. this.username = username;
  34. return this;
  35. }
  36. public User vip(boolean vip) {
  37. this.vip = vip;
  38. return this;
  39. }
  40. public User isOldUser(boolean isOldUser) {
  41. this.isOldUser = isOldUser;
  42. return this;
  43. }
  44. }

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Lombok使用

    文章转载自[xxxxx龙果学院的开源项目][xxxxx] Lombok使用 Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代

    相关 使用Lombok

    使用Lombok lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码(比如setter代码和getter代码)的工具。

    相关 Lombok使用示例详情

    简介 Lombok是一个可以通过注解来帮助我们简化消除一些必须有但显得很臃肿的Java代码的一种工具,通过使用对应的注解,可以在编译源码的时候动态添加源码。 例如在实体

    相关 Lombok使用

    官方介绍如下:Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。官方

    相关 Lombok使用

    前言 `Lombok` 是一种 `Java™` 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO)。它通过注解实现这一目的。

    相关 Lombok使用

    在实际的编码过程中,想必大家都写过很多机械的固定化的代码(如POJO的getter/setter/toString/构造器;异常处理;I/O流的关闭;日志打印等等),这些代码的