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 assignment
21  
22          c = b++; // common practice, don't flag
23                   // even though technically an assignment 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 assignment 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          ChildParent o = new ChildParent();
83          Object t = null;
84  
85          while (o != null)
86              t = o = o.getParent();
87      }
88  
89      @SuppressWarnings(value = "unchecked")
90      public java.util.Collection<Object> allParams() {
91          java.util.ArrayList params = new java.util.ArrayList();
92          params.add("one");
93          params.add("two");
94          return params;
95      }
96  
97      // Taken from JDK7 java.lang.Package src code.
98      private static Manifest loadManifest(String fn) {
99          try (FileInputStream fis = new FileInputStream(fn);
100 	     JarInputStream jis = new JarInputStream(fis, false))
101         {
102             return jis.getManifest();
103         } catch (IOException e)
104         {
105             return null;
106         }
107     }
108 
109     private static class ChildParent {
110         public ChildParent getParent() {
111             return this;
112         }
113     }
114 }