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