1 package com.puppycrawl.tools.checkstyle.checks.coding; 20 21 import antlr.collections.AST; 22 import com.puppycrawl.tools.checkstyle.api.Check; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 25 26 38 public class DoubleCheckedLockingCheck extends Check 39 { 40 41 public int[] getDefaultTokens() 42 { 43 return new int[]{TokenTypes.LITERAL_IF}; 44 } 45 46 47 public void visitToken(DetailAST aAST) 48 { 49 final DetailAST synchronizedAST = 50 getLowestParent(aAST, TokenTypes.LITERAL_SYNCHRONIZED); 51 if (synchronizedAST == null) { 52 return; 53 } 54 55 final DetailAST ifAST = 56 getLowestParent(synchronizedAST, TokenTypes.LITERAL_IF); 57 if (ifAST == null) { 58 return; 59 } 60 61 if (getIfCondition(aAST).equalsTree(getIfCondition(ifAST))) { 62 log(aAST.getLineNo(), aAST.getColumnNo(), 63 "doublechecked.locking.avoid"); 64 } 65 } 66 67 72 private AST getIfCondition(DetailAST aIfAST) 73 { 74 return aIfAST.getFirstChild().getNextSibling(); 75 } 76 77 83 private DetailAST getLowestParent(DetailAST aAST, int aTokenType) 84 { 85 DetailAST synchronizedParent = aAST; 86 while ((synchronizedParent != null) 87 && (synchronizedParent.getType() != aTokenType)) 88 { 89 synchronizedParent = synchronizedParent.getParent(); 90 } 91 return synchronizedParent; 92 } 93 } 94 | Popular Tags |