View Javadoc
1   /*
2   UnnecessaryNullCheckWithInstanceOf
3   
4   */
5   
6   package com.puppycrawl.tools.checkstyle.checks.coding.unnecessarynullcheckwithinstanceof;
7   
8   public class InputUnnecessaryNullCheckWithInstanceOfMultipleConditions {
9   
10      public void multipleConditions(Object obj, String str) {
11          // violation below, 'Unnecessary nullity check'
12          if (obj != null && obj instanceof String && !str.isEmpty()) {
13              String s = (String) obj;
14          }
15  
16          // violation below, 'Unnecessary nullity check'
17          if (!str.isEmpty() && obj != null && obj instanceof String) {
18              String s = (String) obj;
19          }
20  
21          // violation below, 'Unnecessary nullity check'
22          if (obj != null && obj instanceof String && str != null && !str.isEmpty()) {
23              String s = (String) obj;
24          }
25  
26          // violation below, 'Unnecessary nullity check'
27          if ( obj instanceof String && str != null && obj != null ) {
28              String s = (String) obj;
29          }
30      }
31  
32      public void multipleInstanceOfChecks(Object obj1, Object obj2) {
33          // 2 violations 3 lines below:
34          //                            'Unnecessary nullity check'
35          //                            'Unnecessary nullity check'
36          if (obj1 != null && obj1 instanceof String && obj2 != null && obj2 instanceof Integer) {
37              String s = (String) obj1;
38              Integer i = (Integer) obj2;
39          }
40          if (obj1 != null && obj2 instanceof String) {
41              String s = (String) obj1;
42          }
43          if (null != obj2 && obj1 instanceof String) {
44              String s = (String) obj1;
45          }
46          if (obj1 != obj2 && obj1 instanceof String) {
47              String s = (String) obj1;
48          }
49          // violation below, 'Unnecessary nullity check'
50          if (obj1 != null && (obj2 != null && obj1 instanceof String)) {
51             System.out.println("obj is a String and obj2 is not null");
52          }
53          if ((obj1 != null || obj2 instanceof String) && obj2 != null) {
54             String s = (String) obj1;
55          }
56  
57      }
58  }