View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.equalsavoidnull;
2   import java.util.Map;
3   public class InputEqualsAvoidNull {
4   
5       public boolean equals(Object o) {
6       return false;
7       }
8       // another comment
9       /**
10       * methods that should get flagged
11       * @return
12       */
13      public void flagForEquals() {
14  
15          Object o = new Object();
16          String s = "pizza";
17  
18          o.equals("hot pizza")/*comment test*/;
19  
20          o.equals(s = "cold pizza");
21  
22          o.equals(((s = "cold pizza")));
23  
24          o.equals("cheese" + "ham" + "sauce");
25  
26          o.equals(("cheese" + "ham") + "sauce");
27  
28          o.equals((("cheese" + "ham")) + "sauce");
29      }
30  
31      /**
32       * methods that should get flagged
33       */
34      public void flagForEqualsIgnoreCase() {
35          String s = "pizza";
36  
37          s.equalsIgnoreCase("hot pizza");
38  
39          s.equalsIgnoreCase(s = "cold pizza");
40  
41          s.equalsIgnoreCase(((s = "cold pizza")));
42  
43          s.equalsIgnoreCase("cheese" + "ham" + "sauce");
44  
45          s.equalsIgnoreCase(("cheese" + "ham") + "sauce");
46  
47          s.equalsIgnoreCase((("cheese" + "ham")) + "sauce");
48      }
49  
50      /**
51       * methods that should get flagged
52       */
53      public void flagForBoth() {
54          Object o = new Object();
55          String s = "pizza";
56  
57          o.equals("hot pizza");
58  
59          o.equals(s = "cold pizza");
60  
61          o.equals(((s = "cold pizza")));
62  
63          o.equals("cheese" + "ham" + "sauce");
64  
65          o.equals(("cheese" + "ham") + "sauce");
66  
67          o.equals((("cheese" + "ham")) + "sauce");
68  
69          s.equalsIgnoreCase("hot pizza");
70  
71          s.equalsIgnoreCase(s = "cold pizza");
72  
73          s.equalsIgnoreCase(((s = "cold pizza")));
74  
75          s.equalsIgnoreCase("cheese" + "ham" + "sauce");
76  
77          s.equalsIgnoreCase(("cheese" + "ham") + "sauce");
78  
79          s.equalsIgnoreCase((("cheese" + "ham")) + "sauce");
80      }
81  
82  
83      /**
84       * methods that should not get flagged
85       *
86       * @return
87       */
88      public void noFlagForEquals() {
89          Object o = new Object();
90          String s = "peperoni";
91  
92          o.equals(s += "mushrooms");
93  
94          (s = "thin crust").equals("thick crust");
95  
96          (s += "garlic").equals("basil");
97  
98          ("Chicago Style" + "NY Style").equals("California Style" + "Any Style");
99  
100         equals("peppers");
101 
102         "onions".equals(o);
103 
104         o.equals(new Object());
105 
106         o.equals(equals(o));
107 
108         equals("yummy");
109 
110         new Object().equals("more cheese");
111 
112         InputEqualsAvoidNullOutter outter = new InputEqualsAvoidNullOutter();
113 
114         outter.new InputEqualsAvoidNullInner().equals("eat pizza and enjoy inner classes");
115     }
116 
117     /**
118      * methods that should not get flagged
119      */
120     public void noFlagForEqualsIgnoreCase() {
121         String s = "peperoni";
122         String s1 = "tasty";
123 
124         s.equalsIgnoreCase(s += "mushrooms");
125 
126         s1.equalsIgnoreCase(s += "mushrooms");
127 
128         (s = "thin crust").equalsIgnoreCase("thick crust");
129 
130         (s += "garlic").equalsIgnoreCase("basil");
131 
132         ("Chicago Style" + "NY Style").equalsIgnoreCase("California Style" + "Any Style");
133 
134         "onions".equalsIgnoreCase(s);
135 
136         s.equalsIgnoreCase(new String());
137 
138         s.equals(s1);
139 
140         new String().equalsIgnoreCase("more cheese");
141 
142     }
143 
144     public void noFlagForBoth() {
145         Object o = new Object();
146         String s = "peperoni";
147         String s1 = "tasty";
148 
149         o.equals(s += "mushrooms");
150 
151         (s = "thin crust").equals("thick crust");
152 
153         (s += "garlic").equals("basil");
154 
155         ("Chicago Style" + "NY Style").equals("California Style" + "Any Style");
156 
157         equals("peppers");
158 
159         "onions".equals(o);
160 
161         o.equals(new Object());
162 
163         o.equals(equals(o));
164 
165         equals("yummy");
166 
167         new Object().equals("more cheese");
168 
169         InputEqualsAvoidNullOutter outter = new InputEqualsAvoidNullOutter();
170 
171         outter.new InputEqualsAvoidNullInner().equals("eat pizza and enjoy inner classes");
172 
173         s.equalsIgnoreCase(s += "mushrooms");
174 
175         s1.equalsIgnoreCase(s += "mushrooms");
176 
177         (s = "thin crust").equalsIgnoreCase("thick crust");
178 
179         (s += "garlic").equalsIgnoreCase("basil");
180 
181         ("Chicago Style" + "NY Style").equalsIgnoreCase("California Style" + "Any Style");
182 
183         "onions".equalsIgnoreCase(s);
184 
185         s.equalsIgnoreCase(new String());
186 
187         s.equals(s1);
188 
189         new String().equalsIgnoreCase("more cheese");
190 
191     }
192 
193 }
194 
195 class InputEqualsAvoidNullOutter {
196     public class InputEqualsAvoidNullInner {
197     	public boolean equals(Object o) {
198     	    return true;
199     	}
200     }
201 }
202 
203 class MyString {
204     public boolean equals() {
205         return true;
206     }
207     
208     public boolean equals(String s1) {
209         return true;
210     }
211     
212     public boolean equalsIgnoreCase() {
213         return true;
214     }
215     
216     public boolean equalsIgnoreCase(String s1) {
217         return true;
218     }
219     
220     private String pizza;
221     
222     public void main() {
223         MyString myString = new MyString();
224         myString.equals();
225         myString.equals("what");
226         myString.equalsIgnoreCase();
227         myString.equalsIgnoreCase("what");
228         myString.equals(this.pizza = "cold pizza");
229     }
230 }
231 
232 class NewTest {
233     static String classVar;
234     String instanceVar;
235     NewTest testObj = new NewTest("");
236 
237     NewTest(String param) {
238         param.equals("");
239     }
240 
241     public void method(String param) {
242         final String localVar = "";
243 
244         localVar.equals("");
245         param.equals("");
246 
247         classVar.equals("");
248         instanceVar.equals("");
249         NewTest.classVar.equals("");
250         this.classVar.equals("");
251         this.instanceVar.equals("");
252 
253         NewTest testObj = new NewTest("");
254         this.testObj.instanceVar.equals(""); // not violated, too confusing
255         testObj.classVar.equals(""); // not violated
256 
257         for (Nested instanceVar = new Nested(); instanceVar != null; ) {
258             instanceVar.equals(1);
259             if (instanceVar.equals("")) {
260                 instanceVar.equals("");
261             }
262         }
263 
264         class Inner {
265             String instanceVarInner;
266 
267             public void main() {
268                 classVar.equals("");
269                 instanceVar.equals("");
270                 NewTest.classVar.equals("");
271 
272                 instanceVarInner.equals("");
273                 this.instanceVarInner.equals("");
274                 localVar.equals("");
275 
276                 NewTest testObj = new NewTest("");
277                 testObj.instanceVar.equals(""); // not violated
278                 testObj.classVar.equals(""); // not violated
279 
280                 Inner testInnerObj = new Inner();
281                 testInnerObj.instanceVarInner.equals(""); // not violated
282             }
283         }
284 
285         Inner testInnerObj = new Inner();
286         testInnerObj.instanceVarInner.equals(""); // not violated
287 
288         Nested.nestedClassVar.equals(""); // not violated, because the equals call is not
289         Nested Nested = new Nested(); // embedded in class Nested, what can lead to really
290         Nested.nestedInstanceVar.equals(""); // confusing constructions. But could be improved.
291         Nested.nestedClassVar.equals("");
292     }
293     static {
294         final String s = "";
295         s.equals("");
296         {
297             final String x = "";
298             class A {
299                 void foo() {
300                     s.equals("");
301                     x.equals("");
302                 }
303             }
304         }
305     }
306     void foo(String param) {
307         try {
308             param.equals("");
309             do {
310                 String s = "";
311                 s.equals("");
312             } while (param.equals(""));
313         } catch (Exception e) {
314             while (param.equals("")) {
315                 for (String s = ""; s.equals(""); ){
316                     if (s.equals("")) {
317                         synchronized (this) {
318                             switch (s) {
319                                 case "1": String str = ""; str.equals("");
320                                 case "2": s.equals(""); str = ""; str.equals("");
321                                 case "3": param.equals("");
322                                     break;
323                             }
324                         }
325                     }
326                 }
327             }
328         }
329     }
330 
331     static class Nested {
332         static String nestedClassVar;
333         String nestedInstanceVar;
334         public void method() {
335             classVar.equals("");
336             NewTest.classVar.equals("");
337             this.nestedInstanceVar.equals("");
338             nestedClassVar.equals("");
339             nestedInstanceVar.equals("");
340 
341             class Inner {
342                 public void method() {
343                     classVar.equals("");
344                     NewTest.classVar.equals("");
345                     nestedClassVar.equals("");
346                     nestedInstanceVar.equals("");
347                 }
348             }
349         }
350     }
351     enum EmbeddedEnum {
352         A(129),
353         B(283),
354         C(1212) {
355             String constDefVar;
356             public void doSomething() {
357                 constDefVar.equals("");
358             }
359         };
360 
361         Map.Entry<String,Long> enumInstance;
362 
363         EmbeddedEnum(int i) {
364             enumInstance.equals("");
365         }
366 
367         public static void doSomethingStatic() {
368             enumStatic.equals("");
369             enumStatic.equals(null);
370         }
371         static String enumStatic;
372     }
373 
374     private String foo() {return "";}
375     private Object foo(int i) {return i;}
376 
377     Object o2 = new Object();
378     Object o3 = new Object();
379     private void bar() {
380         foo().equals(""); // methods are not checked
381         foo(0).equals(""); 
382         this.foo().equals("");
383         Object o1 = new Object(); o1.equals("");
384         o2.equals(""); String o2 = "";
385         o3.equals(""); 
386 String o3 = "";
387     }
388 }
389 class Anonymous {
390     public static void main(String[] args) {
391         Runnable anonym = new Runnable() {
392             String nullableStr = null;
393             public void run() {
394                 nullableStr.equals("Null");
395             };
396         };
397         Object nullableStr = new Object();
398         nullableStr.equals("");
399     }
400     {}
401 }
402 
403 enum TestEnum {
404     ONE;
405     public void foo() {
406         TestEnum.ONE.equals(this);
407         this.ONE.equals(this);
408     }
409 }
410 
411 class TestConcatenations {
412     String s = null;
413 
414     void foo() {
415         s.equals(s + s);
416         s.equals("a" + "b");
417         s.equals(getInt() + s);
418         s.equals(getInt() + getInt());
419     }
420 
421     int getInt() {
422         return (Integer) null;
423     }
424 }
425