KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > InnerAssignmentCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19

20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import java.util.Arrays JavaDoc;
23
24 import antlr.collections.AST;
25
26 import com.puppycrawl.tools.checkstyle.api.Check;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29
30 /**
31  * <p>
32  * Checks for assignments in subexpressions, such as in
33  * <code>String s = Integer.toString(i = 2);</code>.
34  * </p>
35  * <p>
36  * Rationale: With the exception of <code>for</code> iterators, all assignments
37  * should occur in their own toplevel statement to increase readability.
38  * With inner assignments like the above it is difficult to see all places
39  * where a variable is set.
40  * </p>
41  *
42  * @author lkuehne
43  */

44 public class InnerAssignmentCheck
45         extends Check
46 {
47     /**
48      * list of allowed AST types from an assignement AST node
49      * towards the root.
50      */

51     private static final int[][] ALLOWED_ASSIGMENT_CONTEXT = {
52         {TokenTypes.EXPR, TokenTypes.SLIST},
53         {TokenTypes.VARIABLE_DEF},
54         {TokenTypes.EXPR, TokenTypes.ELIST, TokenTypes.FOR_INIT},
55         {TokenTypes.EXPR, TokenTypes.ELIST, TokenTypes.FOR_ITERATOR},
56         {TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR},
57     };
58
59     /**
60      * list of allowed AST types from an assignement AST node
61      * towards the root.
62      */

63     private static final int[][] CONTROL_CONTEXT = {
64         {TokenTypes.EXPR, TokenTypes.LITERAL_DO},
65         {TokenTypes.EXPR, TokenTypes.LITERAL_FOR},
66         {TokenTypes.EXPR, TokenTypes.LITERAL_WHILE},
67         {TokenTypes.EXPR, TokenTypes.LITERAL_IF},
68         {TokenTypes.EXPR, TokenTypes.LITERAL_ELSE},
69     };
70
71     /**
72      * list of allowed AST types from a comparison node (above an assignement)
73      * towards the root.
74      */

75     private static final int[][] ALLOWED_ASSIGMENT_IN_COMPARISON_CONTEXT = {
76         {TokenTypes.EXPR, TokenTypes.LITERAL_WHILE, },
77     };
78
79     /**
80      * The token types that identify comparison operators.
81      */

82     private static final int[] COMPARISON_TYPES = {
83         TokenTypes.EQUAL,
84         TokenTypes.GE,
85         TokenTypes.GT,
86         TokenTypes.LE,
87         TokenTypes.LT,
88         TokenTypes.NOT_EQUAL,
89     };
90
91     static {
92         Arrays.sort(COMPARISON_TYPES);
93     }
94
95     /** {@inheritDoc} */
96     public int[] getDefaultTokens()
97     {
98         return new int[] {
99             TokenTypes.ASSIGN, // '='
100
TokenTypes.DIV_ASSIGN, // "/="
101
TokenTypes.PLUS_ASSIGN, // "+="
102
TokenTypes.MINUS_ASSIGN, //"-="
103
TokenTypes.STAR_ASSIGN, // "*="
104
TokenTypes.MOD_ASSIGN, // "%="
105
TokenTypes.SR_ASSIGN, // ">>="
106
TokenTypes.BSR_ASSIGN, // ">>>="
107
TokenTypes.SL_ASSIGN, // "<<="
108
TokenTypes.BXOR_ASSIGN, // "^="
109
TokenTypes.BOR_ASSIGN, // "|="
110
TokenTypes.BAND_ASSIGN, // "&="
111
};
112     }
113
114     /** {@inheritDoc} */
115     public void visitToken(DetailAST aAST)
116     {
117         if (isInContext(aAST, ALLOWED_ASSIGMENT_CONTEXT)) {
118             return;
119         }
120
121         if (isInNoBraceControlStatement(aAST)) {
122             return;
123         }
124
125         if (isInWhileIdiom(aAST)) {
126             return;
127         }
128
129         log(aAST.getLineNo(), aAST.getColumnNo(), "assignment.inner.avoid");
130     }
131
132     /**
133      * Determines if aAST is in the body of a flow control statement without
134      * braces. An example of such a statement would be
135      * <p>
136      * <pre>
137      * if (y < 0)
138      * x = y;
139      * </pre>
140      * <p>
141      * This leads to the following AST structure:
142      * <p>
143      * <pre>
144      * LITERAL_IF
145      * LPAREN
146      * EXPR // test
147      * RPAREN
148      * EXPR // body
149      * SEMI
150      * </pre>
151      * <p>
152      * We need to ensure that aAST is in the body and not in the test.
153      *
154      * @param aAST an assignment operator AST
155      * @return whether aAST is in the body of a flow control statement
156      */

157     private static boolean isInNoBraceControlStatement(DetailAST aAST)
158     {
159         if (!isInContext(aAST, CONTROL_CONTEXT)) {
160             return false;
161         }
162         final DetailAST expr = aAST.getParent();
163         final AST exprNext = expr.getNextSibling();
164         return (exprNext != null) && (exprNext.getType() == TokenTypes.SEMI);
165     }
166
167     /**
168      * Tests whether the given AST is used in the "assignment in while test"
169      * idiom.
170      * <p>
171      * <pre>
172      * while ((b = is.read()) != -1) {
173      * // work with b
174      * }
175      * <pre>
176      * @param aAST assignment AST
177      * @return whether the context of the assignemt AST indicates the idiom
178      */

179     private boolean isInWhileIdiom(DetailAST aAST)
180     {
181         if (!isComparison(aAST.getParent())) {
182             return false;
183         }
184         return isInContext(
185                 aAST.getParent(), ALLOWED_ASSIGMENT_IN_COMPARISON_CONTEXT);
186     }
187
188     /**
189      * Checks if an AST is a comparison operator.
190      * @param aAST the AST to check
191      * @return true iff aAST is a comparison operator.
192      */

193     private static boolean isComparison(DetailAST aAST)
194     {
195         final int astType = aAST.getType();
196         return (Arrays.binarySearch(COMPARISON_TYPES, astType) >= 0);
197     }
198
199     /**
200      * Tests whether the provided AST is in
201      * one of the given contexts.
202      *
203      * @param aAST the AST from which to start walking towards root
204      * @param aContextSet the contexts to test against.
205      *
206      * @return whether the parents nodes of aAST match
207      * one of the allowed type paths
208      */

209     private static boolean isInContext(DetailAST aAST, int[][] aContextSet)
210     {
211         for (int i = 0; i < aContextSet.length; i++) {
212             DetailAST current = aAST;
213             final int len = aContextSet[i].length;
214             for (int j = 0; j < len; j++) {
215                 current = current.getParent();
216                 final int expectedType = aContextSet[i][j];
217                 if ((current == null) || (current.getType() != expectedType)) {
218                     break;
219                 }
220                 if (j == len - 1) {
221                     return true;
222                 }
223             }
224         }
225         return false;
226     }
227 }
228
Popular Tags