Advertisement
NLinker

Lombok NonNull annotation

May 24th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1.     @Value
  2.     @Builder
  3.     static class E {
  4.         @NonNull String x;
  5.         @NonNull Integer y;
  6.         @NonNull Long z;
  7.     }
  8.  
  9.     @Test(expectedExceptions = {NullPointerException.class})
  10.     public void testBuilder() throws Exception {
  11.         // the test shows how to suppress nulls in the immutable values
  12.         val e = E.builder()
  13.             .x(null)
  14.             .y(213)
  15.             .z(43782L)
  16.             .build();
  17.         System.out.println("e = " + e);
  18.     }
  19.  
  20. // the compiler outputs  the following bytecode: (the source is obtained by decompilation)
  21.  
  22.     static final class E {
  23.         @NonNull
  24.         private final String x;
  25.         @NonNull
  26.         private final Integer y;
  27.         @NonNull
  28.         private final Long z;
  29.  
  30.         E(@NonNull String x, @NonNull Integer y, @NonNull Long z) {
  31.             if(x == null) {
  32.                 throw new NullPointerException("x");
  33.             } else if(y == null) {
  34.                 throw new NullPointerException("y");
  35.             } else if(z == null) {
  36.                 throw new NullPointerException("z");
  37.             } else {
  38.                 this.x = x;
  39.                 this.y = y;
  40.                 this.z = z;
  41.             }
  42.         }
  43.  
  44.         public static MatchingTest.E.EBuilder builder() {
  45.             return new MatchingTest.E.EBuilder();
  46.         }
  47.  
  48.         @NonNull
  49.         public String getX() {
  50.             return this.x;
  51.         }
  52.  
  53.         @NonNull
  54.         public Integer getY() {
  55.             return this.y;
  56.         }
  57.  
  58.         @NonNull
  59.         public Long getZ() {
  60.             return this.z;
  61.         }
  62.  
  63.         public boolean equals(Object o) {
  64.             if(o == this) {
  65.                 return true;
  66.             } else if(!(o instanceof MatchingTest.E)) {
  67.                 return false;
  68.             } else {
  69.                 MatchingTest.E other;
  70.                 label44: {
  71.                     other = (MatchingTest.E)o;
  72.                     Object this$x = this.getX();
  73.                     Object other$x = other.getX();
  74.                     if(this$x == null) {
  75.                         if(other$x == null) {
  76.                             break label44;
  77.                         }
  78.                     } else if(this$x.equals(other$x)) {
  79.                         break label44;
  80.                     }
  81.  
  82.                     return false;
  83.                 }
  84.  
  85.                 Object this$y = this.getY();
  86.                 Object other$y = other.getY();
  87.                 if(this$y == null) {
  88.                     if(other$y != null) {
  89.                         return false;
  90.                     }
  91.                 } else if(!this$y.equals(other$y)) {
  92.                     return false;
  93.                 }
  94.  
  95.                 Object this$z = this.getZ();
  96.                 Object other$z = other.getZ();
  97.                 if(this$z == null) {
  98.                     if(other$z != null) {
  99.                         return false;
  100.                     }
  101.                 } else if(!this$z.equals(other$z)) {
  102.                     return false;
  103.                 }
  104.  
  105.                 return true;
  106.             }
  107.         }
  108.  
  109.         public int hashCode() {
  110.             int PRIME = true;
  111.             int result = 1;
  112.             Object $x = this.getX();
  113.             int result = result * 59 + ($x == null?43:$x.hashCode());
  114.             Object $y = this.getY();
  115.             result = result * 59 + ($y == null?43:$y.hashCode());
  116.             Object $z = this.getZ();
  117.             result = result * 59 + ($z == null?43:$z.hashCode());
  118.             return result;
  119.         }
  120.  
  121.         public String toString() {
  122.             return "MatchingTest.E(x=" + this.getX() + ", y=" + this.getY() + ", z=" + this.getZ() + ")";
  123.         }
  124.  
  125.         public static class EBuilder {
  126.             private String x;
  127.             private Integer y;
  128.             private Long z;
  129.  
  130.             EBuilder() {
  131.             }
  132.  
  133.             public MatchingTest.E.EBuilder x(String x) {
  134.                 this.x = x;
  135.                 return this;
  136.             }
  137.  
  138.             public MatchingTest.E.EBuilder y(Integer y) {
  139.                 this.y = y;
  140.                 return this;
  141.             }
  142.  
  143.             public MatchingTest.E.EBuilder z(Long z) {
  144.                 this.z = z;
  145.                 return this;
  146.             }
  147.  
  148.             public MatchingTest.E build() {
  149.                 return new MatchingTest.E(this.x, this.y, this.z);
  150.             }
  151.  
  152.             public String toString() {
  153.                 return "MatchingTest.E.EBuilder(x=" + this.x + ", y=" + this.y + ", z=" + this.z + ")";
  154.             }
  155.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement