使用lombok提高编码效率

Lombok简介

 lombok项目通过增加处理程序使我们的java语言更加刺激(简洁和快速)。(lombok其实就是在我们进行编译事加一层处理的机制,通过各种注释,大量简化类生成时的步骤)。

 

常用注解

@Data :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter:注解在属性上;为属性提供 setting 方法
@Getter:注解在属性上;为属性提供 getting 方法
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@Slf4j : 注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
@Accessors(chain = true) 支持链式调用的:
@Builder 建筑者模式

@NonNull自检是否为空值

--------------------- 本文来自 汪小哥 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012881904/article/details/64438046?utm_source=copy 

@Data

编译前:

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wangji on 2017/3/20.
 */

@Slf4j
@Data
public class Person {
    private String name;
    private String old;

    public static void main(String[] args) {

        Person px =new Person();
        px.setName("xx");
        px.setOld("xx");
        log.info(px.getName());
    }
}

编译后的结果,可以看到给予了许多的get,set and toString等等

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Person {
    private static final Logger log = LoggerFactory.getLogger(Person.class);
    private String name;
    private String old;

    public static void main(String[] args) {
        Person px = new Person();
        px.setName("xx");
        px.setOld("xx");
        log.info(px.getName());
    }

    public Person() {
    }

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

    public String getOld() {
        return this.old;
    }

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

    public void setOld(String old) {
        this.old = old;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof Person)) {
            return false;
        } else {
            Person other = (Person)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                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;
                }

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?43:$name.hashCode());
        String $old = this.getOld();
        result1 = result1 * 59 + ($old == null?43:$old.hashCode());
        return result1;
    }

    public String toString() {
        return "Person(name=" + this.getName() + ", old=" + this.getOld() + ")";
    }
}
  •  

支持链式调用 @Data @Accessors(chain = true) 配合使用

类似这种类型:

Student student = new Student()
        .setAge(24)
        .setName("zs");

 

  

编译前:

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wangji on 2017/3/20.
 */

@Slf4j
@Data
@Accessors(chain = true)
public class Person {
    private String name;
    private String old;

    public static void main(String[] args) {

        Person px =new Person().setOld("xxx").setName("xxx");
        log.info(px.getName());
    }
} 

编译后的代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Person {
    private static final Logger log = LoggerFactory.getLogger(Person.class);
    private String name;
    private String old;

    public static void main(String[] args) {
        Person px = (new Person()).setOld("xxx").setName("xxx");
        log.info(px.getName());
    }

    public Person() {
    }

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

    public String getOld() {
        return this.old;
    }

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

    public Person setOld(String old) {
        this.old = old;
        return this;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof Person)) {
            return false;
        } else {
            Person other = (Person)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                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;
                }

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?43:$name.hashCode());
        String $old = this.getOld();
        result1 = result1 * 59 + ($old == null?43:$old.hashCode());
        return result1;
    }

    public String toString() {
        return "Person(name=" + this.getName() + ", old=" + this.getOld() + ")";
    }
}

@val @var

使用Lombok ,java也能够像javascript一样使用弱类型定义变量了

val注解变量申明是final类型 var注解变量是非final类型

 

  1.  
    import java.util.ArrayList;
  2.  
    import java.util.HashMap;
  3.  
    import lombok.val;
  4.  
     
  5.  
    public class ValExample {
  6.  
    public String example() {
  7.  
    val example = new ArrayList<String>();
  8.  
    example.add("Hello, World!");
  9.  
    val foo = example.get(0);
  10.  
    return foo.toLowerCase();
  11.  
    }
  12.  
     
  13.  
    public void example2() {
  14.  
    val map = new HashMap<Integer, String>();
  15.  
    map.put(0, "zero");
  16.  
    map.put(5, "five");
  17.  
    for (val entry : map.entrySet()) {
  18.  
    System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
  19.  
    }
  20.  
    }
  21.  
    }
翻译后 
  1.  
    import java.util.ArrayList;
  2.  
    import java.util.HashMap;
  3.  
    import java.util.Map;
  4.  
     
  5.  
    public class ValExample {
  6.  
    public String example() {
  7.  
    final ArrayList<String> example = new ArrayList<String>();
  8.  
    example.add("Hello, World!");
  9.  
    final String foo = example.get(0);
  10.  
    return foo.toLowerCase();
  11.  
    }
  12.  
     
  13.  
    public void example2() {
  14.  
    final HashMap<Integer, String> map = new HashMap<Integer, String>();
  15.  
    map.put(0, "zero");
  16.  
    map.put(5, "five");
  17.  
    for (final Map.Entry<Integer, String> entry : map.entrySet()) {
  18.  
    System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
  19.  
    }
  20.  
    }
  21.  
    }
 

 



2.@NonNull

在方法或构造函数的参数上使用@NonNull,lombok将生成一个空值检查语句。 

  1.  
    import lombok.NonNull;
  2.  
     
  3.  
    public class NonNullExample extends Something {
  4.  
    private String name;
  5.  
     
  6.  
    public NonNullExample(@NonNull Person person) {
  7.  
    super("Hello");
  8.  
    this.name = person.getName();
  9.  
    }
  10.  
    }
翻译后

 

  1.  
    import lombok.NonNull;
  2.  
     
  3.  
    public class NonNullExample extends Something {
  4.  
    private String name;
  5.  
     
  6.  
    public NonNullExample(@NonNull Person person) {
  7.  
    super("Hello");
  8.  
    if (person == null) {
  9.  
    throw new NullPointerException("person");
  10.  
    }
  11.  
    this.name = person.getName();
  12.  
    }
  13.  
    }
 

                            本文来自 汪小哥和再见尼罗河 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012881904/article/details/64438046?utm_source=copy 

                                      https://blog.csdn.net/v2sking/article/details/73431364?utm_source=copy

 

posted @ 2018-10-09 09:18  Yjf_C-o  阅读(639)  评论(0编辑  收藏  举报