这个内容在之前的帖子中已经有人讨论过,今天再做一更进一步的了解.
本篇会涉及如下三个注解:
- @Getter/@Setter: 帮助我们对应的 getter/setter方法
- (Come on, 这个没什么意思, Hibernate Tools会生成, Eclipse 佷智能的好不拉! 是的, 从这个角度出发的确没太大吸引力, 各位看管姑且看之, @Data会更有用, 总是会有需要手写pojo的情况)
- @NonNull: 在setter方法中做非null判断,遇到null参数,会抛出NullPointerException
如果你还不知道Lombok是什么, 先看看这篇:
Lombok简化Java代码 - code less do more
关于这部分主题,官方文档在这里(E文强力的朋友也看这里)
http://projectlombok.org/features/GetterSetter.html
代码演示:
package lv.sample.lombok;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
@ToString
public class GetterSetterUser {
@Getter @Setter private int age = 25;
@Setter(AccessLevel.PROTECTED) private String address = "shanghai";
/*
* add a notnull constraint.
*/
@Getter @Setter @NonNull
private String email = null;
/**
* if the setName method has been declared,
* the Lombok does not generate it.
*/
@Setter @Getter
private Object name = null;
/**
* code by myself
* @param name
*/
public void setName(String name) {
this.name = name + " from my code";
}
public static void main(String[] args) {
// we test the getter/setter annotation
GetterSetterUser bean = new GetterSetterUser();
bean.setAge(20);
bean.setAddress("xi'an");
bean.setName("wity_lv,");
/*
* the toString() method has been instead of the Lombok @ToString
* annotation
*/
System.out.println(bean.toString());
/*
* set a null value to email
*/
try {
bean.setEmail(null);
} catch(NullPointerException e) {
System.out.println("phone could not be null value");
}
}
}
console out 写道
GetterSetterUser(age=20, address=xi'an, email=null, name=wity_lv, from my code)
email could not be null value
email could not be null value
使用 delombok工具反向生成代码, 看看使用注解能省多少代码:
注意: lombok的注解是可以直接用的, delombok工具只是用来查看生成的代码, 不需要将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 10:57:14 CST 2011
package lv.sample.lombok;
import lombok.NonNull;
public class GetterSetterUser {
private int age = 25;
private String address = "shanghai";
/*
* add a notnull constraint.
*/
@NonNull
private String email = null;
/**
* if the setName method has been declared,
* the Lombok does not generate it.
*/
private Object name = null;
/**
* code by myself
* @param name
*/
public void setName(String name) {
this.name = name + " from my code";
}
public static void main(String[] args) {
// we test the getter/setter annotation
GetterSetterUser bean = new GetterSetterUser();
bean.setAge(20);
bean.setAddress("xi\'an");
bean.setName("wity_lv,");
/*
* the toString() method has been instead of the Lombok @ToString
* annotation
*/
System.out.println(bean.toString());
/*
* set a null value to email
*/
try {
bean.setEmail(null);
} catch (NullPointerException e) {
System.out.println("phone could not be null value");
}
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "GetterSetterUser(age=" + this.getAge() + ", address=" + this.address + ", email=" + this.getEmail() + ", name=" + this.getName() + ")";
}
@java.lang.SuppressWarnings("all")
public int getAge() {
return this.age;
}
@java.lang.SuppressWarnings("all")
public void setAge(final int age) {
this.age = age;
}
@java.lang.SuppressWarnings("all")
protected void setAddress(final String address) {
this.address = address;
}
@NonNull
@java.lang.SuppressWarnings("all")
public String getEmail() {
return this.email;
}
@java.lang.SuppressWarnings("all")
public void setEmail(@NonNull final String email) {
if (email == null) throw new java.lang.NullPointerException("email");
this.email = email;
}
@java.lang.SuppressWarnings("all")
public Object getName() {
return this.name;
}
}
话说,为每个字段写@Getter @Setter有些麻烦, 这里有个@Data会省不少事:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Log4j
public class LombokUser {
private String id = null;
private String name = null;
@NonNull
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));
try {
u.setEmail(null);
} catch(NullPointerException ex) {
log.info("email could not be null: " + ex.getMessage());
}
}
}
console out 写道
2011-08-26 11:10:40 test the lombok bean
2011-08-26 11:10:40 LombokUser(id=001, name=wity_lv, email=wity_lv@sample.com)
2011-08-26 11:10:40 true
2011-08-26 11:10:40 email could not be null: email
2011-08-26 11:10:40 LombokUser(id=001, name=wity_lv, email=wity_lv@sample.com)
2011-08-26 11:10:40 true
2011-08-26 11:10:40 email could not be null: email