这个内容在之前的帖子中已经有人讨论过,今天再做一更进一步的了解.
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");
}
}
}
注意: lombok的注解是可以直接用的, delombok工具只是用来查看生成的代码, 不需要将delombok生成的代码复制回工程
// 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;
}
}
@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", "[email protected]");
log.info(u.toString());
LombokUser u2 = new LombokUser("001", "wity_lv", "[email protected]");
log.info(u.equals(u2));
try {
u.setEmail(null);
} catch(NullPointerException ex) {
log.info("email could not be null: " + ex.getMessage());
}
}
}