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


001:        /**
002:         * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003:         */package net.sourceforge.pmd.rules.strings;
004:
005:        import net.sourceforge.pmd.AbstractRule;
006:        import net.sourceforge.pmd.ast.ASTAdditiveExpression;
007:        import net.sourceforge.pmd.ast.ASTAllocationExpression;
008:        import net.sourceforge.pmd.ast.ASTArgumentList;
009:        import net.sourceforge.pmd.ast.ASTBlockStatement;
010:        import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
011:        import net.sourceforge.pmd.ast.ASTLiteral;
012:        import net.sourceforge.pmd.ast.ASTName;
013:        import net.sourceforge.pmd.ast.ASTStatementExpression;
014:        import net.sourceforge.pmd.ast.Node;
015:        import net.sourceforge.pmd.ast.SimpleNode;
016:        import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
017:        import net.sourceforge.pmd.typeresolution.TypeHelper;
018:
019:        import java.util.Iterator;
020:        import java.util.List;
021:
022:        /*
023:         * How this rule works:
024:         * find additive expressions: +
025:         * check that the addition is between anything other than two literals
026:         * if true and also the parent is StringBuffer constructor or append,
027:         * report a violation.
028:         * 
029:         * @author mgriffa
030:         */
031:        public class InefficientStringBuffering extends AbstractRule {
032:
033:            public Object visit(ASTAdditiveExpression node, Object data) {
034:                ASTBlockStatement bs = node
035:                        .getFirstParentOfType(ASTBlockStatement.class);
036:                if (bs == null) {
037:                    return data;
038:                }
039:
040:                int immediateLiterals = 0;
041:                List<ASTLiteral> nodes = node
042:                        .findChildrenOfType(ASTLiteral.class);
043:                for (ASTLiteral literal : nodes) {
044:                    if (literal.jjtGetParent().jjtGetParent().jjtGetParent() instanceof  ASTAdditiveExpression) {
045:                        immediateLiterals++;
046:                    }
047:                    try {
048:                        Integer.parseInt(literal.getImage());
049:                        return data;
050:                    } catch (NumberFormatException nfe) {
051:                        // NFE means new StringBuffer("a" + "b"), want to flag those
052:                    }
053:                }
054:
055:                if (immediateLiterals > 1) {
056:                    return data;
057:                }
058:
059:                // if literal + public static final, return
060:                List<ASTName> nameNodes = node
061:                        .findChildrenOfType(ASTName.class);
062:                for (ASTName name : nameNodes) {
063:                    if (name.getNameDeclaration() instanceof  VariableNameDeclaration) {
064:                        VariableNameDeclaration vnd = (VariableNameDeclaration) name
065:                                .getNameDeclaration();
066:                        if (vnd.getAccessNodeParent().isFinal()
067:                                && vnd.getAccessNodeParent().isStatic()) {
068:                            return data;
069:                        }
070:                    }
071:                }
072:
073:                if (bs.isAllocation()) {
074:                    for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator
075:                            .hasNext();) {
076:                        ASTName name = iterator.next();
077:                        if (!name.getImage().endsWith("length")) {
078:                            break;
079:                        } else if (!iterator.hasNext()) {
080:                            return data; //All names end with length
081:                        }
082:                    }
083:
084:                    if (isAllocatedStringBuffer(node)) {
085:                        addViolation(data, node);
086:                    }
087:                } else if (isInStringBufferOperation(node, 6, "append")) {
088:                    addViolation(data, node);
089:                }
090:                return data;
091:            }
092:
093:            protected static boolean isInStringBufferOperation(SimpleNode node,
094:                    int length, String methodName) {
095:                if (!xParentIsStatementExpression(node, length)) {
096:                    return false;
097:                }
098:                ASTStatementExpression s = node
099:                        .getFirstParentOfType(ASTStatementExpression.class);
100:                if (s == null) {
101:                    return false;
102:                }
103:                ASTName n = s.getFirstChildOfType(ASTName.class);
104:                if (n == null
105:                        || n.getImage().indexOf(methodName) == -1
106:                        || !(n.getNameDeclaration() instanceof  VariableNameDeclaration)) {
107:                    return false;
108:                }
109:
110:                // TODO having to hand-code this kind of dredging around is ridiculous
111:                // we need something to support this in the framework
112:                // but, "for now" (tm):
113:                // if more than one arg to append(), skip it
114:                ASTArgumentList argList = s
115:                        .getFirstChildOfType(ASTArgumentList.class);
116:                if (argList == null || argList.jjtGetNumChildren() > 1) {
117:                    return false;
118:                }
119:                return TypeHelper.isA((VariableNameDeclaration) n
120:                        .getNameDeclaration(), StringBuffer.class);
121:            }
122:
123:            // TODO move this method to SimpleNode
124:            private static boolean xParentIsStatementExpression(
125:                    SimpleNode node, int length) {
126:                Node curr = node;
127:                for (int i = 0; i < length; i++) {
128:                    if (node.jjtGetParent() == null) {
129:                        return false;
130:                    }
131:                    curr = curr.jjtGetParent();
132:                }
133:                return curr instanceof  ASTStatementExpression;
134:            }
135:
136:            private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) {
137:                ASTAllocationExpression ao = node
138:                        .getFirstParentOfType(ASTAllocationExpression.class);
139:                if (ao == null) {
140:                    return false;
141:                }
142:                // note that the child can be an ArrayDimsAndInits, for example, from java.lang.FloatingDecimal:  t = new int[ nWords+wordcount+1 ];
143:                ASTClassOrInterfaceType an = ao
144:                        .getFirstChildOfType(ASTClassOrInterfaceType.class);
145:                return an != null
146:                        && (TypeHelper.isA(an, StringBuffer.class) || TypeHelper
147:                                .isA(an, StringBuilder.class));
148:            }
149:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.