走进Java接口测试之效率插件lombok

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zuozewei/article/details/85109439

引言

我们在开发测试代码过程中,通常都会定义大量的 JavaBean ,然后通过IDE 去生成其属性的构造器、getter、setter、equals、hashcode、toString 方法,当要增加属性或者对某个属性进行改变时,比如命名、类型等,都需要重新去生成上面提到的这些方法。这样重复的劳动没有任何意义,Lombok 里面的注解可以轻松解决这些问题。

lombok简介

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

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

lombok 实现原理

自从Java 6起,javac 就支持“JSR 269 Pluggable Annotation Processing API”规范,只要程序实现了该API,就能在javac运行的时候得到调用。Lombok就是一个实现了"JSR 269 API"的程序。

在使用 javac 的过程中,它产生作用的具体流程如下:

  • javac对源代码进行分析,生成一棵抽象语法树(AST)
  • javac编译过程中调用实现了JSR 269的Lombok 程序此时
  • Lombok 就对第一步骤得到的 AST 进行处理,找到 Lombok 注解所在类对应的语法树(AST),然后修改该语法树(AST),增加 Lombok 注解定义的相应树节点
  • javac使用修改后的抽象语法树(AST)生成字节码文件

lombok特点

  • 简化冗余的 JavaBean 代码,使得实体文件很简洁。
  • 大大提高J avaBean 中方法的执行效率,省去重复的步骤

lombok安装

本文演示 IntelliJ IDEA

  • 打开 File > Settings > Plugins
  • 点击 Browse repositories
  • 搜索 Lombok Plugin
  • 点击 Install plugin
    在这里插入图片描述
    点击File-- Settings设置界面,开启Annocation Processors
    在这里插入图片描述
    开启该项是为了让 Lombok 注解在编译阶段起到作用。
    最后,重启 IntelliJ IDEA

lombok使用

添加maven依赖

<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.18.4</version>
		<scope>provided</scope>
	</dependency>

常用注解介绍

@Data 标签,生成 getter/setter toString() 等方法
@NonNull` : 让你不在担忧并且爱上 NullPointerException
@CleanUp : 自动资源管理:不用再在 finally 中添加资源的 close 方法
@Setter/@Getter : 自动生成set和get方法
@ToString : 自动生成 toString 方法
@EqualsAndHashcode : 从对象的字段中生成 hashCode 和 equals 的实现
@NoArgsConstructor/@RequiredArgsConstructor/@AllArgsConstructor
自动生成构造方法
@Data : 自动生成 set/get 方法,toString 方法,equals 方法,hashCode方法,不带参数的构造方法
@Value : 用于注解 final 类
@Builder : 产生复杂的构建器 api 类
@SneakyThrows : 异常处理(谨慎使用)
@Synchronized : 同步方法安全的转化
@Getter(lazy=true) :
@Log : 支持各种logger对象,使用时用对应的注解,如:@Log4j

更多注解说明,可查看:https://projectlombok.org/features/index.html

简单使用示例

使用lombok

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Demo {

    String code;
    String name;

}

等同于

public class Demo {
        String code;
        String name;

        public static DemoBuilder builder() {
            return new DemoBuilder();
        }

        public String getCode() {
            return this.code;
        }

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

        public void setCode(String code) {
            this.code = code;
        }

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

        public boolean equals(Object o) {
            if (o == this)
                return true;
            if (!(o instanceof Demo))
                return false;
            Demo other = (Demo) o;
            if (!other.canEqual(this))
                return false;
            Object this$code = getCode();
            Object other$code = other.getCode();
            if (this$code == null ? other$code != null : !this$code.equals(other$code))
                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 Demo;
        }

        public int hashCode() {
            int PRIME = 59;
            int result = 1;
            Object $code = getCode();
            result = result * 59 + ($code == null ? 43 : $code.hashCode());
            Object $name = getName();
            return result * 59 + ($name == null ? 43 : $name.hashCode());
        }

        public String toString() {
            return "Demo(code=" + getCode() + ", name=" + getName() + ")";
        }

        public Demo() {
        }

        public Demo(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public static class DemoBuilder {
            private String code;
            private String name;

            public DemoBuilder code(String code) {
                this.code = code;
                return this;
            }

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

            public Demo build() {
                return new Demo(this.code, this.name);
            }

            public String toString() {
                return "Demo.DemoBuilder(code=" + this.code + ", name=" + this.name + ")";
            }
        }
    }

使用@Slf4j注解

@Slf4j
public class LogExampleOther {
  
  public static void main(String... args) {
    log.error("Something else is wrong here");
  }
}

常规的写法

public class LogExampleOther {
  private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);
  
  public static void main(String... args) {
    log.error("Something else is wrong here");
  }
}

@NonNull注解,Null 即是罪恶

public class NonNullExample extends Something {
  private String name;
  
  public NonNullExample(@NonNull Person person) {
    super("Hello");
    this.name = person.getName();
  }
}

常规的写法

public class NonNullExample extends Something {
  private String name;
  
  public NonNullExample(@NonNull Person person) {
    super("Hello");
    if (person == null) {
      throw new NullPointerException("person");
    }
    this.name = person.getName();
  }
}

@Builder注解,builder 是现在比较推崇的一种构建值对象的方式。

@Builder
public class BuilderExample {
  private String name;
  private int age;
  @Singular private Set<String> occupations;
}

常规写法:

public class BuilderExample {
  private String name;
  private int age;
  private Set<String> occupations;
  
  BuilderExample(String name, int age, Set<String> occupations) {
    this.name = name;
    this.age = age;
    this.occupations = occupations;
  }
  
  public static BuilderExampleBuilder builder() {
    return new BuilderExampleBuilder();
  }
  
  public static class BuilderExampleBuilder {
    private String name;
    private int age;
    private java.util.ArrayList<String> occupations;
    
    BuilderExampleBuilder() {
    }
    
    public BuilderExampleBuilder name(String name) {
      this.name = name;
      return this;
    }
    
    public BuilderExampleBuilder age(int age) {
      this.age = age;
      return this;
    }
    
    public BuilderExampleBuilder occupation(String occupation) {
      if (this.occupations == null) {
        this.occupations = new java.util.ArrayList<String>();
      }
      
      this.occupations.add(occupation);
      return this;
    }
    
    public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
      if (this.occupations == null) {
        this.occupations = new java.util.ArrayList<String>();
      }

      this.occupations.addAll(occupations);
      return this;
    }
    
    public BuilderExampleBuilder clearOccupations() {
      if (this.occupations != null) {
        this.occupations.clear();
      }
      
      return this;
    }

    public BuilderExample build() {
      // complicated switch statement to produce a compact properly sized immutable set omitted.
      // go to https://projectlombok.org/features/Singular-snippet.html to see it.
      Set<String> occupations = ...;
      return new BuilderExample(name, age, occupations);
    }
    
    @java.lang.Override
    public String toString() {
      return "BuilderExample.BuilderExampleBuilder(name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
    }
  }
}

总之,省了多少事!!!

总结

以上注解可根据需要一起搭配使用!虽说轮子好,但是我们不仅要知其然,也要知其所以然!

猜你喜欢

转载自blog.csdn.net/zuozewei/article/details/85109439