Lombok 的使用

Lombok作为一个奇淫巧技,感觉知道的人还是挺少的
Lombok的一个作用就是不需要写getter和setter方法了,日后修改字段名,也不用修改getter和setter方法,lombok会屏蔽掉这一点

@Data 的使用

@Data 是在class类定义上添加的注解

  1. @Data
  2. public class Persion{
  3. private String name;
  4. private Date birthDay;
  5. private String habbit;
  6. }

代码在IDEA里面表现的代码结构为

class 文件反编译之后的结果

  1. package main.java.com.ditmark.lombok;
  2. import java.util.Date;
  3. public class Persion {
  4. private String name;
  5. private Date birthDay;
  6. private String hobbit;
  7. public Persion() {
  8. }
  9. public String getName() {
  10. return this.name;
  11. }
  12. public Date getBirthDay() {
  13. return this.birthDay;
  14. }
  15. public String getHobbit() {
  16. return this.hobbit;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public void setBirthDay(Date birthDay) {
  22. this.birthDay = birthDay;
  23. }
  24. public void setHobbit(String hobbit) {
  25. this.hobbit = hobbit;
  26. }
  27. public boolean equals(Object o) {
  28. if(o == this) {
  29. return true;
  30. } else if(!(o instanceof Persion)) {
  31. return false;
  32. } else {
  33. Persion other = (Persion)o;
  34. if(!other.canEqual(this)) {
  35. return false;
  36. } else {
  37. label47: {
  38. Object this$name = this.getName();
  39. Object other$name = other.getName();
  40. if(this$name == null) {
  41. if(other$name == null) {
  42. break label47;
  43. }
  44. } else if(this$name.equals(other$name)) {
  45. break label47;
  46. }
  47. return false;
  48. }
  49. Object this$birthDay = this.getBirthDay();
  50. Object other$birthDay = other.getBirthDay();
  51. if(this$birthDay == null) {
  52. if(other$birthDay != null) {
  53. return false;
  54. }
  55. } else if(!this$birthDay.equals(other$birthDay)) {
  56. return false;
  57. }
  58. Object this$hobbit = this.getHobbit();
  59. Object other$hobbit = other.getHobbit();
  60. if(this$hobbit == null) {
  61. if(other$hobbit != null) {
  62. return false;
  63. }
  64. } else if(!this$hobbit.equals(other$hobbit)) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. }
  70. }
  71. protected boolean canEqual(Object other) {
  72. return other instanceof Persion;
  73. }
  74. public int hashCode() {
  75. int PRIME = true;
  76. int result = 1;
  77. Object $name = this.getName();
  78. int result = result * 59 + ($name == null?43:$name.hashCode());
  79. Object $birthDay = this.getBirthDay();
  80. result = result * 59 + ($birthDay == null?43:$birthDay.hashCode());
  81. Object $hobbit = this.getHobbit();
  82. result = result * 59 + ($hobbit == null?43:$hobbit.hashCode());
  83. return result;
  84. }
  85. public String toString() {
  86. return "Persion(name=" + this.getName() + ", birthDay=" + this.getBirthDay() + ", hobbit=" + this.getHobbit() + ")";
  87. }
  88. }

Lombok还可以在循环的时候使用,强制循环中使用final 类型的对象,避免对象在循环过程中进行误操作.

  1. public static void main(String args){
  2. val list= new ArrayList<String>();
  3. list.add("A");
  4. list.add("B");
  5. list.add("C");
  6. list.add("D");
  7. for(val str: list){
  8. System.out.println(str);
  9. }
  10. val map = new HashMap<String,String>();
  11. for(val key : map.keySet()){
  12. System.out.println(key);
  13. }
  14. for(val entry : map.entrySet()){
  15. System.out.println(entry.getKey());
  16. }
  17. }

class 反编译之后,并没有发现final 可怕…也许是版本的差异?使用的版本为lombok 1.16.16

  1. public static void main(String args) {
  2. ArrayList<String> list = new ArrayList();
  3. list.add("A");
  4. list.add("B");
  5. list.add("C");
  6. list.add("D");
  7. Iterator var2 = list.iterator();
  8. while(var2.hasNext()) {
  9. String str = (String)var2.next();
  10. System.out.println(str);
  11. }
  12. HashMap<String, String> map = new HashMap();
  13. Iterator var6 = map.keySet().iterator();
  14. while(var6.hasNext()) {
  15. String key = (String)var6.next();
  16. System.out.println(key);
  17. }
  18. var6 = map.entrySet().iterator();
  19. while(var6.hasNext()) {
  20. Entry<String, String> entry = (Entry)var6.next();
  21. System.out.println((String)entry.getKey());
  22. }
  23. }

Lombok在流资源的开启和关闭也可以使用,避免写一堆close catch代码

posted on 2017-05-23 23:09  凤坡  阅读(304)  评论(0编辑  收藏  举报