Source Code Cross Referenced for InnerAssignmentCheck.java in  » Code-Analyzer » checkstyle » com » puppycrawl » tools » checkstyle » checks » coding » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Code Analyzer » checkstyle » com.puppycrawl.tools.checkstyle.checks.coding 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        ////////////////////////////////////////////////////////////////////////////////
002:        // checkstyle: Checks Java source code for adherence to a set of rules.
003:        // Copyright (C) 2001-2007  Oliver Burn
004:        //
005:        // This library is free software; you can redistribute it and/or
006:        // modify it under the terms of the GNU Lesser General Public
007:        // License as published by the Free Software Foundation; either
008:        // version 2.1 of the License, or (at your option) any later version.
009:        //
010:        // This library is distributed in the hope that it will be useful,
011:        // but WITHOUT ANY WARRANTY; without even the implied warranty of
012:        // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:        // Lesser General Public License for more details.
014:        //
015:        // You should have received a copy of the GNU Lesser General Public
016:        // License along with this library; if not, write to the Free Software
017:        // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:        ////////////////////////////////////////////////////////////////////////////////
019:
020:        package com.puppycrawl.tools.checkstyle.checks.coding;
021:
022:        import java.util.Arrays;
023:
024:        import antlr.collections.AST;
025:
026:        import com.puppycrawl.tools.checkstyle.api.Check;
027:        import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028:        import com.puppycrawl.tools.checkstyle.api.DetailAST;
029:
030:        /**
031:         * <p>
032:         * Checks for assignments in subexpressions, such as in
033:         * <code>String s = Integer.toString(i = 2);</code>.
034:         * </p>
035:         * <p>
036:         * Rationale: With the exception of <code>for</code> iterators, all assignments
037:         * should occur in their own toplevel statement to increase readability.
038:         * With inner assignments like the above it is difficult to see all places
039:         * where a variable is set.
040:         * </p>
041:         *
042:         * @author lkuehne
043:         */
044:        public class InnerAssignmentCheck extends Check {
045:            /**
046:             * list of allowed AST types from an assignement AST node
047:             * towards the root.
048:             */
049:            private static final int[][] ALLOWED_ASSIGMENT_CONTEXT = {
050:                    { TokenTypes.EXPR, TokenTypes.SLIST },
051:                    { TokenTypes.VARIABLE_DEF },
052:                    { TokenTypes.EXPR, TokenTypes.ELIST, TokenTypes.FOR_INIT },
053:                    { TokenTypes.EXPR, TokenTypes.ELIST,
054:                            TokenTypes.FOR_ITERATOR },
055:                    { TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR }, };
056:
057:            /**
058:             * list of allowed AST types from an assignement AST node
059:             * towards the root.
060:             */
061:            private static final int[][] CONTROL_CONTEXT = {
062:                    { TokenTypes.EXPR, TokenTypes.LITERAL_DO },
063:                    { TokenTypes.EXPR, TokenTypes.LITERAL_FOR },
064:                    { TokenTypes.EXPR, TokenTypes.LITERAL_WHILE },
065:                    { TokenTypes.EXPR, TokenTypes.LITERAL_IF },
066:                    { TokenTypes.EXPR, TokenTypes.LITERAL_ELSE }, };
067:
068:            /**
069:             * list of allowed AST types from a comparison node (above an assignement)
070:             * towards the root.
071:             */
072:            private static final int[][] ALLOWED_ASSIGMENT_IN_COMPARISON_CONTEXT = { {
073:                    TokenTypes.EXPR, TokenTypes.LITERAL_WHILE, }, };
074:
075:            /**
076:             * The token types that identify comparison operators.
077:             */
078:            private static final int[] COMPARISON_TYPES = { TokenTypes.EQUAL,
079:                    TokenTypes.GE, TokenTypes.GT, TokenTypes.LE, TokenTypes.LT,
080:                    TokenTypes.NOT_EQUAL, };
081:
082:            static {
083:                Arrays.sort(COMPARISON_TYPES);
084:            }
085:
086:            /** {@inheritDoc} */
087:            public int[] getDefaultTokens() {
088:                return new int[] { TokenTypes.ASSIGN, // '='
089:                        TokenTypes.DIV_ASSIGN, // "/="
090:                        TokenTypes.PLUS_ASSIGN, // "+="
091:                        TokenTypes.MINUS_ASSIGN, //"-="
092:                        TokenTypes.STAR_ASSIGN, // "*="
093:                        TokenTypes.MOD_ASSIGN, // "%="
094:                        TokenTypes.SR_ASSIGN, // ">>="
095:                        TokenTypes.BSR_ASSIGN, // ">>>="
096:                        TokenTypes.SL_ASSIGN, // "<<="
097:                        TokenTypes.BXOR_ASSIGN, // "^="
098:                        TokenTypes.BOR_ASSIGN, // "|="
099:                        TokenTypes.BAND_ASSIGN, // "&="
100:                };
101:            }
102:
103:            /** {@inheritDoc} */
104:            public void visitToken(DetailAST aAST) {
105:                if (isInContext(aAST, ALLOWED_ASSIGMENT_CONTEXT)) {
106:                    return;
107:                }
108:
109:                if (isInNoBraceControlStatement(aAST)) {
110:                    return;
111:                }
112:
113:                if (isInWhileIdiom(aAST)) {
114:                    return;
115:                }
116:
117:                log(aAST.getLineNo(), aAST.getColumnNo(),
118:                        "assignment.inner.avoid");
119:            }
120:
121:            /**
122:             * Determines if aAST is in the body of a flow control statement without
123:             * braces. An example of such a statement would be
124:             * <p>
125:             * <pre>
126:             * if (y < 0)
127:             *     x = y;
128:             * </pre>
129:             * <p>
130:             * This leads to the following AST structure:
131:             * <p>
132:             * <pre>
133:             * LITERAL_IF
134:             *     LPAREN
135:             *     EXPR // test
136:             *     RPAREN
137:             *     EXPR // body
138:             *     SEMI
139:             * </pre>
140:             * <p>
141:             * We need to ensure that aAST is in the body and not in the test.
142:             *
143:             * @param aAST an assignment operator AST
144:             * @return whether aAST is in the body of a flow control statement
145:             */
146:            private static boolean isInNoBraceControlStatement(DetailAST aAST) {
147:                if (!isInContext(aAST, CONTROL_CONTEXT)) {
148:                    return false;
149:                }
150:                final DetailAST expr = aAST.getParent();
151:                final AST exprNext = expr.getNextSibling();
152:                return (exprNext != null)
153:                        && (exprNext.getType() == TokenTypes.SEMI);
154:            }
155:
156:            /**
157:             * Tests whether the given AST is used in the "assignment in while test"
158:             * idiom.
159:             * <p>
160:             * <pre>
161:             * while ((b = is.read()) != -1) {
162:             *   // work with b
163:             * }
164:             * <pre>
165:             * @param aAST assignment AST
166:             * @return whether the context of the assignemt AST indicates the idiom
167:             */
168:            private boolean isInWhileIdiom(DetailAST aAST) {
169:                if (!isComparison(aAST.getParent())) {
170:                    return false;
171:                }
172:                return isInContext(aAST.getParent(),
173:                        ALLOWED_ASSIGMENT_IN_COMPARISON_CONTEXT);
174:            }
175:
176:            /**
177:             * Checks if an AST is a comparison operator.
178:             * @param aAST the AST to check
179:             * @return true iff aAST is a comparison operator.
180:             */
181:            private static boolean isComparison(DetailAST aAST) {
182:                final int astType = aAST.getType();
183:                return (Arrays.binarySearch(COMPARISON_TYPES, astType) >= 0);
184:            }
185:
186:            /**
187:             * Tests whether the provided AST is in
188:             * one of the given contexts.
189:             *
190:             * @param aAST the AST from which to start walking towards root
191:             * @param aContextSet the contexts to test against.
192:             *
193:             * @return whether the parents nodes of aAST match
194:             * one of the allowed type paths
195:             */
196:            private static boolean isInContext(DetailAST aAST,
197:                    int[][] aContextSet) {
198:                for (int i = 0; i < aContextSet.length; i++) {
199:                    DetailAST current = aAST;
200:                    final int len = aContextSet[i].length;
201:                    for (int j = 0; j < len; j++) {
202:                        current = current.getParent();
203:                        final int expectedType = aContextSet[i][j];
204:                        if ((current == null)
205:                                || (current.getType() != expectedType)) {
206:                            break;
207:                        }
208:                        if (j == len - 1) {
209:                            return true;
210:                        }
211:                    }
212:                }
213:                return false;
214:            }
215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.