Source Code Cross Referenced for ForLoopControlVariablesRule.java in  » Code-Analyzer » hammurapi-3.20.0.3 » org » hammurapi » inspectors » 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 » hammurapi 3.20.0.3 » org.hammurapi.inspectors 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Hammurapi
003:         * Automated Java code review system. 
004:         * Copyright (C) 2004  Hammurapi Group
005:         *
006:         * This program is free software; you can redistribute it and/or modify
007:         * it under the terms of the GNU General Public License as published by
008:         * the Free Software Foundation; either version 2 of the License, or
009:         *  (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License
017:         * along with this program; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         * URL: http://www.hammurapi.org
021:         * e-Mail: support@hammurapi.biz
022:         */
023:        package org.hammurapi.inspectors;
024:
025:        import java.util.Collection;
026:        import java.util.HashSet;
027:        import java.util.Iterator;
028:
029:        import org.hammurapi.InspectorBase;
030:        import org.hammurapi.HammurapiException;
031:        import org.hammurapi.InspectorContext;
032:
033:        import com.pavelvlasov.jsel.Declaration;
034:        import com.pavelvlasov.jsel.VariableDefinition;
035:        import com.pavelvlasov.jsel.expressions.AssignmentExpression;
036:        import com.pavelvlasov.jsel.expressions.ConditionalExpression;
037:        import com.pavelvlasov.jsel.expressions.Expression;
038:        import com.pavelvlasov.jsel.expressions.ExpressionList;
039:        import com.pavelvlasov.jsel.expressions.Ident;
040:        import com.pavelvlasov.jsel.expressions.LogicalOr;
041:        import com.pavelvlasov.jsel.statements.ForStatement;
042:        import com.pavelvlasov.review.SourceMarker;
043:        import com.pavelvlasov.util.AccumulatingVisitorExceptionSink;
044:        import com.pavelvlasov.util.DispatchingVisitor;
045:        import com.pavelvlasov.util.Visitable;
046:
047:        /**
048:         * ER-098
049:         * Do not assign loop control variables in the body of a "for" loop
050:         * @author  Janos Czako
051:         * @author Pavel Vlasov
052:         * @version $Revision: 1.5 $
053:         */
054:        public class ForLoopControlVariablesRule extends InspectorBase {
055:
056:            /**
057:             * The chained visitor class which searches after assigning 
058:             * the loop control variable.
059:             */
060:            public static class Snooper {
061:
062:                /**
063:                 * The name of the loop control variable, searched for.
064:                 */
065:                private Collection names;
066:                private InspectorContext context;
067:
068:                /**
069:                 * Constructor, takes the name searched for.
070:                 * 
071:                 * @param theIdentName the name of the variable searched for.
072:                 */
073:                public Snooper(final Collection names, InspectorContext context) {
074:                    this .names = names;
075:                    this .context = context;
076:                }
077:
078:                /**
079:                 * Reviews the assignments.
080:                 * 
081:                 * @param element the assignment to be reviewed.
082:                 */
083:                public void visit(AssignmentExpression element) {
084:                    if (!(element instanceof  LogicalOr || element instanceof  ConditionalExpression)) {
085:                        if (element.getOperand(0) instanceof  Ident) {
086:                            Ident ident = (Ident) element.getOperand(0);
087:                            if (names.contains(ident.getText())) {
088:                                context.reportViolation((SourceMarker) element);
089:                            }
090:                        }
091:                    }
092:                }
093:            }
094:
095:            /**
096:             * The error text for exceptions in the chained visitor.
097:             */
098:            private static final String CHAINED_ERRS = "There have been exceptions (see above)";
099:
100:            /**
101:             * Reviews the for statement if it violates agains the rule
102:             * 
103:             * @param element the fro statement
104:             * @throws HammurapiException in case of any exception in the chained visitor
105:             */
106:            public void visit(ForStatement element) throws HammurapiException {
107:                Collection names = new HashSet();
108:                if (element.getInitializer() != null) {
109:                    if (element.getInitializer() instanceof  Declaration) {
110:                        getNames((Declaration) element.getInitializer(), names);
111:                    } else if (element.getInitializer() instanceof  ExpressionList) {
112:                        getNames((ExpressionList) element.getInitializer(),
113:                                names);
114:                    }
115:                }
116:
117:                if (!names.isEmpty()) {
118:                    Snooper rs = new Snooper(names, context);
119:                    // TODO Don't use DispatchingVisitor, use plain visitor
120:                    // TODO Use Scope.getVariableNamespace().find() to make sure that
121:                    // first operand of assignment expression is control variable
122:                    AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink();
123:                    ((Visitable) element.getStatement())
124:                            .accept(new DispatchingVisitor(rs, es));
125:
126:                    if (!es.getExceptions().isEmpty()) {
127:                        es.dump();
128:                        throw new HammurapiException(CHAINED_ERRS);
129:                    }
130:                }
131:            }
132:
133:            /**
134:             * Gets the control variable name in for loop 
135:             * if it is declared in the for statement
136:             * 
137:             * @param element the declaration
138:             * @return the name of the variable
139:             */
140:            private void getNames(Declaration element, Collection names) {
141:                Iterator it = element.iterator();
142:                while (it.hasNext()) {
143:                    VariableDefinition vd = (VariableDefinition) it.next();
144:                    names.add(vd.getName());
145:                }
146:            }
147:
148:            /**
149:             * Gets the control variable name in for loop 
150:             * if it is declared not in the for statement
151:             * 
152:             * @param element the assigment expression from the for statement
153:             * @return the assignment expression
154:             */
155:            private void getNames(ExpressionList element, Collection names) {
156:                Iterator it = element.iterator();
157:                while (it.hasNext()) {
158:                    Object o = it.next();
159:                    if (o instanceof  AssignmentExpression
160:                            && !(o instanceof  LogicalOr || o instanceof  ConditionalExpression)
161:                            && ((Expression) o).getOperand(0) instanceof  Ident) {
162:                        names.add(((Ident) ((Expression) o).getOperand(0))
163:                                .getText());
164:                    }
165:                }
166:            }
167:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.