关于Lombok这个东西,论坛里已经有人介绍过,我也是通过别人帖子了解到这个小工具:
http://www.iteye.com/topic/798305
不过作者以免写Getter/Setter为题来介绍Lombok的优势,看了后面的评论之后,觉得这个工具意义不大.
后来在博客在线看到一篇文章,期中提到了Lombok工具.这个工具已经被国外创业者所使用,随之对其产生了很浓厚的兴趣.
<Java开源建站工具>http://www.jobbole.com/entry.php/1233
关于如何介绍Lombok这个工具,自己想了半天,觉得实在比不过作者在首页提供的视频介绍:
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 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 进行了反向生成, 看看这些注释究竟省了多少代码:
// 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);
}

Lombok是一款Java库,可通过注解自动生成Getter/Setter等代码,简化开发过程。本文介绍了Lombok的主要注解及其使用方法,并展示了它如何减少样板代码。
1140

被折叠的 条评论
为什么被折叠?



