程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> lombok在IntelliJ IDEA下的使用,lombokintellij

lombok在IntelliJ IDEA下的使用,lombokintellij

編輯:JAVA綜合教程

lombok在IntelliJ IDEA下的使用,lombokintellij


lombok是一款可以精減java代碼、提升開發人員生產效率的輔助工具,利用注解在編譯期自動生成setter/getter/toString()/constructor之類的代碼。代碼越少,意味著出bug的可能性越低。

官網地址:https://projectlombok.org/ 首頁有一段幾分鐘的演示視頻,看完就明白是怎麼回事了。

先來二段對比代碼:

這是用lombok後的java代碼:

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;
import java.io.*;
import java.util.ArrayList;

@Data
@Slf4j
@AllArgsConstructor
public class Something {

    private String name;
    private final String country;
    private final Object lockObj = new Object();

    public void sayHello(@NonNull String target) {
        String content = String.format("hello,%s", target);
        System.out.println(content);
        log.info(content);
    }

    public void addBalabala() {
        val list = new ArrayList<String>();
        list.add("haha");
        System.out.println(list.size());
    }

    @SneakyThrows(IOException.class)
    public void closeBalabala() {
        @Cleanup InputStream is = new ByteArrayInputStream("hello world".getBytes());
        System.out.println(is.available());
    }

    @Synchronized("lockObj")
    public void lockMethod() {
        System.out.println("test lock method");
    }
}

等效於下面這段java代碼:

import java.beans.ConstructorProperties;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Something {
    private static final Logger log = LoggerFactory.getLogger(Something.class);
    private String name;
    private final String country;
    private final Object lockObj = new Object();

    public void sayHello(@NonNull String target) {
        if(target == null) {
            throw new NullPointerException("target");
        } else {
            String content = String.format("hello,%s", new Object[]{target});
            System.out.println(content);
            log.info(content);
        }
    }

    public void addBalabala() {
        ArrayList list = new ArrayList();
        list.add("haha");
        System.out.println(list.size());
    }

    public void closeBalabala() {
        try {
            ByteArrayInputStream $ex = new ByteArrayInputStream("hello world".getBytes());

            try {
                System.out.println($ex.available());
            } finally {
                if(Collections.singletonList($ex).get(0) != null) {
                    $ex.close();
                }

            }

        } catch (IOException var6) {
            throw var6;
        }
    }

    public void lockMethod() {
        Object var1 = this.lockObj;
        synchronized(this.lockObj) {
            System.out.println("test lock method");
        }
    }

    public String getName() {
        return this.name;
    }

    public String getCountry() {
        return this.country;
    }

    public Object getLockObj() {
        return this.lockObj;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof Something)) {
            return false;
        } else {
            Something other = (Something)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    String this$name = this.getName();
                    String other$name = other.getName();
                    if(this$name == null) {
                        if(other$name == null) {
                            break label47;
                        }
                    } else if(this$name.equals(other$name)) {
                        break label47;
                    }

                    return false;
                }

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

                Object this$lockObj = this.getLockObj();
                Object other$lockObj = other.getLockObj();
                if(this$lockObj == null) {
                    if(other$lockObj != null) {
                        return false;
                    }
                } else if(!this$lockObj.equals(other$lockObj)) {
                    return false;
                }

                return true;
            }
        }
    }

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

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?0:$name.hashCode());
        String $country = this.getCountry();
        result1 = result1 * 59 + ($country == null?0:$country.hashCode());
        Object $lockObj = this.getLockObj();
        result1 = result1 * 59 + ($lockObj == null?0:$lockObj.hashCode());
        return result1;
    }

    public String toString() {
        return "Something(name=" + this.getName() + ", country=" + this.getCountry() + ", lockObj=" + this.getLockObj() + ")";
    }

    @ConstructorProperties({"name", "country"})
    public Something(String name, String country) {
        this.name = name;
        this.country = country;
    }
}

大概減少了2/3的代碼,各種注解的詳細用法,請參考:https://projectlombok.org/features/index.html

IDEA下使用時,可以通過插件的形式安裝,插件下載地址:https://github.com/mplushnikov/lombok-intellij-plugin/releases 

然後

Plugins -> Install plugin from disk... 選擇下載的zip包安裝,重啟idea即可。

另外,還有一個關鍵設置:

為了讓設置生效,建議再重啟一次idea,然後就可以開心的編碼了,可以ide裡可以直接看到生成的方法:(下圖中打紅圈的都是自動生成的)

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved