1 4 package net.sourceforge.pmd.rules.design; 5 6 import net.sourceforge.pmd.AbstractRule; 7 import net.sourceforge.pmd.ast.ASTClassOrInterfaceType; 8 import net.sourceforge.pmd.ast.ASTFieldDeclaration; 9 import net.sourceforge.pmd.ast.ASTMethodDeclaration; 10 import net.sourceforge.pmd.ast.ASTSynchronizedStatement; 11 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; 12 import net.sourceforge.pmd.ast.SimpleNode; 13 import net.sourceforge.pmd.symboltable.NameOccurrence; 14 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 19 31 public class UnsynchronizedStaticDateFormatter extends AbstractRule { 32 33 private static Set targets = new HashSet (); 34 static { 35 targets.add("DateFormat"); 36 targets.add("SimpleDateFormat"); 37 targets.add("java.text.DateFormat"); 38 targets.add("java.text.SimpleDateFormat"); 39 } 40 41 public Object visit(ASTFieldDeclaration node, Object data) { 42 if (!node.isStatic()) { 43 return data; 44 } 45 ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.getFirstChildOfType(ASTClassOrInterfaceType.class); 46 if (cit == null || !targets.contains(cit.getImage())) { 47 return data; 48 } 49 ASTVariableDeclaratorId var = (ASTVariableDeclaratorId) node.getFirstChildOfType(ASTVariableDeclaratorId.class); 50 for (Iterator i = var.getUsages().iterator(); i.hasNext();) { 51 NameOccurrence occ = (NameOccurrence) i.next(); 52 SimpleNode n = occ.getLocation(); 53 if ((ASTSynchronizedStatement) n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) { 54 continue; 55 } 56 ASTMethodDeclaration method = (ASTMethodDeclaration) n.getFirstParentOfType(ASTMethodDeclaration.class); 57 if (method != null && !method.isSynchronized()) { 58 addViolation(data, n); 59 } 60 } 61 return data; 62 } 63 } | Popular Tags |