View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.innerassignment;
2   
3   import java.io.FileInputStream;
4   import java.io.IOException;
5   import java.util.jar.JarInputStream;
6   import java.util.jar.Manifest;
7   
8   public class InputInnerAssignment
9   {
10      void innerAssignments()
11      {
12          int a;
13          int b;
14          int c;
15  
16          a = b = c = 1; // flag two inner assignments
17  
18          String s = Integer.toString(b = 2); // flag inner assignment
19  
20          Integer i = new Integer(a += 5); // flag inner assigment
21  
22          c = b++; // common practice, don't flag
23                   // even though technically an assigment to b
24  
25          for (int j = 0; j < 6; j += 2) { // common practice, don't flag
26              a += j;
27          }
28      }
29  
30      public void demoBug1195047Comment3()
31      {
32          // inner assignment should flag all assignments to b or bb but none of those to i or j
33          int y = 1;
34          int b = 0;
35          boolean bb;
36          int i;
37  
38          if (bb = false) {}
39          for (i = 0; bb = false; i = i + 1) {}
40          while (bb = false) {}
41          if ((bb = false)) {}
42          for (int j = 0; (bb = false); j += 1) {}
43          while ((bb = false)) {}
44          i = (bb = false) ? (b = 2) : (b += 1);
45          i = (b += 1) + (b -= 1);
46          do {i += 1;} while (bb = false);
47      }
48  
49      public static void demoInputStreamIdiom(java.io.InputStream is) throws java.io.IOException
50      {
51          int b;
52          while ((b = is.read()) != -1) // common idiom to avoid clumsy loop control logic, don't flag (make configurable later)
53          {
54              // work with b
55          }
56      }
57  
58      public static void demoNoBrace()
59      {
60          // code that doesn't contain braces around conditional code
61          // results in a parse tree without SLISTs
62          // no assignement should be flagged here
63          int sum = 0;
64  
65          for (int i = 0; i < 3; i++)
66              sum = sum + i;
67  
68          if (sum > 4)
69              sum += 2;
70          else if (sum < 2)
71              sum += 1;
72          else
73              sum += 100;
74  
75          while (sum > 4)
76              sum -= 1;
77  
78          do
79              sum = sum + 1;
80          while (sum < 6);
81      }
82  
83      @SuppressWarnings(value = "unchecked")
84      public java.util.Collection<Object> allParams() {
85          java.util.ArrayList params = new java.util.ArrayList();
86          params.add("one");
87          params.add("two");
88          return params;
89      }
90  
91      // Taken from JDK7 java.lang.Package src code.
92      private static Manifest loadManifest(String fn) {
93          try (FileInputStream fis = new FileInputStream(fn);
94  	     JarInputStream jis = new JarInputStream(fis, false))
95          {
96              return jis.getManifest();
97          } catch (IOException e)
98          {
99              return null;
100         }
101     }
102 }