浏览 5527 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-08-26
这个内容在之前的帖子中已经有人讨论过,今天再做一更进一步的了解.
本篇会涉及如下三个注解:
如果你还不知道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
使用 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; } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |