KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > coding > InputInnerAssignment


1 package com.puppycrawl.tools.checkstyle.coding;
2
3 public class InputInnerAssignment
4 {
5     void innerAssignments()
6     {
7         int a;
8         int b;
9         int c;
10
11         a = b = c = 1; // flag two inner assignments
12

13         String JavaDoc s = Integer.toString(b = 2); // flag inner assignment
14

15         Integer JavaDoc i = new Integer JavaDoc(a += 5); // flag inner assigment
16

17         c = b++; // common practice, don't flag
18
// even though technically an assigment to b
19

20         for (int j = 0; j < 6; j += 2) { // common practice, don't flag
21
a += j;
22         }
23     }
24
25     public void demoBug1195047Comment3()
26     {
27         // inner assignment should flag all assignments to b or bb but none of those to i or j
28
int y = 1;
29         int b = 0;
30         boolean bb;
31         int i;
32
33         if (bb = false) {}
34         for (i = 0; bb = false; i = i + 1) {}
35         while (bb = false) {}
36         if ((bb = false)) {}
37         for (int j = 0; (bb = false); j += 1) {}
38         while ((bb = false)) {}
39         i = (bb = false) ? (b = 2) : (b += 1);
40         i = (b += 1) + (b -= 1);
41         do {i += 1;} while (bb = false);
42     }
43
44     public static void demoInputStreamIdiom(java.io.InputStream JavaDoc is) throws java.io.IOException JavaDoc
45     {
46         int b;
47         while ((b = is.read()) != -1) // common idiom to avoid clumsy loop control logic, don't flag (make configurable later)
48
{
49             // work with b
50
}
51     }
52
53     public static void demoNoBrace()
54     {
55         // code that doesn't contain braces around conditional code
56
// results in a parse tree without SLISTs
57
// no assignement should be flagged here
58
int sum = 0;
59
60         for (int i = 0; i < 3; i++)
61             sum = sum + i;
62
63         if (sum > 4)
64             sum += 2;
65         else if (sum < 2)
66             sum += 1;
67         else
68             sum += 100;
69
70         while (sum > 4)
71             sum -= 1;
72
73         do
74             sum = sum + 1;
75         while (sum < 6);
76     }
77
78     @SuppressWarnings JavaDoc(value = "unchecked")
79     public java.util.Collection JavaDoc<Object JavaDoc> allParams() {
80         java.util.ArrayList JavaDoc params = new java.util.ArrayList JavaDoc();
81         params.add("one");
82         params.add("two");
83         return params;
84     }
85 }
86
Popular Tags