lombok的简单使用

1.安装插件

1.找到lombok-1.18.12.jar

java -jar ./lombok-1.18.12.jar

lombok的简单使用_第1张图片

 2.选择Eclipse对应的安装位置之后安装

3.安装成功在eclipse目录会生成lombok.jar

同时eclipse.ini也会增加一行:

-javaagent:E:\Eclipse\eclipse-jee-mars-1-win32-x86_64\eclipse-jee-mars-1-win32-x86_64\eclipse\lombok.jar

4.重启eclipse即可

 二、lombok简单使用

pom文件引入依赖:

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

1.测试常用注解@Getter、@Setter、@EqualsAndHashCode、@ToString

package cn.qlq.lombok;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@EqualsAndHashCode
@ToString
public class User {

    private long id;

    private String username;

}

 

jd反编译后查看代码:

package cn.qlq.lombok;

public class User
{
  private long id;
  private String username;
  
  public String getUsername()
  {
    return this.username;
  }
  
  public long getId()
  {
    return this.id;
  }
  
  public void setUsername(String username)
  {
    this.username = username;
  }
  
  public void setId(long id)
  {
    this.id = id;
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof User;
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = 1;long $id = getId();result = result * 59 + (int)($id ^ $id >>> 32);Object $username = getUsername();result = result * 59 + ($username == null ? 43 : $username.hashCode());return result;
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof User)) {
      return false;
    }
    User other = (User)o;
    if (!other.canEqual(this)) {
      return false;
    }
    if (getId() != other.getId()) {
      return false;
    }
    Object this$username = getUsername();Object other$username = other.getUsername();return this$username == null ? other$username == null : this$username.equals(other$username);
  }
  
  public String toString()
  {
    return "User(id=" + getId() + ", username=" + getUsername() + ")";
  }
}

 

2.@NoArgsConstructor 、@AllArgsConstructor分别用于无参构造和有参构造

package cn.qlq.lombok;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@NoArgsConstructor // 无参构造
@AllArgsConstructor // 有参构造
public class User {

    private long id;

    private String username;

}

 

反编译查看代码:

package cn.qlq.lombok;

public class User
{
  private long id;
  private String username;
  
  public User() {}
  
  public User(long id, String username)
  {
    this.id = id;this.username = username;
  }
}

 

3.@Data 相当于@Getter、@Setter、@EqualsAndHashCode、@ToString

package cn.qlq.lombok;

import lombok.Data;

@Data
public class User {

    private long id;

    private String username;

}

 

反编译查看:

package cn.qlq.lombok;

public class User
{
  private long id;
  private String username;
  
  public String toString()
  {
    return "User(id=" + getId() + ", username=" + getUsername() + ")";
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = 1;long $id = getId();result = result * 59 + (int)($id ^ $id >>> 32);Object $username = getUsername();result = result * 59 + ($username == null ? 43 : $username.hashCode());return result;
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof User;
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof User)) {
      return false;
    }
    User other = (User)o;
    if (!other.canEqual(this)) {
      return false;
    }
    if (getId() != other.getId()) {
      return false;
    }
    Object this$username = getUsername();Object other$username = other.getUsername();return this$username == null ? other$username == null : this$username.equals(other$username);
  }
  
  public void setUsername(String username)
  {
    this.username = username;
  }
  
  public void setId(long id)
  {
    this.id = id;
  }
  
  public String getUsername()
  {
    return this.username;
  }
  
  public long getId()
  {
    return this.id;
  }
}

 

查看@Data源码如下:其实还有@RequiredArgsConstructor ,只是这个注解一般不用。

/** http://www.cpupk.com/decompiler">Eclipse Class Decompiler plugin, Copyright (c) 2017 Chen Chao. */
package lombok;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
 * all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
 * 

* Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}. *

* Complete documentation is found at https://projectlombok.org/features/Data">the project lombok features page for @Data. * * @see Getter * @see Setter * @see RequiredArgsConstructor * @see ToString * @see EqualsAndHashCode * @see lombok.Value */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface Data { /** * If you specify a static constructor name, then the generated constructor will be private, and * instead a static factory method is created that other classes can use to create instances. * We suggest the name: "of", like so: * *

     *     public @Data(staticConstructor = "of") class Point { final int x, y; }
     * 
* * Default: No static constructor, instead the normal constructor is public. * *
@return Name of static 'constructor' method to generate (blank = generate a normal constructor). */ String staticConstructor() default ""; }

 

需要注意:@Data生成toString和equals、hashCode时不会选择父类的字段,如下:

父类:

package cn.qlq.lombok;

import java.io.Serializable;

import lombok.Data;

@Data
public abstract class AbstractSequenceEntity implements Serializable {

    private static final long serialVersionUID = 7304379208787774552L;

    protected Long id;
}

反编译查看:

package cn.qlq.lombok;

import java.io.Serializable;

public abstract class AbstractSequenceEntity
  implements Serializable
{
  private static final long serialVersionUID = 7304379208787774552L;
  protected Long id;
  
  public String toString()
  {
    return "AbstractSequenceEntity(id=" + getId() + ")";
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = 1;Object $id = getId();result = result * 59 + ($id == null ? 43 : $id.hashCode());return result;
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof AbstractSequenceEntity;
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof AbstractSequenceEntity)) {
      return false;
    }
    AbstractSequenceEntity other = (AbstractSequenceEntity)o;
    if (!other.canEqual(this)) {
      return false;
    }
    Object this$id = getId();Object other$id = other.getId();return this$id == null ? other$id == null : this$id.equals(other$id);
  }
  
  public void setId(Long id)
  {
    this.id = id;
  }
  
  public Long getId()
  {
    return this.id;
  }
}

 

子类如下:

package cn.qlq.lombok;

import lombok.Data;

@Data
public class User extends AbstractSequenceEntity {

    private static final long serialVersionUID = 1487903039867324395L;

    private String username;

}

 

反编译查看:没有从父类继承的属性

package cn.qlq.lombok;

public class User
  extends AbstractSequenceEntity
{
  private static final long serialVersionUID = 1487903039867324395L;
  private String username;
  
  public String toString()
  {
    return "User(username=" + getUsername() + ")";
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = 1;Object $username = getUsername();result = result * 59 + ($username == null ? 43 : $username.hashCode());return result;
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof User;
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof User)) {
      return false;
    }
    User other = (User)o;
    if (!other.canEqual(this)) {
      return false;
    }
    Object this$username = getUsername();Object other$username = other.getUsername();return this$username == null ? other$username == null : this$username.equals(other$username);
  }
  
  public void setUsername(String username)
  {
    this.username = username;
  }
  
  public String getUsername()
  {
    return this.username;
  }
}

 

解决办法:

package cn.qlq.lombok;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
// callSuper: 是否包含父类的属性
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class User extends AbstractSequenceEntity {

    private static final long serialVersionUID = 1487903039867324395L;

    private String username;

}

反编译查看:

package cn.qlq.lombok;

public class User
  extends AbstractSequenceEntity
{
  private static final long serialVersionUID = 1487903039867324395L;
  private String username;
  
  public String getUsername()
  {
    return this.username;
  }
  
  public void setUsername(String username)
  {
    this.username = username;
  }
  
  public String toString()
  {
    return "User(super=" + super.toString() + ", username=" + getUsername() + ")";
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof User)) {
      return false;
    }
    User other = (User)o;
    if (!other.canEqual(this)) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }
    Object this$username = getUsername();Object other$username = other.getUsername();return this$username == null ? other$username == null : this$username.equals(other$username);
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof User;
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = super.hashCode();Object $username = getUsername();result = result * 59 + ($username == null ? 43 : $username.hashCode());return result;
  }
}

 

4.@Slf4j可以整合日志

原来我们使用日志的方式如下:

package cn.qlq.lombok;

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

public class User {

    private static final Logger log = LoggerFactory.getLogger(User.class);

}

 

注解方式如下:

package cn.qlq.lombok;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class User {


}

反编译查看:

package cn.qlq.lombok;

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

public class User
{
  private static final Logger log = LoggerFactory.getLogger(User.class);
}

 

5.@Builder用于生成加个构造者模式

package cn.qlq.lombok;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class User  {

    private long id;
    
    private String username;

}

 

测试代码:

package cn.qlq.lombok;

public class Client {

    public static void main(String[] args) {
        User.UserBuilder userBuilder = new User.UserBuilder();
        User user = userBuilder.id(5).username("admin").build();
        System.out.println(user);
    }
}

结果:

User(id=5, username=admin)

 

反编译查看:(User$UserBuilder.class是内部类编译后生成的cass文件)

package cn.qlq.lombok;

public class User
{
  private long id;
  private String username;
  
  public String toString()
  {
    return "User(id=" + getId() + ", username=" + getUsername() + ")";
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = 1;long $id = getId();result = result * 59 + (int)($id ^ $id >>> 32);Object $username = getUsername();result = result * 59 + ($username == null ? 43 : $username.hashCode());return result;
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof User;
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof User)) {
      return false;
    }
    User other = (User)o;
    if (!other.canEqual(this)) {
      return false;
    }
    if (getId() != other.getId()) {
      return false;
    }
    Object this$username = getUsername();Object other$username = other.getUsername();return this$username == null ? other$username == null : this$username.equals(other$username);
  }
  
  public void setUsername(String username)
  {
    this.username = username;
  }
  
  public void setId(long id)
  {
    this.id = id;
  }
  
  public String getUsername()
  {
    return this.username;
  }
  
  public long getId()
  {
    return this.id;
  }
  
  public static class UserBuilder
  {
    private long id;
    private String username;
    
    public String toString()
    {
      return "User.UserBuilder(id=" + this.id + ", username=" + this.username + ")";
    }
    
    public User build()
    {
      return new User(this.id, this.username);
    }
    
    public UserBuilder username(String username)
    {
      this.username = username;return this;
    }
    
    public UserBuilder id(long id)
    {
      this.id = id;return this;
    }
  }
  
  public static UserBuilder builder()
  {
    return new UserBuilder();
  }
  
  User(long id, String username)
  {
    this.id = id;this.username = username;
  }
}

 

package cn.qlq.lombok;

public class User$UserBuilder
{
  private long id;
  private String username;
  
  public String toString()
  {
    return "User.UserBuilder(id=" + this.id + ", username=" + this.username + ")";
  }
  
  public User build()
  {
    return new User(this.id, this.username);
  }
  
  public UserBuilder username(String username)
  {
    this.username = username;return this;
  }
  
  public UserBuilder id(long id)
  {
    this.id = id;return this;
  }
}

 

补充一些其他不常用的注解:

@Cleanup : 可以关闭流
@Synchronized : 加个同步锁

@SneakyThrows : 等同于try/catch 捕获异常

@NonNull : 如果给参数加个这个注解 参数为null会抛出空指针异常

@Value : 注解和@Data类似,区别在于它会把所有成员变量默认定义为private final修饰,并且不会生成set方法。

 

你可能感兴趣的:(lombok的简单使用)