Source Code Cross Referenced for SingularField.java in  » Code-Analyzer » pmd-4.2rc1 » net » sourceforge » pmd » rules » design » 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 » pmd 4.2rc1 » net.sourceforge.pmd.rules.design 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * SingularField.java
003:         *
004:         * Created on April 17, 2005, 9:49 PM
005:         */
006:        package net.sourceforge.pmd.rules.design;
007:
008:        import java.util.ArrayList;
009:        import java.util.List;
010:
011:        import net.sourceforge.pmd.AbstractRule;
012:        import net.sourceforge.pmd.PropertyDescriptor;
013:        import net.sourceforge.pmd.ast.ASTAssignmentOperator;
014:        import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
015:        import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
016:        import net.sourceforge.pmd.ast.ASTFieldDeclaration;
017:        import net.sourceforge.pmd.ast.ASTIfStatement;
018:        import net.sourceforge.pmd.ast.ASTInitializer;
019:        import net.sourceforge.pmd.ast.ASTMethodDeclaration;
020:        import net.sourceforge.pmd.ast.ASTPrimaryExpression;
021:        import net.sourceforge.pmd.ast.ASTStatementExpression;
022:        import net.sourceforge.pmd.ast.ASTSynchronizedStatement;
023:        import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
024:        import net.sourceforge.pmd.ast.Node;
025:        import net.sourceforge.pmd.ast.SimpleNode;
026:        import net.sourceforge.pmd.properties.BooleanProperty;
027:        import net.sourceforge.pmd.symboltable.NameOccurrence;
028:
029:        /**
030:         * @author Eric Olander
031:         * @author Wouter Zelle
032:         */
033:        public class SingularField extends AbstractRule {
034:
035:            /**
036:             * Restore old behaviour by setting both properties to true, which will result in many false positives
037:             */
038:            private static final PropertyDescriptor CHECK_INNER_CLASSES = new BooleanProperty(
039:                    "CheckInnerClasses", "Check inner classes", false, 1.0f);
040:            private static final PropertyDescriptor DISALLOW_NOT_ASSIGNMENT = new BooleanProperty(
041:                    "DisallowNotAssignment",
042:                    "Disallow violations where the first usage is not an assignment",
043:                    false, 1.0f);
044:
045:            public Object visit(ASTFieldDeclaration node, Object data) {
046:                boolean checkInnerClasses = getBooleanProperty(CHECK_INNER_CLASSES);
047:                boolean disallowNotAssignment = getBooleanProperty(DISALLOW_NOT_ASSIGNMENT);
048:
049:                if (node.isPrivate() && !node.isStatic()) {
050:                    List<ASTVariableDeclaratorId> list = node
051:                            .findChildrenOfType(ASTVariableDeclaratorId.class);
052:                    ASTVariableDeclaratorId declaration = list.get(0);
053:                    List<NameOccurrence> usages = declaration.getUsages();
054:                    SimpleNode decl = null;
055:                    boolean violation = true;
056:                    for (int ix = 0; ix < usages.size(); ix++) {
057:                        NameOccurrence no = usages.get(ix);
058:                        SimpleNode location = no.getLocation();
059:
060:                        ASTPrimaryExpression primaryExpressionParent = location
061:                                .getFirstParentOfType(ASTPrimaryExpression.class);
062:                        if (ix == 0 && !disallowNotAssignment) {
063:                            if (primaryExpressionParent
064:                                    .getFirstParentOfType(ASTIfStatement.class) != null) {
065:                                //the first usage is in an if, so it may be skipped on 
066:                                //later calls to the method. So this might be legit code
067:                                //that simply stores an object for later use.
068:                                violation = false;
069:                                break; //Optimization
070:                            }
071:
072:                            //Is the first usage in an assignment?
073:                            Node potentialStatement = primaryExpressionParent
074:                                    .jjtGetParent();
075:                            boolean assignmentToField = no.getImage().equals(
076:                                    location.getImage()); //Check the the assignment is not to a field inside the field object
077:                            if (!assignmentToField
078:                                    || !isInAssignment(potentialStatement)) {
079:                                violation = false;
080:                                break; //Optimization
081:                            } else {
082:                                if (usages.size() > ix + 1) {
083:                                    SimpleNode secondUsageLocation = usages
084:                                            .get(ix + 1).getLocation();
085:
086:                                    List<ASTStatementExpression> parentStatements = secondUsageLocation
087:                                            .getParentsOfType(ASTStatementExpression.class);
088:                                    for (ASTStatementExpression statementExpression : parentStatements) {
089:                                        if (statementExpression != null
090:                                                && statementExpression
091:                                                        .equals(potentialStatement)) {
092:                                            //The second usage is in the assignment of the first usage, which is allowed
093:                                            violation = false;
094:                                            break; //Optimization
095:                                        }
096:                                    }
097:
098:                                }
099:                            }
100:                        }
101:
102:                        if (!checkInnerClasses) {
103:                            //Skip inner classes because the field can be used in the outer class and checking this is too difficult
104:                            ASTClassOrInterfaceDeclaration clazz = location
105:                                    .getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
106:                            if (clazz != null
107:                                    && clazz
108:                                            .getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) != null) {
109:                                violation = false;
110:                                break; //Optimization
111:                            }
112:                        }
113:
114:                        if (primaryExpressionParent.jjtGetParent() instanceof  ASTSynchronizedStatement) {
115:                            //This usage is directly in an expression of a synchronized block
116:                            violation = false;
117:                            break; //Optimization
118:                        }
119:
120:                        SimpleNode method = location
121:                                .getFirstParentOfType(ASTMethodDeclaration.class);
122:                        if (method == null) {
123:                            method = location
124:                                    .getFirstParentOfType(ASTConstructorDeclaration.class);
125:                            if (method == null) {
126:                                method = location
127:                                        .getFirstParentOfType(ASTInitializer.class);
128:                                if (method == null) {
129:                                    continue;
130:                                }
131:                            }
132:                        }
133:
134:                        if (decl == null) {
135:                            decl = method;
136:                            continue;
137:                        } else if (decl != method) {
138:                            violation = false;
139:                            break; //Optimization
140:                        }
141:
142:                    }
143:
144:                    if (violation && !usages.isEmpty()) {
145:                        addViolation(data, node, new Object[] { declaration
146:                                .getImage() });
147:                    }
148:                }
149:                return data;
150:            }
151:
152:            private boolean isInAssignment(Node potentialStatement) {
153:                if (potentialStatement instanceof  ASTStatementExpression) {
154:                    ASTStatementExpression statement = (ASTStatementExpression) potentialStatement;
155:                    List<ASTAssignmentOperator> assignments = new ArrayList<ASTAssignmentOperator>();
156:                    statement.findChildrenOfType(ASTAssignmentOperator.class,
157:                            assignments, false);
158:                    if (assignments.isEmpty()
159:                            || !"=".equals(assignments.get(0).getImage())) {
160:                        return false;
161:                    } else {
162:                        return true;
163:                    }
164:                } else {
165:                    return false;
166:                }
167:            }
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.