lombok的使用

lombok是一個編譯插件,其能夠幫助咱們完成一些重複性可是又不得不寫的代碼,而且可使代碼變得更優雅。java

下面開始介紹其使用,首先是下載地址:https://projectlombok.org/download。其次是英文的介紹地址:https://projectlombok.org/features/all,這裏詳盡的介紹了各個註解的使用和做用。eclipse

1、安裝ui

由於我使用的是eclipse,因此只介紹eclipse的安裝,將下載下來的lombok.jar包放到你的eclipse安裝的目錄下,而後運行jar包,this

會彈出以下圖的框出來:插件

首先點擊specify location選中你的eclipse.exe,而後點擊install就安裝完成了,而後重啓eclipse就能夠了code

2、下面咱們結合代碼來看看效果,對象

首先定義一個本身的類:blog

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

一、@Setter、@Getter爲相應的類或字段添上set方法和get方法,且這兩個註解均可以設置value值已設置其可見性如:value=lombok.AccessLevel.PRIVATE;ip

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

編譯後的代碼爲:ci

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;
	}
}

二、@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() + ")";
	}
}

三、@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;
	}
}

四、@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;
	}
}

五、@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;
	}
}

六、@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;
	}
}

七、@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() + ")";
	}
}

 八、建造者模式能夠簡化對象的聲明和建立過程,可是實現建造者模式的代碼卻很繁多並且格式化,@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 + ")";
		}
	}

}

 九、當操做文件時可能最困擾咱們的就是流的關閉了,由於須要寫不少沒有必要的代碼這個時候@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還有一些其餘的註解可是我以爲上面的這些最有意義,完畢

相關文章
相關標籤/搜索