View Javadoc
1   package com.github.sevntu.checkstyle.checks.coding;
2   
3   public class InputAvoidConstantAsFirstOperandInConditionCheck {
4   
5       private Object classField;
6   
7       private boolean someFunction() {
8           return false;
9       }
10  
11      private Object returnNull() {
12          return null;
13      }
14  
15      public void allTypesCheck() {
16  
17          Object someVariable = new Object();
18  
19          final int constant = 5;
20          int testInt = 10;
21          float testConst = 5.0f;
22          boolean testBool = true;
23  
24          if ((null) == someVariable) {} //!!
25          if (5 == testInt) {} //!!
26          if (constant == testInt) {}
27          if (5.0d == (Double) someVariable) {} //!!
28          if (5.0f == (Float) someVariable) {} //!!
29          if (5l == (Long) someVariable) {} //!!
30          if (true == testBool) {} //!!
31          if (false == testBool) {} //!!
32      }
33  
34      /**
35       * Basic tests.
36       */
37      public void ifCheck() {
38          Object someVariable = new Object();
39          Object nullVariable = null;
40  
41          if (null == null) {}
42  
43          // Compare variable
44          if (someVariable == null) {}
45          if (someVariable != null) {}
46          if (null == someVariable) {} //!!
47          if (null != someVariable) {} //!!
48  
49          // Compare function
50          if (returnNull() == null) {}
51          if (returnNull() != null) {}
52          if (null == returnNull()) {} //!!
53          if (null != returnNull()) {} //!!
54  
55          // Compare class field
56          if (this.classField == null) {}
57          if (classField != null) {}
58          if (null == this.classField) {} //!!
59          if (null != classField) {} //!!
60  
61          // Compare with null-variable
62          if (someVariable == nullVariable) {}
63          if (nullVariable == someVariable) {}
64  
65          // Short IF tag
66          someVariable = (nullVariable == null) ? true : false;
67          someVariable = (null == nullVariable) ? true : false; //!!
68  
69          // Test more than one condition
70          if (someFunction() && (someVariable == null)) {}
71          if (someFunction() && null == someVariable) {} //!!
72          if (someFunction() && (null == someVariable)) {} //!!
73          if ((null == someVariable) && someFunction()) {} //!!
74          if (someFunction() || (null == someVariable) && someFunction()) {} //!!
75  
76          if (someVariable == null && null == someVariable) {} //!!
77          if (null == someVariable && someVariable == null) {} //!!
78          if (null == someVariable && null == someVariable) {} //!! twice
79  
80          // Test for different code style
81          // (check line and position number in error message)
82          if ((
83                  null
84                          == someVariable)
85                  &&null==someVariable
86                  || (	null	==	someVariable)) {} //!!
87      }
88  
89      /**
90       * Almost all tests are checked in ifCheck() function,
91       * so there are only checked some tests
92       */
93      public void whileCyclesCheck() {
94          Object someVariable = new Object();
95  
96          while (someVariable == null) {}
97          while (null == someVariable) {} //!!
98  
99          do {
100             // Something...
101         } while (null == someVariable); //!!
102     }
103 
104     /**
105      * Almost all tests are checked in ifCheck() function,
106      * so there are only checked some tests
107      */
108     public void forCyclesCheck() {
109         Object someVariable = new Object();
110 
111         for (;null==someVariable;) {} //!!
112         for (someVariable = null; null == someVariable; someVariable = null) {} //!!
113     }
114 
115 }