Source Code Cross Referenced for DoubleCheckedLocking.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:        /**
002:         * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003:         */package net.sourceforge.pmd.rules;
004:
005:        import net.sourceforge.pmd.ast.ASTAssignmentOperator;
006:        import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
007:        import net.sourceforge.pmd.ast.ASTIfStatement;
008:        import net.sourceforge.pmd.ast.ASTLiteral;
009:        import net.sourceforge.pmd.ast.ASTMethodDeclaration;
010:        import net.sourceforge.pmd.ast.ASTName;
011:        import net.sourceforge.pmd.ast.ASTNullLiteral;
012:        import net.sourceforge.pmd.ast.ASTPrimaryExpression;
013:        import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
014:        import net.sourceforge.pmd.ast.ASTReferenceType;
015:        import net.sourceforge.pmd.ast.ASTReturnStatement;
016:        import net.sourceforge.pmd.ast.ASTStatementExpression;
017:        import net.sourceforge.pmd.ast.ASTSynchronizedStatement;
018:        import net.sourceforge.pmd.ast.ASTType;
019:        import net.sourceforge.pmd.ast.Node;
020:
021:        import java.util.ArrayList;
022:        import java.util.List;
023:
024:        /**
025:         * void method() {
026:         * if(x == null) {
027:         * synchronized(this){
028:         * if(x == null) {
029:         * x = new | method();
030:         * }
031:         * }
032:         * }
033:         * 1.  The error is when one uses the value assigned within a synchronized
034:         * section, outside of a synchronized section.
035:         * if(x == null) is outside of synchronized section
036:         * x = new | method();
037:         * <p/>
038:         * <p/>
039:         * Very very specific check for double checked locking.
040:         *
041:         * @author CL Gilbert (dnoyeb@users.sourceforge.net)
042:         */
043:        public class DoubleCheckedLocking extends
044:                net.sourceforge.pmd.AbstractRule {
045:
046:            public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
047:                if (node.isInterface()) {
048:                    return data;
049:                }
050:                return super .visit(node, data);
051:            }
052:
053:            public Object visit(ASTMethodDeclaration node, Object data) {
054:                if (node.getResultType().isVoid()) {
055:                    return super .visit(node, data);
056:                }
057:
058:                ASTType typeNode = (ASTType) node.getResultType()
059:                        .jjtGetChild(0);
060:                if (typeNode.jjtGetNumChildren() == 0
061:                        || !(typeNode.jjtGetChild(0) instanceof  ASTReferenceType)) {
062:                    return super .visit(node, data);
063:                }
064:
065:                List<ASTReturnStatement> rsl = new ArrayList<ASTReturnStatement>();
066:                node.findChildrenOfType(ASTReturnStatement.class, rsl, true);
067:                if (rsl.size() != 1) {
068:                    return super .visit(node, data);
069:                }
070:                ASTReturnStatement rs = rsl.get(0);
071:
072:                List<ASTPrimaryExpression> pel = new ArrayList<ASTPrimaryExpression>();
073:                rs.findChildrenOfType(ASTPrimaryExpression.class, pel, true);
074:                ASTPrimaryExpression ape = pel.get(0);
075:                Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
076:                String returnVariableName = null;
077:                if (lastChild instanceof  ASTPrimaryPrefix) {
078:                    returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
079:                }
080:                if (returnVariableName == null) {
081:                    return super .visit(node, data);
082:                }
083:                List<ASTIfStatement> isl = new ArrayList<ASTIfStatement>();
084:                node.findChildrenOfType(ASTIfStatement.class, isl, true);
085:                if (isl.size() == 2) {
086:                    ASTIfStatement is = isl.get(0);
087:                    if (ifVerify(is, returnVariableName)) {
088:                        //find synchronized
089:                        List<ASTSynchronizedStatement> ssl = new ArrayList<ASTSynchronizedStatement>();
090:                        is.findChildrenOfType(ASTSynchronizedStatement.class,
091:                                ssl, true);
092:                        if (ssl.size() == 1) {
093:                            ASTSynchronizedStatement ss = ssl.get(0);
094:                            isl.clear();
095:                            ss.findChildrenOfType(ASTIfStatement.class, isl,
096:                                    true);
097:                            if (isl.size() == 1) {
098:                                ASTIfStatement is2 = isl.get(0);
099:                                if (ifVerify(is2, returnVariableName)) {
100:                                    List<ASTStatementExpression> sel = new ArrayList<ASTStatementExpression>();
101:                                    is2.findChildrenOfType(
102:                                            ASTStatementExpression.class, sel,
103:                                            true);
104:                                    if (sel.size() == 1) {
105:                                        ASTStatementExpression se = sel.get(0);
106:                                        if (se.jjtGetNumChildren() == 3) { //primaryExpression, AssignmentOperator, Expression
107:                                            if (se.jjtGetChild(0) instanceof  ASTPrimaryExpression) {
108:                                                ASTPrimaryExpression pe = (ASTPrimaryExpression) se
109:                                                        .jjtGetChild(0);
110:                                                if (matchName(pe,
111:                                                        returnVariableName)) {
112:                                                    if (se.jjtGetChild(1) instanceof  ASTAssignmentOperator) {
113:                                                        addViolation(data, node);
114:                                                    }
115:                                                }
116:                                            }
117:                                        }
118:                                    }
119:                                }
120:                            }
121:                        }
122:                    }
123:                }
124:                return super .visit(node, data);
125:            }
126:
127:            private boolean ifVerify(ASTIfStatement is, String varname) {
128:                List<ASTPrimaryExpression> finder = new ArrayList<ASTPrimaryExpression>();
129:                is.findChildrenOfType(ASTPrimaryExpression.class, finder, true);
130:                if (finder.size() > 1) {
131:                    ASTPrimaryExpression apeLeft = finder.get(0);
132:                    if (matchName(apeLeft, varname)) {
133:                        ASTPrimaryExpression apeRight = finder.get(1);
134:                        if ((apeRight.jjtGetNumChildren() == 1)
135:                                && (apeRight.jjtGetChild(0) instanceof  ASTPrimaryPrefix)) {
136:                            ASTPrimaryPrefix pp2 = (ASTPrimaryPrefix) apeRight
137:                                    .jjtGetChild(0);
138:                            if ((pp2.jjtGetNumChildren() == 1)
139:                                    && (pp2.jjtGetChild(0) instanceof  ASTLiteral)) {
140:                                ASTLiteral lit = (ASTLiteral) pp2
141:                                        .jjtGetChild(0);
142:                                if ((lit.jjtGetNumChildren() == 1)
143:                                        && (lit.jjtGetChild(0) instanceof  ASTNullLiteral)) {
144:                                    return true;
145:                                }
146:                            }
147:                        }
148:                    }
149:                }
150:                return false;
151:            }
152:
153:            private boolean matchName(ASTPrimaryExpression ape, String name) {
154:                if ((ape.jjtGetNumChildren() == 1)
155:                        && (ape.jjtGetChild(0) instanceof  ASTPrimaryPrefix)) {
156:                    ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ape.jjtGetChild(0);
157:                    String name2 = getNameFromPrimaryPrefix(pp);
158:                    if (name2 != null && name2.equals(name)) {
159:                        return true;
160:                    }
161:                }
162:                return false;
163:            }
164:
165:            private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
166:                if ((pp.jjtGetNumChildren() == 1)
167:                        && (pp.jjtGetChild(0) instanceof  ASTName)) {
168:                    return ((ASTName) pp.jjtGetChild(0)).getImage();
169:                }
170:                return null;
171:            }
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.