Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 222 |  | Methods: | 33
| NCLOC: | 135 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
CommonAbstractRule.java | 0% | 34% | 45.5% | 36% |

1 |  | package net.sourceforge.pmd;
2 |  |
3 |  | import net.sourceforge.pmd.ast.Node;
4 |  | import net.sourceforge.pmd.ast.SimpleNode;
5 |  |
6 |  | import java.text.MessageFormat;
7 |  | import java.util.Properties;
8 |  |
9 |  | /**
10 |  | * Basic abstract implementation of all parser-independent methods of the
11 |  | * Rule interface.
12 |  | *
13 |  | * @author pieter_van_raemdonck - Application Engineers NV/SA - www.ae.be
14 |  | */
15 |  | public abstract class CommonAbstractRule implements Rule {
16 |  |
17 |  | protected String name = getClass().getName();
18 |  | protected Properties properties = new Properties();
19 |  | protected String message;
20 |  | protected String description;
21 |  | protected String example;
22 |  | protected String ruleSetName;
23 |  | protected boolean include;
24 |  | protected boolean usesDFA;
25 |  | protected int priority = LOWEST_PRIORITY;
26 |  | protected String externalInfoUrl;
27 |  |
28 | 0 | public String getRuleSetName() {
29 | 0 | return ruleSetName;
30 |  | }
31 |  |
32 | 2623 | public void setRuleSetName(String ruleSetName) {
33 | 2623 | this.ruleSetName = ruleSetName;
34 |  | }
35 |  |
36 | 0 | public String getDescription() {
37 | 0 | return description;
38 |  | }
39 |  |
40 | 2623 | public void setDescription(String description) {
41 | 2623 | this.description = description;
42 |  | }
43 |  |
44 | 0 | public String getExample() {
45 | 0 | return example;
46 |  | }
47 |  |
48 | 2607 | public void setExample(String example) {
49 | 2607 | this.example = example;
50 |  | }
51 |  |
52 | 0 | public boolean hasProperty(String name) {
53 | 0 | return properties.containsKey(name);
54 |  | }
55 |  |
56 | 2932 | public void addProperty(String name, String value) {
57 | 2932 | properties.setProperty(name, value);
58 |  | }
59 |  |
60 | 0 | public void addProperties(Properties properties) {
61 | 0 | this.properties.putAll(properties);
62 |  | }
63 |  |
64 | 0 | public double getDoubleProperty(String name) {
65 | 0 | return Double.parseDouble(properties.getProperty(name));
66 |  | }
67 |  |
68 | 0 | public int getIntProperty(String name) {
69 | 0 | return Integer.parseInt(properties.getProperty(name));
70 |  | }
71 |  |
72 | 18 | public boolean getBooleanProperty(String name) {
73 | 18 | return Boolean.valueOf(properties.getProperty(name)).booleanValue();
74 |  | }
75 |  |
76 | 126 | public String getStringProperty(String name) {
77 | 126 | return properties.getProperty(name);
78 |  | }
79 |  |
80 | 1256 | public String getName() {
81 | 1256 | return name;
82 |  | }
83 |  |
84 | 2623 | public void setName(String name) {
85 | 2623 | this.name = name;
86 |  | }
87 |  |
88 | 474 | public String getMessage() {
89 | 474 | return message;
90 |  | }
91 |  |
92 | 2630 | public void setMessage(String message) {
93 | 2630 | this.message = message;
94 |  | }
95 |  |
96 | 0 | public String getExternalInfoUrl() {
97 | 0 | return externalInfoUrl;
98 |  | }
99 |  |
100 | 2623 | public void setExternalInfoUrl(String url) {
101 | 2623 | this.externalInfoUrl = url;
102 |  | }
103 |  |
104 |  | /**
105 |  | * Test if rules are equals. Rules are equals if
106 |  | * 1. they have the same implementation class
107 |  | * 2. they have the same name
108 |  | * 3. they have the same priority
109 |  | * 4. they share the same properties/values
110 |  | */
111 | 0 | public boolean equals(Object o) {
112 | 0 | if (o == null) {
113 | 0 | return false; // trivial
114 |  | }
115 |  |
116 | 0 | if (this == o) {
117 | 0 | return true; // trivial
118 |  | }
119 |  |
120 | 0 | Rule rule = null;
121 | 0 | boolean equality = this.getClass().getName().equals(o.getClass().getName());
122 |  |
123 | 0 | if (equality) {
124 | 0 | rule = (Rule) o;
125 | 0 | equality = this.getName().equals(rule.getName())
126 |  | && this.getPriority() == rule.getPriority()
127 |  | && this.getProperties().equals(rule.getProperties());
128 |  | }
129 |  |
130 | 0 | return equality;
131 |  | }
132 |  |
133 |  | /**
134 |  | * Return a hash code to conform to equality. Try with a string.
135 |  | */
136 | 0 | public int hashCode() {
137 | 0 | String s = this.getClass().getName() + this.getName() + String.valueOf(this.getPriority()) + this.getProperties().toString();
138 | 0 | return s.hashCode();
139 |  | }
140 |  |
141 |  |
142 | 0 | public Properties getProperties() {
143 | 0 | return properties;
144 |  | }
145 |  |
146 | 0 | public boolean include() {
147 | 0 | return include;
148 |  | }
149 |  |
150 | 0 | public void setInclude(boolean include) {
151 | 0 | this.include = include;
152 |  | }
153 |  |
154 | 2652 | public int getPriority() {
155 | 2652 | return priority;
156 |  | }
157 |  |
158 | 0 | public String getPriorityName() {
159 | 0 | return PRIORITIES[getPriority() - 1];
160 |  | }
161 |  |
162 | 2624 | public void setPriority(int priority) {
163 | 2624 | this.priority = priority;
164 |  | }
165 |  |
166 | 0 | public void setUsesDFA() {
167 | 0 | this.usesDFA = true;
168 |  | }
169 |  |
170 | 440 | public boolean usesDFA() {
171 | 440 | return this.usesDFA;
172 |  | }
173 |  |
174 |  |
175 |  | /**
176 |  | * Adds a violation to the report.
177 |  | *
178 |  | * @param ctx the RuleContext
179 |  | * @param node the node that produces the violation
180 |  | */
181 | 0 | protected final void addViolation(Object data, SimpleNode node) {
182 | 0 | RuleContext ctx = (RuleContext) data;
183 | 0 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node));
184 |  | }
185 |  |
186 |  | /**
187 |  | * Adds a violation to the report.
188 |  | *
189 |  | * @param ctx the RuleContext
190 |  | * @param node the node that produces the violation
191 |  | * @param msg specific message to put in the report
192 |  | */
193 | 0 | protected final void addViolationWithMessage(Object data, SimpleNode node, String msg) {
194 | 0 | RuleContext ctx = (RuleContext) data;
195 | 0 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node, msg));
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 embed a variable to embed in the rule violation message
204 |  | */
205 | 243 | protected final void addViolation(Object data, SimpleNode node, String embed) {
206 | 243 | RuleContext ctx = (RuleContext) data;
207 | 243 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, node, MessageFormat.format(getMessage(), new Object[]{embed})));
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, may be null, in which case all line and column info will be set to zero
215 |  | * @param args objects to embed in the rule violation message
216 |  | */
217 | 0 | protected final void addViolation(Object data, Node node, Object[] args) {
218 | 0 | RuleContext ctx = (RuleContext) data;
219 | 0 | ctx.getReport().addRuleViolation(new RuleViolation(this, ctx, (SimpleNode) node, MessageFormat.format(getMessage(), args)));
220 |  | }
221 |  |
222 |  | }