lombok的使用

首页 编程分享 EXPERIENCE 正文

潘帅_阿尔法 转载 编程分享 2018-11-09 20:05:20

简介 ojectlombok.org/download。其次是英文的介绍地址:https://pro...


 

lombok是一个编译插件,其可以帮助我们完成一些重复性但是又不得不写的代码,并且可以使代码变得更优雅。

下面开始介绍其使用,首先是下载地址:https://projectlombok.org/download。其次是英文的介绍地址:https://projectlombok.org/features/all,这里详尽的介绍了各个注解的使用和作用。

一、安装

因为我使用的是eclipse,所以只介绍eclipse的安装,将下载下来的lombok.jar包放到你的eclipse安装的目录下,然后运行jar包,

会弹出如下图的框出来:

首先点击specify location选中你的eclipse.exe,然后点击install就安装完成了,然后重启eclipse就可以了

二、下面我们结合代码来看看效果,

首先定义一个自己的类:

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
public class User {
	private Long id;
	private String name;
	
}

1、@Setter、@Getter为相应的类或字段添上set方法和get方法,且这两个注解都可以设置value值已设置其可见性如:value=lombok.AccessLevel.PRIVATE;

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Setter
@Getter
public class User {
	private Long id;
	private String name;
	
}

编译后的代码为:

public class User {
	private Long id;
	private String name;

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

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

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

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

2、@ToString为类添加toString方法,其可以指定要添加到方法里的属性有哪些。

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Setter
@Getter
@ToString
public class User {
	private Long id;
	private String name;
	
}

编译后的代码为:

public class User {
	private Long id;
	private String name;

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

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

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

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

	public String toString() {
		return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
	}
}

3、@EqualsAndHashCode为类添上equal方法和hashCode方法,同样它也可以指定为哪些字段生成;

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Setter
@Getter
@ToString
@EqualsAndHashCode
public class User {
	private Long id;
	private String name;
	
}

编译后的代码为:

public class User {
	private Long id;
	private String name;

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

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

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

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

	public String toString() {
		return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
	}

	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;
		}
		Long this$id = this.getId();
		Long other$id = other.getId();
		if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
			return false;
		}
		String this$name = this.getName();
		String other$name = other.getName();
		if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
			return false;
		}
		return true;
	}

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

	public int hashCode() {
		int PRIME = 59;
		int result = 1;
		Long $id = this.getId();
		result = result * 59 + ($id == null ? 43 : $id.hashCode());
		String $name = this.getName();
		result = result * 59 + ($name == null ? 43 : $name.hashCode());
		return result;
	}
}

4、@NonNull设置属性不能为空否则会报错:

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Setter
@Getter
@ToString
@EqualsAndHashCode
public class User {
	@NonNull
	private Long id;
	private String name;
	
}

编译后的代码如下:

public class User {
	@NonNull
	private Long id;
	private String name;

	public void setId(@NonNull Long id) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
	}

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

	@NonNull
	public Long getId() {
		return this.id;
	}

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

	public String toString() {
		return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
	}

	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;
		}
		Long this$id = this.getId();
		Long other$id = other.getId();
		if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
			return false;
		}
		String this$name = this.getName();
		String other$name = other.getName();
		if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
			return false;
		}
		return true;
	}

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

	public int hashCode() {
		int PRIME = 59;
		int result = 1;
		Long $id = this.getId();
		result = result * 59 + ($id == null ? 43 : $id.hashCode());
		String $name = this.getName();
		result = result * 59 + ($name == null ? 43 : $name.hashCode());
		return result;
	}
}

5、@NoArgsConstructor,@AllArgsConstructor为类添上无参构造方法和全部参数的构造方法

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Setter
@Getter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class User {
	@NonNull
	private Long id;
	private String name;
	
}

编译后的代码为:

public class User {
	@NonNull
	private Long id;
	private String name;

	public void setId(@NonNull Long id) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
	}

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

	@NonNull
	public Long getId() {
		return this.id;
	}

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

	public String toString() {
		return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
	}

	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;
		}
		Long this$id = this.getId();
		Long other$id = other.getId();
		if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
			return false;
		}
		String this$name = this.getName();
		String other$name = other.getName();
		if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
			return false;
		}
		return true;
	}

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

	public int hashCode() {
		int PRIME = 59;
		int result = 1;
		Long $id = this.getId();
		result = result * 59 + ($id == null ? 43 : $id.hashCode());
		String $name = this.getName();
		result = result * 59 + ($name == null ? 43 : $name.hashCode());
		return result;
	}

	public User() {
	}

	public User(@NonNull Long id, String name) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
		this.name = name;
	}
}

6、@RequiredArgsConstructor为类中标注了@nonNull的属性生成构造方法

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Setter
@Getter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class User {
	@NonNull
	private Long id;
	private String name;
	
}

编译后的代码如下:

public class User {
	@NonNull
	private Long id;
	private String name;

	public void setId(@NonNull Long id) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
	}

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

	@NonNull
	public Long getId() {
		return this.id;
	}

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

	public String toString() {
		return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
	}

	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;
		}
		Long this$id = this.getId();
		Long other$id = other.getId();
		if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
			return false;
		}
		String this$name = this.getName();
		String other$name = other.getName();
		if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
			return false;
		}
		return true;
	}

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

	public int hashCode() {
		int PRIME = 59;
		int result = 1;
		Long $id = this.getId();
		result = result * 59 + ($id == null ? 43 : $id.hashCode());
		String $name = this.getName();
		result = result * 59 + ($name == null ? 43 : $name.hashCode());
		return result;
	}

	public User() {
	}

	public User(@NonNull Long id, String name) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
		this.name = name;
	}

	public User(@NonNull Long id) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
	}
}

7、@Data是@Getter,@Setter,@RequiredArgsConstructor,@ToString,@EqualsAndHashCode的集合体

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Data
public class User {
	@NonNull
	private Long id;
	private String name;
	
}

编译后的代码为:

public class User {
	@NonNull
	private Long id;
	private String name;

	public User(@NonNull Long id) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
	}

	@NonNull
	public Long getId() {
		return this.id;
	}

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

	public void setId(@NonNull Long id) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
	}

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

	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;
		}
		Long this$id = this.getId();
		Long other$id = other.getId();
		if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
			return false;
		}
		String this$name = this.getName();
		String other$name = other.getName();
		if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
			return false;
		}
		return true;
	}

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

	public int hashCode() {
		int PRIME = 59;
		int result = 1;
		Long $id = this.getId();
		result = result * 59 + ($id == null ? 43 : $id.hashCode());
		String $name = this.getName();
		result = result * 59 + ($name == null ? 43 : $name.hashCode());
		return result;
	}

	public String toString() {
		return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
	}
}

 8、建造者模式可以简化对象的声明和创建过程,但是实现建造者模式的代码却很繁多而且格式化,@Builder可以帮助我们:

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
@Builder
public class User {
	@NonNull
	private Long id;
	private String name;
	
}

编译后的代码如下:

public class User {
	@NonNull
	private Long id;
	private String name;

	User(@NonNull Long id, String name) {
		if (id == null) {
			throw new NullPointerException("id is marked @NonNull but is null");
		}
		this.id = id;
		this.name = name;
	}

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

	public static class UserBuilder {
		private Long id;
		private String name;

		UserBuilder() {
		}

		public UserBuilder id(Long id) {
			this.id = id;
			return this;
		}

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

		public User build() {
			return new User(this.id, this.name);
		}

		public String toString() {
			return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ")";
		}
	}

}

 9、当操作文件时可能最困扰我们的就是流的关闭了,因为需要写很多没有必要的代码这个时候@Cleanup可以帮助我们

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年7月15日下午4:49:18
 *
 */
public class User {
	private Long id;
	private String name;
	
	public static void main(String[] args) throws IOException {
        @Cleanup InputStream in = new FileInputStream(new File("E:\\123.txt"));
        @Cleanup OutputStream out = new FileOutputStream(new File("F:\\123.txt"));
        byte[] b = new byte[10000];
        while (true) {
            int r = in.read(b);
            if (r == -1) break;
            out.write(b, 0, r);
        }
    }
}

编译后的代码如下:

public class User {
	private Long id;
	private String name;

	public static void main(String[] args) throws IOException {
		FileInputStream in = new FileInputStream(new File("E:\\123.txt"));
		try {
			FileOutputStream out = new FileOutputStream(new File("F:\\123.txt"));
			try {
				int r;
				byte[] b = new byte[10000];
				while ((r = in.read(b)) != -1) {
					out.write(b, 0, r);
				}
			} finally {
				if (Collections.singletonList(out).get(0) != null) {
					out.close();
				}
			}
		} finally {
			if (Collections.singletonList(in).get(0) != null) {
				in.close();
			}
		}
	}
}

lombok还有一些其他的注解但是我觉得上面的这些最有意义,完毕

 

 

转载链接:https://my.oschina.net/u/3534905/blog/1853928


Tags:


本篇评论 —— 揽流光,涤眉霜,清露烈酒一口话苍茫。


    声明:参照站内规则,不文明言论将会删除,谢谢合作。


      最新评论




ABOUT ME

Blogger:袅袅牧童 | Arkin

Ido:PHP攻城狮

WeChat:nnmutong

Email:nnmutong@icloud.com

标签云