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


001:        package net.sourceforge.pmd.rules;
002:
003:        import java.util.Iterator;
004:        import java.util.List;
005:        import java.util.Set;
006:
007:        import net.sourceforge.pmd.AbstractRule;
008:        import net.sourceforge.pmd.ast.ASTConditionalExpression;
009:        import net.sourceforge.pmd.ast.ASTExpression;
010:        import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
011:        import net.sourceforge.pmd.ast.ASTPrimaryExpression;
012:        import net.sourceforge.pmd.ast.ASTPrimarySuffix;
013:        import net.sourceforge.pmd.ast.ASTType;
014:        import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
015:        import net.sourceforge.pmd.ast.Node;
016:        import net.sourceforge.pmd.ast.SimpleNode;
017:        import net.sourceforge.pmd.symboltable.NameOccurrence;
018:        import net.sourceforge.pmd.util.CollectionUtil;
019:
020:        /**
021:         * An operation on an Immutable object (BigDecimal or BigInteger) won't change
022:         * the object itself. The result of the operation is a new object. Therefore,
023:         * ignoring the operation result is an error.
024:         */
025:        public class UselessOperationOnImmutable extends AbstractRule {
026:            /**
027:             * These are the methods which are immutable
028:             */
029:            private static final Set<String> targetMethods = CollectionUtil
030:                    .asSet(new String[] { ".add", ".multiply", ".divide",
031:                            ".subtract", ".setScale", ".negate",
032:                            ".movePointLeft", ".movePointRight", ".pow",
033:                            ".shiftLeft", ".shiftRight" });
034:
035:            /**
036:             * These are the classes that the rule can apply to
037:             */
038:            private static final Set<String> targetClasses = CollectionUtil
039:                    .asSet(new String[] { "java.math.BigDecimal", "BigDecimal",
040:                            "java.math.BigInteger", "BigInteger" });
041:
042:            public Object visit(ASTLocalVariableDeclaration node, Object data) {
043:
044:                ASTVariableDeclaratorId var = getDeclaration(node);
045:                if (var == null) {
046:                    return super .visit(node, data);
047:                }
048:                String variableName = var.getImage();
049:                for (NameOccurrence no : var.getUsages()) {
050:                    // FIXME - getUsages will return everything with the same name as the variable, 
051:                    // see JUnit test, case 6. Changing to SimpleNode below, revisit when getUsages is fixed
052:                    SimpleNode sn = no.getLocation();
053:                    Node primaryExpression = sn.jjtGetParent().jjtGetParent();
054:                    Class<? extends Node> parentClass = primaryExpression
055:                            .jjtGetParent().getClass();
056:                    if (!(parentClass.equals(ASTExpression.class)
057:                            || parentClass
058:                                    .equals(ASTConditionalExpression.class) || hasComparisons(primaryExpression))) {
059:                        String methodCall = sn.getImage().substring(
060:                                variableName.length());
061:                        if (targetMethods.contains(methodCall)) {
062:                            addViolation(data, sn);
063:                        }
064:                    }
065:                }
066:                return super .visit(node, data);
067:            }
068:
069:            /**
070:             * Check whether the Immutable is compareTo'd something
071:             */
072:            private boolean hasComparisons(Node primaryExpression) {
073:                if (primaryExpression.getClass().equals(
074:                        ASTPrimaryExpression.class)) {
075:                    List<ASTPrimarySuffix> suffixes = ((ASTPrimaryExpression) primaryExpression)
076:                            .findChildrenOfType(ASTPrimarySuffix.class);
077:                    for (Iterator<ASTPrimarySuffix> iterator = suffixes
078:                            .iterator(); iterator.hasNext();) {
079:                        ASTPrimarySuffix suffix = iterator.next();
080:                        if ("compareTo".equals(suffix.getImage()))
081:                            return true;
082:                    }
083:                } else {
084:                    //Some weird usage of the Immutable
085:                }
086:                return false; //No comparison
087:            }
088:
089:            /**
090:             * This method checks the variable declaration if it is on a class we care
091:             * about. If it is, it returns the DeclaratorId
092:             * 
093:             * @param node
094:             *            The ASTLocalVariableDeclaration which is a problem
095:             * @return ASTVariableDeclaratorId
096:             */
097:            private ASTVariableDeclaratorId getDeclaration(
098:                    ASTLocalVariableDeclaration node) {
099:                ASTType type = node.getTypeNode();
100:                if (targetClasses.contains(type.getTypeImage())) {
101:                    return (ASTVariableDeclaratorId) node.jjtGetChild(1)
102:                            .jjtGetChild(0);
103:                }
104:                return null;
105:            }
106:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.