lombok

Lombok

作用:幫使用者提高編碼效率,減少重複與冗餘的代碼

原理:ASM 動態修改class文件


配置

maven

依賴

     <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.8</version>
    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

編譯

     <build>
      <plugins>
          <plugin>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok-maven-plugin</artifactId>
              <version>1.16.6.1</version>
          </plugin>
      </plugins>
    </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

idea

  • 安裝插件

常用註解:

java bean相關

@Setter

  • 功能

    生成setter方法

  • 源碼

    @Setter
        public class LombokDemo {

            private Integer id;
            private String name;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 編譯後

  package xyz.mrwood.study.lombok;

  public class LombokDemo {
      private Integer id;
      private String name;

      public LombokDemo() {
      }

      public void setId(Integer id) {
          this.id = id;
      }

      public void setName(String name) {
          this.name = name;
      }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

@Getter

  • 功能

    生成getter方法

  • 源碼

@Getter
public class LombokDemo {

  private Integer id;
  private String name;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 編譯後
package xyz.mrwood.study.lombok;

public class LombokDemo {
    private Integer id;
    private String name;

    public LombokDemo() {
    }

    public Integer getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

@ToString

  • 功能

    生成toString方法

  • 源碼

    @ToString
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      public LombokDemo() {
      }
    
      public String toString() {
          return "LombokDemo(id=" + this.id + ", name=" + this.name + ")";
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

@Getter(lazy = true)

  • 功能

    懶加載屬性

  • 注意:

    這個與上面@Getter不同,那個是修飾在類上的,也可以修飾在屬性上。如果有lazy=true只能修飾在屬性,並且還要是private final修飾,限制很大

  • 編碼

    public class LombokDemo {
    
      @Getter(lazy = true) private final List<Integer> ids = Arrays.asList(1, 2, 3, 4);
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.concurrent.atomic.AtomicReference;
    
    public class LombokDemo {
      private final AtomicReference<Object> ids = new AtomicReference();
      private String name;
    
      public LombokDemo() {
      }
    
      public List<Integer> getIds() {
          Object value = this.ids.get();
          if(value == null) {
              AtomicReference var2 = this.ids;
              synchronized(this.ids) {
                  value = this.ids.get();
                  if(value == null) {
                      List actualValue = Arrays.asList(new Integer[]{Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4)});
                      value = actualValue == null?this.ids:actualValue;
                      this.ids.set(value);
                  }
              }
          }
    
          return (List)((List)(value == this.ids?null:value));
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

@EqualsAndHashCode

  • 功能

    生成equals方法與hashCode方法

  • 源碼

@EqualsAndHashCode
public class LombokDemo {

    private Integer id;
    private String name;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 編譯後
package xyz.mrwood.study.lombok;

public class LombokDemo {
    private Integer id;
    private String name;

    public LombokDemo() {
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof LombokDemo)) {
            return false;
        } else {
            LombokDemo other = (LombokDemo)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                Integer this$id = this.id;
                Integer other$id = other.id;
                if(this$id == null) {
                    if(other$id != null) {
                        return false;
                    }
                } else if(!this$id.equals(other$id)) {
                    return false;
                }

                String this$name = this.name;
                String other$name = other.name;
                if(this$name == null) {
                    if(other$name != null) {
                        return false;
                    }
                } else if(!this$name.equals(other$name)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof LombokDemo;
    }

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        Integer $id = this.id;
        int result1 = result * 59 + ($id == <span class="hljs-keyword">null</span>?<span class="hljs-number">43</span>:$id.hashCode());
        String $name = this.name;
        result1 = result1 * 59 + ($name == <span class="hljs-keyword">null</span>?<span class="hljs-number">43</span>:$name.hashCode());
        return result1;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

@NoAragsConstructor

  • 功能

    添加一個無參構造函數

  • 注意

    這個註解在沒有其它有參構造函數的情況下使用意義不大,因爲在這種情況下java默認會添加一個無參構造函數

  • 源碼

    @NoArgsConstructor
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      public LombokDemo() {
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

@AllArgsConstructor

  • 功能

    添加一個所有參數的構造函數

  • 源碼

    @AllArgsConstructor
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.beans.ConstructorProperties;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      @ConstructorProperties({"id", "name"})
      public LombokDemo(Integer id, String name) {
          this.id = id;
          this.name = name;
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

@RequiredArgsConstructor

  • 功能

    生成一個包含必填參數的構造函數

  • 注意

    要與@NonNull 搭配使用,該註解修飾的屬性就是必填參數

  • 源碼

    @RequiredArgsConstructor
    public class LombokDemo {
    
      @NonNull private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.beans.ConstructorProperties;
    import lombok.NonNull;
    
    public class LombokDemo {
      @NonNull
      private Integer id;
      private String name;
    
      @ConstructorProperties({"id"})
      public LombokDemo(@NonNull Integer id) {
          if(id == null) {
              throw new NullPointerException("id");
          } else {
              this.id = id;
          }
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

@Date

  • 功能

    這是一個綜合註解了,等於同時使用

    @Getter@Setter@ToString@EqualsAndHashCode,@RequiredArgsConstructor

  • 源碼

    @Data
    public class LombokDemo {
    
      @NonNull private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      public LombokDemo() {
      }
    
      public Integer getId() {
          return this.id;
      }
    
      public String getName() {
          return this.name;
      }
    
      public void setId(Integer id) {
          this.id = id;
      }
    
      public void setName(String name) {
          this.name = name;
      }
    
      public boolean equals(Object o) {
          if(o == this) {
              return true;
          } else if(!(o instanceof LombokDemo)) {
              return false;
          } else {
              LombokDemo other = (LombokDemo)o;
              if(!other.canEqual(this)) {
                  return false;
              } else {
                  Integer this$id = this.getId();
                  Integer other$id = other.getId();
                  if(this$id == null) {
                      if(other$id != null) {
                          return false;
                      }
                  } else if(!this$id.equals(other$id)) {
                      return false;
                  }
    
                  String this$name = this.getName();
                  String other$name = other.getName();
                  if(this$name == null) {
                      if(other$name != null) {
                          return false;
                      }
                  } else if(!this$name.equals(other$name)) {
                      return false;
                  }
    
                  return true;
              }
          }
      }
    
      protected boolean canEqual(Object other) {
          return other instanceof LombokDemo;
      }
    
      public int hashCode() {
          boolean PRIME = true;
          byte result = 1;
          Integer $id = this.getId();
          int result1 = result * 59 + ($id == <span class="hljs-keyword">null</span>?<span class="hljs-number">43</span>:$id.hashCode());
          String $name = this.getName();
          result1 = result1 * 59 + ($name == <span class="hljs-keyword">null</span>?<span class="hljs-number">43</span>:$name.hashCode());
          return result1;
      }
    
      public String toString() {
          return "LombokDemo(id=" + this.getId() + ", name=" + this.getName() + ")";
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

@Value

  • 功能

    不可變類的@Date, 他會默認給屬性加上final

  • 源碼

    @Value
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.beans.ConstructorProperties;
    
    public final class LombokDemo {
      private final Integer id;
      private final String name;
    
      @ConstructorProperties({"id", "name"})
      public LombokDemo(Integer id, String name) {
          this.id = id;
          this.name = name;
      }
    
      public Integer getId() {
          return this.id;
      }
    
      public String getName() {
          return this.name;
      }
    
      public boolean equals(Object o) {
          if(o == this) {
              return true;
          } else if(!(o instanceof LombokDemo)) {
              return false;
          } else {
              LombokDemo other = (LombokDemo)o;
              Integer this$id = this.getId();
              Integer other$id = other.getId();
              if(this$id == null) {
                  if(other$id != null) {
                      return false;
                  }
              } else if(!this$id.equals(other$id)) {
                  return false;
              }
    
              String this$name = this.getName();
              String other$name = other.getName();
              if(this$name == null) {
                  if(other$name != null) {
                      return false;
                  }
              } else if(!this$name.equals(other$name)) {
                  return false;
              }
    
              return true;
          }
      }
    
      public int hashCode() {
          boolean PRIME = true;
          byte result = 1;
          Integer $id = this.getId();
          int result1 = result * 59 + ($id == <span class="hljs-keyword">null</span>?<span class="hljs-number">43</span>:$id.hashCode());
          String $name = this.getName();
          result1 = result1 * 59 + ($name == <span class="hljs-keyword">null</span>?<span class="hljs-number">43</span>:$name.hashCode());
          return result1;
      }
    
      public String toString() {
          return "LombokDemo(id=" + this.getId() + ", name=" + this.getName() + ")";
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

@Accessors

  • 功能

    這個註解要搭配@Getter與@Setter使用,用來修改默認的setter與getter方法的形式

  • 注意

    @Accessors有三個參數可以使用

    1. chain 鏈式的形式
    2. fluent 流式的形式
    3. prefix 生成指定前綴的屬性的getter與setter方法,並且生成的getter與setter方法時會去除前綴
  • 源碼 chain = true

    @Accessors(chain = true)
    @Setter
    @Getter
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 編譯後 chain = true

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      public LombokDemo() {
      }
    
      public LombokDemo setId(Integer id) {
          this.id = id;
          return this;
      }
    
      public LombokDemo setName(String name) {
          this.name = name;
          return this;
      }
    
      public Integer getId() {
          return this.id;
      }
    
      public String getName() {
          return this.name;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 源碼 fluent = true

    @Accessors(fluent = true)
    @Setter
    @Getter
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 編譯後 fluent = true

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      public LombokDemo() {
      }
    
      public LombokDemo id(Integer id) {
          this.id = id;
          return this;
      }
    
      public LombokDemo name(String name) {
          this.name = name;
          return this;
      }
    
      public Integer id() {
          return this.id;
      }
    
      public String name() {
          return this.name;
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 源碼 prefix = "xxx"

    @Accessors(prefix = "xxx")
    @Setter
    @Getter
    public class LombokDemo {
    
      private Integer xxxId;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 編譯後 prefix = "xxx"

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer xxxId;
      private String name;
    
      public LombokDemo() {
      }
    
      public void setId(Integer xxxId) {
          this.xxxId = xxxId;
      }
    
      public Integer getId() {
          return this.xxxId;
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17


其它註解:

日誌相關

@Log4j

  • 源碼

    @Log4j
    public class LombokDemo {
    
      private Integer xxxId;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import org.apache.log4j.Logger;
    
    public class LombokDemo {
      private static final Logger log = Logger.getLogger(LombokDemo.class);
      private Integer xxxId;
      private String name;
    
      public LombokDemo() {
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

@CommonsLog

  • 源碼

    @CommonsLog
    public class LombokDemo {
    
      private Integer xxxId;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    public class LombokDemo {
      private static final Log log = LogFactory.getLog(LombokDemo.class);
      private Integer xxxId;
      private String name;
    
      public LombokDemo() {
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

@Log

  • 源碼

    @Log
    public class LombokDemo {
    
      private Integer xxxId;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.util.logging.Logger;
    
    public class LombokDemo {
      private static final Logger log = Logger.getLogger(LombokDemo.class.getName());
      private Integer xxxId;
      private String name;
    
      public LombokDemo() {
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

@Log4j2

  • 源碼

    @Log4j2
    public class LombokDemo {
    
      private Integer xxxId;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class LombokDemo {
      private static final Logger log = LogManager.getLogger(LombokDemo.class);
      private Integer xxxId;
      private String name;
    
      public LombokDemo() {
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

@Slf4j

  • 源碼

    @Slf4j
    public class LombokDemo {
    
      private Integer xxxId;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class LombokDemo {
      private static final Logger log = LoggerFactory.getLogger(LombokDemo.class);
      private Integer xxxId;
      private String name;
    
      public LombokDemo() {
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

設計模式相關

@Builder

  • 功能

    通過建造者模塊來生成bean

  • 源碼

    @Builder
    public class LombokDemo {
    
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      LombokDemo(Integer id, String name) {
          this.id = id;
          this.name = name;
      }
    
      public static LombokDemo.LombokDemoBuilder builder() {
          return new LombokDemo.LombokDemoBuilder();
      }
    
      public static class LombokDemoBuilder {
          private Integer id;
          private String name;
    
          LombokDemoBuilder() {
          }
    
          public LombokDemo.LombokDemoBuilder id(Integer id) {
              this.id = id;
              return this;
          }
    
          public LombokDemo.LombokDemoBuilder name(String name) {
              this.name = name;
              return this;
          }
    
          public LombokDemo build() {
              return new LombokDemo(this.id, this.name);
          }
    
          public String toString() {
              return "LombokDemo.LombokDemoBuilder(id=" + this.id + ", name=" + this.name + ")";
          }
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

@Delegate

  • 功能

    @Delegate註釋的屬性,會把這個屬性對象的公有非靜態方法合到當前類

  • 注意

    公共 非靜態方法

  • 源碼

    public class LombokDemo {
    
      @Delegate
      private Integer id;
      private String name;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    public class LombokDemo {
      private Integer id;
      private String name;
    
      public LombokDemo() {
      }
    
      public byte byteValue() {
          return this.id.byteValue();
      }
    
      public short shortValue() {
          return this.id.shortValue();
      }
    
      public int intValue() {
          return this.id.intValue();
      }
    
      public long longValue() {
          return this.id.longValue();
      }
    
      public float floatValue() {
          return this.id.floatValue();
      }
    
      public double doubleValue() {
          return this.id.doubleValue();
      }
    
      public int compareTo(Integer arg0) {
          return this.id.compareTo(arg0);
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

工具相關

@Cleanup

  • 功能

    關閉流

  • 注意

    關閉流的方式有點怪異,而且沒有在finally裏面關閉,如果出現異常的就不會關閉了

  • 源碼

    public class LombokDemo {
    
      public void test() throws IOException {
    
          @Cleanup InputStream inputStream = new FileInputStream("xxx.txt");
    
      }
    
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Collections;
    
    public class LombokDemo {
      public LombokDemo() {
      }
    
      public void test() throws IOException {
          FileInputStream inputStream = new FileInputStream("xxx.txt");
          if(Collections.singletonList(inputStream).get(0) != null) {
              inputStream.close();
          }
    
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

@Synchronized

  • 功能

    給方法加一個同步塊

  • 源碼

    public class LombokDemo {
    
      @Synchronized
      public void test() throws IOException {
    
          System.out.println("test");
    
      }
    
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.io.IOException;
    
    public class LombokDemo {
      private final Object $lock = new Object[0];
    
      public LombokDemo() {
      }
    
      public void test() throws IOException {
          Object var1 = this.$lock;
          synchronized(this.$lock) {
              System.out.println("test");
          }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

@SneakyThrows

  • 功能

    忽略異常

  • 源碼

    public class LombokDemo {
    
      @SneakyThrows
      public void test() {
    
          String s = new String("test".getBytes(), "utf-8");
    
      }
    
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import java.io.IOException;
    
    public class LombokDemo {
      private final Object $lock = new Object[0];
    
      public LombokDemo() {
      }
    
      public void test() throws IOException {
          Object var1 = this.$lock;
          synchronized(this.$lock) {
              System.out.println("test");
          }
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

@NonNull


  • 功能


設置不能爲空的參數


  • 源碼

  • public class LombokDemo {
    
      public void test(@NonNull String val) {
    
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    import lombok.NonNull;
    
    public class LombokDemo {
        public LombokDemo() {
        }
    
        public void test(@NonNull String val) {
            if(val == null) {
                throw new NullPointerException("val");
            }
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    @UtilityClass

  • 功能


    把普通類轉爲工具類


  • 源碼

      @UtilityClass
      public class LombokDemo {
    
          private Integer id = 1;
          private String name = "kiwi";
    
          public void util(){
    
              System.out.println("xxx");
          }
      }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 編譯後

    package xyz.mrwood.study.lombok;
    
    public final class LombokDemo {
      private static Integer id = Integer.valueOf(1);
      private static String name = "kiwi";
    
      public static void util() {
          System.out.println("xxx");
      }
    
      private LombokDemo() {
          throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14



    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章