Lombok简化Java代码 - Code less do More

10 篇文章 0 订阅
2 篇文章 0 订阅

关于Lombok这个东西,论坛里已经有人介绍过,我也是通过别人帖子了解到这个小工具:

http://www.iteye.com/topic/798305

不过作者以免写Getter/Setter为题来介绍Lombok的优势,看了后面的评论之后,觉得这个工具意义不大.

后来在博客在线看到一篇文章,期中提到了Lombok工具.这个工具已经被国外创业者所使用,随之对其产生了很浓厚的兴趣.

<Java开源建站工具>http://www.jobbole.com/entry.php/1233

 

关于如何介绍Lombok这个工具,自己想了半天,觉得实在比不过作者在首页提供的视频介绍:

http://projectlombok.org/

 

ok~ 写了这么多Lombok到底是什么东西,他具体能带来什么便利呢?

Lombok主要是提供一套注解,根据注解在编译时生成相关代码,目前提供如下注解:

 

 

@Getter / @Setter 永远不用再写  public int getFoo() {return foo;}. @Getter(lazy=true) Laziness is a virtue! @ToString
Lombok会根据field自动生成 toString  方法,这个对调试来说很方便! 这个注解生成的toString格式:

 

LombokUser(id=001, name=wity_lv, email=wity_lv@sample.com)
 

 

@EqualsAndHashCode 生成  hashCode and  equals 方法. @NoArgsConstructor@RequiredArgsConstructor and @AllArgsConstructor Constructors made to order: Generates constructors that take no arguments, one argument per final / non-null field, or one argument for every field. @Data All together now: A shortcut for  @ToString@EqualsAndHashCode@Getter on all fields, and  @Setter on all non-final fields, and  @RequiredArgsConstructor! @Cleanup Automatic resource management: Call your  close() methods safely with no hassle. @Synchronized synchronized done right: Don't expose your locks. @SneakyThrows To boldly throw checked exceptions where no one has thrown them before! @Log 支持多种Log工具, 我平时用@Log4j注解 val Finally! Hassle-free final local variables. @Delegate Don't lose your composition.

如何使用?

1. 从项目首页下载lombok.jar

2. 双击lombok.jar, 将其安装到eclipse中(该项目需要jdk1.6+的环境)


 

3. 将Lombok.jar添加到classpath中

4.  编码看看:

 

项目结构:


 

 

package lv.sample.lombok;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Log4j
public class LombokUser {
	
	private String id = null;
	private String name = null;
	private String email = null;
	
	public static void main(String[] args) {
		log.info("test the lombok bean");
		
		LombokUser u = new LombokUser("001", "wity_lv", "wity_lv@sample.com");
		System.out.println(u.toString());
		
		LombokUser u2 = new LombokUser("001", "wity_lv", "wity_lv@sample.com");
		System.out.println(u.equals(u2));
	}
}

 
 console out 写道

2011-08-24 11:57:36 test the lombok bean
2011-08-24 11:57:36 LombokUser(id=001, name=wity_lv, email=wity_lv@sample.com)
2011-08-24 11:57:36 true
 

What is happenning???

使用javap看看编译后的代码:


在eclipse outline中显示的结构

 


 

到这里 ~ 这些仅仅只是作者提供一些方便的注释, 当然我们可以fork这个项目的 GitHub, 研究内部原理,构建适合自己团队的Lombok:

https://github.com/rzwitserloot/lombok

 

2011-08-26 10:00 补充  User中究竟省了多少代码:

关于上面写的User方法,我用作者提供的 delombok 进行了反向生成, 看看这些注释究竟省了多少代码:

 

delombok 写道
java -jar -cp lib/log4j-1.2.8.jar lib/lombok.jar delombok src -d ./src-delomboked
 

 

// Generated by delombok at Fri Aug 26 18:12:13 CST 2011
package lv.sample.lombok;

public class LombokUser {
	private String id = null;
	private String name = null;
	private String email = null;
	
	public static void main(String[] args) {
		log.info("test the lombok bean");
		LombokUser u = new LombokUser("001", "wity_lv", "wity_lv@sample.com");
		log.info(u.toString());
		LombokUser u2 = new LombokUser("001", "wity_lv", "wity_lv@sample.com");
		log.info(u.equals(u2));
	}
	
	@java.lang.SuppressWarnings("all")
	public String getId() {
		return this.id;
	}
	
	@java.lang.SuppressWarnings("all")
	public String getName() {
		return this.name;
	}
	
	@java.lang.SuppressWarnings("all")
	public String getEmail() {
		return this.email;
	}
	
	@java.lang.SuppressWarnings("all")
	public void setId(final String id) {
		this.id = id;
	}
	
	@java.lang.SuppressWarnings("all")
	public void setName(final String name) {
		this.name = name;
	}
	
	@java.lang.SuppressWarnings("all")
	public void setEmail(final String email) {
		this.email = email;
	}
	
	@java.lang.Override
	@java.lang.SuppressWarnings("all")
	public boolean equals(final java.lang.Object o) {
		if (o == this) return true;
		if (!(o instanceof LombokUser)) return false;
		final LombokUser other = (LombokUser)o;
		if (!other.canEqual((java.lang.Object)this)) return false;
		if (this.getId() == null ? other.getId() != null : !this.getId().equals((java.lang.Object)other.getId())) return false;
		if (this.getName() == null ? other.getName() != null : !this.getName().equals((java.lang.Object)other.getName())) return false;
		if (this.getEmail() == null ? other.getEmail() != null : !this.getEmail().equals((java.lang.Object)other.getEmail())) return false;
		return true;
	}
	
	@java.lang.SuppressWarnings("all")
	public boolean canEqual(final java.lang.Object other) {
		return other instanceof LombokUser;
	}
	
	@java.lang.Override
	@java.lang.SuppressWarnings("all")
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = result * PRIME + (this.getId() == null ? 0 : this.getId().hashCode());
		result = result * PRIME + (this.getName() == null ? 0 : this.getName().hashCode());
		result = result * PRIME + (this.getEmail() == null ? 0 : this.getEmail().hashCode());
		return result;
	}
	
	@java.lang.Override
	@java.lang.SuppressWarnings("all")
	public java.lang.String toString() {
		return "LombokUser(id=" + this.getId() + ", name=" + this.getName() + ", email=" + this.getEmail() + ")";
	}
	
	@java.lang.SuppressWarnings("all")
	public LombokUser() {
	}
	
	@java.beans.ConstructorProperties({"id", "name", "email"})
	@java.lang.SuppressWarnings("all")
	public LombokUser(final String id, final String name, final String email) {
		this.id = id;
		this.name = name;
		this.email = email;
	}
	private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LombokUser.class);
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值