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