Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

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