View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.basic;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import net.sourceforge.pmd.lang.ast.Node;
10  import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator;
11  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
12  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
13  import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
14  import net.sourceforge.pmd.lang.java.ast.ASTExpression;
15  import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
16  import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
17  import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
18  import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
19  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
20  import net.sourceforge.pmd.lang.java.ast.ASTName;
21  import net.sourceforge.pmd.lang.java.ast.ASTNullLiteral;
22  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
23  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
24  import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
25  import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
26  import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
27  import net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement;
28  import net.sourceforge.pmd.lang.java.ast.ASTType;
29  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
30  import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
31  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
32  
33  /**
34   * void method() {
35   * if(x == null) {
36   * synchronized(this){
37   * if(x == null) {
38   * x = new | method();
39   * }
40   * }
41   * }
42   * 1.  The error is when one uses the value assigned within a synchronized
43   * section, outside of a synchronized section.
44   * if(x == null) is outside of synchronized section
45   * x = new | method();
46   * <p/>
47   * <p/>
48   * Very very specific check for double checked locking.
49   *
50   * @author CL Gilbert (dnoyeb@users.sourceforge.net)
51   */
52  public class DoubleCheckedLockingRule extends AbstractJavaRule {
53  
54      private List<String> volatileFields;
55  
56      @Override
57      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
58          if (node.isInterface()) {
59              return data;
60          }
61          return super.visit(node, data);
62      }
63  
64      @Override
65      public Object visit(ASTCompilationUnit compilationUnit, Object data) {
66          if (this.volatileFields == null) {
67              this.volatileFields = new ArrayList<String>(0);
68          } else {
69              this.volatileFields.clear();
70          }
71          return super.visit(compilationUnit, data);
72      }
73  
74      @Override
75      public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) {
76          if (fieldDeclaration.isVolatile()) {
77              for (ASTVariableDeclaratorId declarator : fieldDeclaration
78                      .findDescendantsOfType(ASTVariableDeclaratorId.class)) {
79                  this.volatileFields.add(declarator.getImage());
80              }
81          }
82          return super.visit(fieldDeclaration, data);
83      }
84  
85      @Override
86      public Object visit(ASTMethodDeclaration node, Object data) {
87          if (node.getResultType().isVoid()) {
88              return super.visit(node, data);
89          }
90  
91          ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
92          if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
93              return super.visit(node, data);
94          }
95  
96          List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
97          if (rsl.size() != 1) {
98              return super.visit(node, data);
99          }
100         ASTReturnStatement rs = rsl.get(0);
101 
102         List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
103         ASTPrimaryExpression ape = pel.get(0);
104         Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
105         String returnVariableName = null;
106         if (lastChild instanceof ASTPrimaryPrefix) {
107             returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
108         }
109         // With Java5 and volatile keyword, DCL is no longer an issue
110         if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
111             return super.visit(node, data);
112         }
113         // if the return variable is local and only written with the volatile field, then it's ok, too
114         if (checkLocalVariableUsage(node, returnVariableName)) {
115             return super.visit(node, data);
116         }
117         List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
118         if (isl.size() == 2) {
119             ASTIfStatement is = isl.get(0);
120             if (ifVerify(is, returnVariableName)) {
121                 // find synchronized
122                 List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
123                 if (ssl.size() == 1) {
124                     ASTSynchronizedStatement ss = ssl.get(0);
125                     isl = ss.findDescendantsOfType(ASTIfStatement.class);
126                     if (isl.size() == 1) {
127                         ASTIfStatement is2 = isl.get(0);
128                         if (ifVerify(is2, returnVariableName)) {
129                             List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
130                             if (sel.size() == 1) {
131                                 ASTStatementExpression se = sel.get(0);
132                                 if (se.jjtGetNumChildren() == 3) { // primaryExpression,
133                                                                    // AssignmentOperator,
134                                                                    // Expression
135                                     if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
136                                         ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
137                                         if (matchName(pe, returnVariableName)) {
138                                             if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
139                                                 addViolation(data, node);
140                                             }
141                                         }
142                                     }
143                                 }
144                             }
145                         }
146                     }
147                 }
148             }
149         }
150         return super.visit(node, data);
151     }
152 
153     private boolean checkLocalVariableUsage(ASTMethodDeclaration node, String returnVariableName) {
154         List<ASTLocalVariableDeclaration> locals = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
155         ASTVariableInitializer initializer = null;
156         for (ASTLocalVariableDeclaration l : locals) {
157             ASTVariableDeclaratorId id = l.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
158             if (id != null && id.hasImageEqualTo(returnVariableName)) {
159                 initializer = l.getFirstDescendantOfType(ASTVariableInitializer.class);
160                 break;
161             }
162         }
163         // the return variable name doesn't seem to be a local variable
164         if (initializer == null) return false;
165 
166         // verify the value with which the local variable is initialized
167         if (initializer.jjtGetNumChildren() > 0 && initializer.jjtGetChild(0) instanceof ASTExpression
168                 && initializer.jjtGetChild(0).jjtGetNumChildren() > 0
169                 && initializer.jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryExpression
170                 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0
171                 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryPrefix
172                 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0
173                 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
174             ASTName name = (ASTName)initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
175             if (name == null || !volatileFields.contains(name.getImage())) {
176                 return false;
177             }
178         } else {
179             // not a simple assignment
180             return false;
181         }
182 
183         // now check every usage/assignment of the variable
184         List<ASTName> names = node.findDescendantsOfType(ASTName.class);
185         for (ASTName n : names) {
186             if (!n.hasImageEqualTo(returnVariableName)) continue;
187 
188             Node expression = n.getNthParent(3);
189             if (expression instanceof ASTEqualityExpression) continue;
190             if (expression instanceof ASTStatementExpression) {
191                 if (expression.jjtGetNumChildren() > 2 && expression.jjtGetChild(1) instanceof ASTAssignmentOperator) {
192                     ASTName value = expression.jjtGetChild(2).getFirstDescendantOfType(ASTName.class);
193                     if (value == null || !volatileFields.contains(value.getImage())) {
194                         return false;
195                     }
196                 }
197             }
198         }
199 
200         return true;
201     }
202 
203     private boolean ifVerify(ASTIfStatement is, String varname) {
204         List<ASTPrimaryExpression> finder = is.findDescendantsOfType(ASTPrimaryExpression.class);
205         if (finder.size() > 1) {
206             ASTPrimaryExpression nullStmt = findNonVariableStmt(varname, finder.get(0), finder.get(1));
207             if (nullStmt != null) {
208                 if (nullStmt.jjtGetNumChildren() == 1 && nullStmt.jjtGetChild(0) instanceof ASTPrimaryPrefix) {
209                     ASTPrimaryPrefix pp2 = (ASTPrimaryPrefix) nullStmt.jjtGetChild(0);
210                     if (pp2.jjtGetNumChildren() == 1 && pp2.jjtGetChild(0) instanceof ASTLiteral) {
211                         ASTLiteral lit = (ASTLiteral) pp2.jjtGetChild(0);
212                         if (lit.jjtGetNumChildren() == 1 && lit.jjtGetChild(0) instanceof ASTNullLiteral) {
213                             return true;
214                         }
215                     }
216                 }
217             }
218         }
219         return false;
220     }
221 
222     /**
223      * <p>
224      * Sort out if apeLeft or apeRight are variable with the provided
225      * 'variableName'.
226      * </p>
227      * 
228      * @param variableName
229      * @param apeLeft
230      * @param apeRight
231      * @return reference from either apeLeft or apeRight, if one of them match,
232      *         or 'null', if none match.
233      */
234     private ASTPrimaryExpression findNonVariableStmt(String variableName, ASTPrimaryExpression apeLeft,
235             ASTPrimaryExpression apeRight) {
236         if (matchName(apeLeft, variableName)) {
237             return apeRight;
238         } else if (matchName(apeRight, variableName)) {
239             return apeLeft;
240         }
241         return null;
242     }
243 
244     private boolean matchName(ASTPrimaryExpression ape, String name) {
245         if (ape.jjtGetNumChildren() == 1 && ape.jjtGetChild(0) instanceof ASTPrimaryPrefix) {
246             ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ape.jjtGetChild(0);
247             String name2 = getNameFromPrimaryPrefix(pp);
248             if (name2 != null && name2.equals(name)) {
249                 return true;
250             }
251         }
252         return false;
253     }
254 
255     private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
256         if (pp.jjtGetNumChildren() == 1 && pp.jjtGetChild(0) instanceof ASTName) {
257             return ((ASTName) pp.jjtGetChild(0)).getImage();
258         }
259         return null;
260     }
261 }