Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 105 |  | Methods: | 4
| NCLOC: | 55 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
AbstractSunSecureRule.java | 77.8% | 96.3% | 100% | 89.8% |

1 |  | /*
2 |  | * Created on Jan 17, 2005
3 |  | *
4 |  | * $Id: AbstractSunSecureRule.java,v 1.10 2006/10/17 01:07:18 hooperbloob Exp $
5 |  | */
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 |  | /**
22 |  | * Utility methods for the package
23 |  | *
24 |  | * @author mgriffa
25 |  | */
26 |  | public abstract class AbstractSunSecureRule extends AbstractRule {
27 |  |
28 |  | /**
29 |  | * Tells if the type declaration has a field with varName.
30 |  | *
31 |  | * @param varName the name of the field to search
32 |  | * @param typeDeclaration the type declaration
33 |  | * @return <code>true</code> if there is a field in the type declaration named varName, <code>false</code> in other case
34 |  | */
35 | 9 | protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
36 | 9 | final List fds = typeDeclaration.findChildrenOfType(ASTFieldDeclaration.class);
37 | 9 | if (fds != null) {
38 | 9 | for (Iterator it = fds.iterator(); it.hasNext();) {
39 | 7 | final ASTFieldDeclaration fd = (ASTFieldDeclaration) it.next();
40 | 7 | final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) fd.getFirstChildOfType(ASTVariableDeclaratorId.class);
41 | 7 | if (vid != null && vid.hasImageEqualTo(varName)) {
42 | 6 | return true;
43 |  | }
44 |  | }
45 |  | }
46 | 3 | return false;
47 |  | }
48 |  |
49 |  |
50 |  | /**
51 |  | * Gets the name of the variable returned.
52 |  | * Some examples: <br>
53 |  | * for this.foo returns foo <br>
54 |  | * for foo returns foo <br>
55 |  | * for foo.bar returns foo.bar
56 |  | *
57 |  | * @param ret a return statement to evaluate
58 |  | * @return the name of the variable associated or <code>null</code> if it cannot be detected
59 |  | */
60 | 9 | protected final String getReturnedVariableName(ASTReturnStatement ret) {
61 | 9 | final ASTName n = (ASTName) ret.getFirstChildOfType(ASTName.class);
62 | 9 | if (n != null)
63 | 6 | return n.getImage();
64 | 3 | final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class);
65 | 3 | if (ps != null)
66 | 3 | return ps.getImage();
67 | 0 | return null;
68 |  | }
69 |  |
70 |  | /**
71 |  | * TODO modify usages to use symbol table
72 |  | * Tells if the variable name is a local variable declared in the method.
73 |  | *
74 |  | * @param vn the variable name
75 |  | * @param node the ASTMethodDeclaration where the local variable name will be searched
76 |  | * @return <code>true</code> if the method declaration contains any local variable named vn and <code>false</code> in other case
77 |  | */
78 | 16 | protected boolean isLocalVariable(String vn, SimpleNode node) {
79 | 16 | final List lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
80 | 16 | if (lvars != null) {
81 | 16 | for (Iterator it = lvars.iterator(); it.hasNext();) {
82 | 3 | final ASTLocalVariableDeclaration lvd = (ASTLocalVariableDeclaration) it.next();
83 | 3 | final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) lvd.getFirstChildOfType(ASTVariableDeclaratorId.class);
84 | 3 | if (vid != null && vid.hasImageEqualTo(vn)) {
85 | 3 | return true;
86 |  | }
87 |  | }
88 |  | }
89 | 13 | return false;
90 |  | }
91 |  |
92 |  | /**
93 |  | * Gets the image of the first ASTName node found by {@link SimpleNode#getFirstChildOfType(Class)}
94 |  | *
95 |  | * @param n the node to search
96 |  | * @return the image of the first ASTName or <code>null</code>
97 |  | */
98 | 14 | protected String getFirstNameImage(SimpleNode n) {
99 | 14 | ASTName name = (ASTName) n.getFirstChildOfType(ASTName.class);
100 | 14 | if (name != null)
101 | 12 | return name.getImage();
102 | 2 | return null;
103 |  | }
104 |  |
105 |  | }