1 6 package net.sourceforge.pmd.rules.sunsecure; 7 8 import net.sourceforge.pmd.AbstractRule; 9 import net.sourceforge.pmd.ast.ASTFieldDeclaration; 10 import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; 11 import net.sourceforge.pmd.ast.ASTName; 12 import net.sourceforge.pmd.ast.ASTPrimarySuffix; 13 import net.sourceforge.pmd.ast.ASTReturnStatement; 14 import net.sourceforge.pmd.ast.ASTTypeDeclaration; 15 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; 16 import net.sourceforge.pmd.ast.SimpleNode; 17 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 26 public abstract class AbstractSunSecureRule extends AbstractRule { 27 28 35 protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) { 36 final List fds = typeDeclaration.findChildrenOfType(ASTFieldDeclaration.class); 37 if (fds != null) { 38 for (Iterator it = fds.iterator(); it.hasNext();) { 39 final ASTFieldDeclaration fd = (ASTFieldDeclaration) it.next(); 40 final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) fd.getFirstChildOfType(ASTVariableDeclaratorId.class); 41 if (vid != null && vid.hasImageEqualTo(varName)) { 42 return true; 43 } 44 } 45 } 46 return false; 47 } 48 49 50 60 protected final String getReturnedVariableName(ASTReturnStatement ret) { 61 final ASTName n = (ASTName) ret.getFirstChildOfType(ASTName.class); 62 if (n != null) 63 return n.getImage(); 64 final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class); 65 if (ps != null) 66 return ps.getImage(); 67 return null; 68 } 69 70 78 protected boolean isLocalVariable(String vn, SimpleNode node) { 79 final List lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class); 80 if (lvars != null) { 81 for (Iterator it = lvars.iterator(); it.hasNext();) { 82 final ASTLocalVariableDeclaration lvd = (ASTLocalVariableDeclaration) it.next(); 83 final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) lvd.getFirstChildOfType(ASTVariableDeclaratorId.class); 84 if (vid != null && vid.hasImageEqualTo(vn)) { 85 return true; 86 } 87 } 88 } 89 return false; 90 } 91 92 98 protected String getFirstNameImage(SimpleNode n) { 99 ASTName name = (ASTName) n.getFirstChildOfType(ASTName.class); 100 if (name != null) 101 return name.getImage(); 102 return null; 103 } 104 105 } 106 | Popular Tags |