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

file stats: | LOC: | 245 |  | Methods: | 36
| NCLOC: | 155 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

AbstractRule.java

90%

92.7%

91.7%

92.1%

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd;
5 |  |
6 |  | import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
7 |  | import net.sourceforge.pmd.ast.ASTCompilationUnit;
8 |  | import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
9 |  | import net.sourceforge.pmd.ast.Node;
10 |  | import net.sourceforge.pmd.ast.SimpleNode;
11 |  |
12 |  | import java.text.MessageFormat;
13 |  | import java.util.Iterator;
14 |  | import java.util.List;
15 |  | import java.util.Properties;
16 |  |
17 |  | public abstract class AbstractRule extends JavaParserVisitorAdapter implements Rule {
18 |  |
19 |  | protected String name = getClass().getName();
20 |  | protected Properties properties = new Properties();
21 |  | protected String message;
22 |  | protected String description;
23 |  | protected String example;
24 |  | protected String ruleSetName;
25 |  | protected boolean include;
26 |  | protected boolean usesDFA;
27 |  | protected int priority = LOWEST_PRIORITY;
28 |  | protected String externalInfoUrl;
29 |  |
30 | 1 | public String getRuleSetName() {
31 | 1 | return ruleSetName;
32 |  | }
33 |  |
34 | 1483 | public void setRuleSetName(String ruleSetName) {
35 | 1483 | this.ruleSetName = ruleSetName;
36 |  | }
37 |  |
38 | 2 | public String getDescription() {
39 | 2 | return description;
40 |  | }
41 |  |
42 | 1476 | public void setDescription(String description) {
43 | 1476 | this.description = description;
44 |  | }
45 |  |
46 | 1 | public String getExample() {
47 | 1 | return example;
48 |  | }
49 |  |
50 | 1508 | public void setExample(String example) {
51 | 1508 | this.example = example;
52 |  | }
53 |  |
54 | 555 | public boolean hasProperty(String name) {
55 | 555 | return properties.containsKey(name);
56 |  | }
57 |  |
58 | 720 | public void addProperty(String name, String value) {
59 | 720 | properties.setProperty(name, value);
60 |  | }
61 |  |
62 | 2 | public void addProperties(Properties properties) {
63 | 2 | this.properties.putAll(properties);
64 |  | }
65 |  |
66 | 235 | public double getDoubleProperty(String name) {
67 | 235 | return Double.parseDouble(properties.getProperty(name));
68 |  | }
69 |  |
70 | 182 | public int getIntProperty(String name) {
71 | 182 | return Integer.parseInt(properties.getProperty(name));
72 |  | }
73 |  |
74 | 1 | public boolean getBooleanProperty(String name) {
75 | 1 | return Boolean.valueOf(properties.getProperty(name)).booleanValue();
76 |  | }
77 |  |
78 | 70 | public String getStringProperty(String name) {
79 | 70 | return properties.getProperty(name);
80 |  | }
81 |  |
82 | 1035 | public String getName() {
83 | 1035 | return name;
84 |  | }
85 |  |
86 | 1504 | public void setName(String name) {
87 | 1504 | this.name = name;
88 |  | }
89 |  |
90 | 298 | public String getMessage() {
91 | 298 | return message;
92 |  | }
93 |  |
94 | 1507 | public void setMessage(String message) {
95 | 1507 | this.message = message;
96 |  | }
97 |  |
98 | 5 | public String getExternalInfoUrl() {
99 | 5 | return externalInfoUrl;
100 |  | }
101 |  |
102 | 1482 | public void setExternalInfoUrl(String url) {
103 | 1482 | this.externalInfoUrl = url;
104 |  | }
105 |  |
106 |  | /**
107 |  | * Test if rules are equals. Rules are equals if
108 |  | * 1. they have the same implementation class
109 |  | * 2. they have the same name
110 |  | * 3. they have the same priority
111 |  | * 4. they share the same properties/values
112 |  | */
113 | 16 | public boolean equals(Object o) {
114 | 16 | if (o == null) {
115 | 1 | return false; // trivial
116 |  | }
117 |  |
118 | 15 | if (this == o) {
119 | 5 | return true; // trivial
120 |  | }
121 |  |
122 | 10 | Rule rule = null;
123 | 10 | boolean equality = this.getClass().getName().equals(o.getClass().getName());
124 |  |
125 | 10 | if (equality) {
126 | 8 | rule = (Rule) o;
127 | 8 | equality = this.getName().equals(rule.getName())
128 |  | && this.getPriority() == rule.getPriority()
129 |  | && this.getProperties().equals(rule.getProperties());
130 |  | }
131 |  |
132 | 10 | return equality;
133 |  | }
134 |  |
135 |  | /**
136 |  | * Return a hash code to conform to equality. Try with a string.
137 |  | */
138 | 20 | public int hashCode() {
139 | 20 | String s = this.getClass().getName() + this.getName() + String.valueOf(this.getPriority()) + this.getProperties().toString();
140 | 20 | return s.hashCode();
141 |  | }
142 |  |
143 | 589 | public void apply(List acus, RuleContext ctx) {
144 | 589 | visitAll(acus, ctx);
145 |  | }
146 |  |
147 |  |
148 | 30 | public Properties getProperties() {
149 | 30 | return properties;
150 |  | }
151 |  |
152 | 0 | public boolean include() {
153 | 0 | return include;
154 |  | }
155 |  |
156 | 0 | public void setInclude(boolean include) {
157 | 0 | this.include = include;
158 |  | }
159 |  |
160 | 1537 | public int getPriority() {
161 | 1537 | return priority;
162 |  | }
163 |  |
164 | 0 | public String getPriorityName() {
165 | 0 | return PRIORITIES[getPriority() - 1];
166 |  | }
167 |  |
168 | 1498 | public void setPriority(int priority) {
169 | 1498 | this.priority = priority;
170 |  | }
171 |  |
172 | 2 | public void setUsesDFA() {
173 | 2 | this.usesDFA = true;
174 |  | }
175 |  |
176 | 610 | public boolean usesDFA() {
177 | 610 | return this.usesDFA;
178 |  | }
179 |  |
180 | 764 | protected void visitAll(List acus, RuleContext ctx) {
181 | 764 | for (Iterator i = acus.iterator(); i.hasNext();) {
182 | 606 | ASTCompilationUnit node = (ASTCompilationUnit) i.next();
183 | 606 | visit(node, ctx);
184 |  | }
185 |  | }
186 |  |
187 |  | /**
188 |  | * Adds a violation to the report.
189 |  | *
190 |  | * @param ctx the RuleContext
191 |  | * @param node the node that produces the violation
192 |  | */
193 | 132 | protected final void addViolation(Object data, SimpleNode node) {
194 | 132 | RuleContext ctx = (RuleContext) data;
195 | 132 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node));
196 |  | }
197 |  |
198 |  | /**
199 |  | * Adds a violation to the report.
200 |  | *
201 |  | * @param ctx the RuleContext
202 |  | * @param node the node that produces the violation
203 |  | * @param msg specific message to put in the report
204 |  | */
205 | 2147 | protected final void addViolationWithMessage(Object data, SimpleNode node, String msg) {
206 | 2147 | RuleContext ctx = (RuleContext) data;
207 | 2147 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node, msg));
208 |  | }
209 |  |
210 |  | /**
211 |  | * Adds a violation to the report.
212 |  | *
213 |  | * @param ctx the RuleContext
214 |  | * @param node the node that produces the violation
215 |  | * @param embed a variable to embed in the rule violation message
216 |  | */
217 | 97 | protected final void addViolation(Object data, SimpleNode node, String embed) {
218 | 97 | RuleContext ctx = (RuleContext) data;
219 | 97 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node, MessageFormat.format(getMessage(), new Object[]{embed})));
220 |  | }
221 |  |
222 |  | /**
223 |  | * Adds a violation to the report.
224 |  | *
225 |  | * @param ctx the RuleContext
226 |  | * @param node the node that produces the violation, may be null, in which case all line and column info will be set to zero
227 |  | * @param args objects to embed in the rule violation message
228 |  | */
229 | 45 | protected final void addViolation(Object data, Node node, Object[] args) {
230 | 45 | RuleContext ctx = (RuleContext) data;
231 | 45 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, (SimpleNode) node, MessageFormat.format(getMessage(), args)));
232 |  | }
233 |  |
234 |  | /**
235 |  | * Gets the Image of the first parent node of type ASTClassOrInterfaceDeclaration or <code>null</code>
236 |  | *
237 |  | * @param node the node which will be searched
238 |  | */
239 | 12 | protected final String getDeclaringType(SimpleNode node) {
240 | 12 | ASTClassOrInterfaceDeclaration c = (ASTClassOrInterfaceDeclaration) node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
241 | 12 | if (c != null)
242 | 12 | return c.getImage();
243 | 0 | return null;
244 |  | }
245 |  | }