Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 266   Methods: 40
NCLOC: 185   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractRule.java 70.8% 88.9% 90% 86%
coverage coverage
 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.ASTMethodDeclaration;
 9    import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
 10    import net.sourceforge.pmd.ast.SimpleNode;
 11    import net.sourceforge.pmd.symboltable.MethodScope;
 12   
 13    import java.text.MessageFormat;
 14    import java.util.Iterator;
 15    import java.util.List;
 16    import java.util.Properties;
 17   
 18    public abstract class AbstractRule extends JavaParserVisitorAdapter implements Rule {
 19   
 20    protected String name = getClass().getName();
 21    protected Properties properties = new Properties();
 22    protected String message;
 23    protected String description;
 24    protected String example;
 25    protected String ruleSetName;
 26    protected boolean include;
 27    protected boolean usesDFA;
 28    protected int priority = LOWEST_PRIORITY;
 29    protected String externalInfoUrl;
 30   
 31  14 public String getRuleSetName() {
 32  14 return ruleSetName;
 33    }
 34   
 35  2608 public void setRuleSetName(String ruleSetName) {
 36  2608 this.ruleSetName = ruleSetName;
 37    }
 38   
 39  2 public String getDescription() {
 40  2 return description;
 41    }
 42   
 43  2603 public void setDescription(String description) {
 44  2603 this.description = description;
 45    }
 46   
 47  1 public String getExample() {
 48  1 return example;
 49    }
 50   
 51  2624 public void setExample(String example) {
 52  2624 this.example = example;
 53    }
 54   
 55  554 public boolean hasProperty(String name) {
 56  554 return properties.containsKey(name);
 57    }
 58   
 59  2172 public void addProperty(String name, String value) {
 60  2172 properties.setProperty(name, value);
 61    }
 62   
 63  2 public void addProperties(Properties properties) {
 64  2 this.properties.putAll(properties);
 65    }
 66   
 67  235 public double getDoubleProperty(String name) {
 68  235 return Double.parseDouble(properties.getProperty(name));
 69    }
 70   
 71  131 public int getIntProperty(String name) {
 72  131 return Integer.parseInt(properties.getProperty(name));
 73    }
 74   
 75  11 public boolean getBooleanProperty(String name) {
 76  11 return Boolean.valueOf(properties.getProperty(name)).booleanValue();
 77    }
 78   
 79  133 public String getStringProperty(String name) {
 80  133 return properties.getProperty(name);
 81    }
 82   
 83  5060 public String getName() {
 84  5060 return name;
 85    }
 86   
 87  2629 public void setName(String name) {
 88  2629 this.name = name;
 89    }
 90   
 91  608 public String getMessage() {
 92  608 return message;
 93    }
 94   
 95  2661 public void setMessage(String message) {
 96  2661 this.message = message;
 97    }
 98   
 99  13 public String getExternalInfoUrl() {
 100  13 return externalInfoUrl;
 101    }
 102  2607 public void setExternalInfoUrl(String url) {
 103  2607 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  17 public boolean equals(Object o) {
 114  17 if (o == null) {
 115  1 return false; // trivial
 116    }
 117   
 118  16 if (this == o) {
 119  7 return true; // trivial
 120    }
 121   
 122  9 Rule rule = null;
 123  9 boolean equality = this.getClass().getName().equals(o.getClass().getName());
 124   
 125  9 if (equality) {
 126  7 rule = (Rule) o;
 127  7 equality = this.getName().equals(rule.getName())
 128    && this.getPriority() == rule.getPriority()
 129    && this.getProperties().equals(rule.getProperties());
 130    }
 131   
 132  9 return equality;
 133    }
 134   
 135    /**
 136    * Return a hash code to conform to equality. Try with a string.
 137    */
 138  3435 public int hashCode() {
 139  3435 String s = this.getClass().getName() + this.getName() + String.valueOf(this.getPriority()) + this.getProperties().toString();
 140  3435 return s.hashCode();
 141    }
 142   
 143  736 public void apply(List acus, RuleContext ctx) {
 144  736 visitAll(acus, ctx);
 145    }
 146   
 147  88 public RuleViolation createRuleViolation(RuleContext ctx, SimpleNode node) {
 148  88 String packageName = node.getScope().getEnclosingSourceFileScope().getPackageName() == null ? "" : node.getScope().getEnclosingSourceFileScope().getPackageName();
 149  88 RuleViolation v = new RuleViolation(this, ctx, packageName, findClassName(node), findMethodName(node));
 150  88 extractNodeInfo(v, node);
 151  88 return v;
 152    }
 153   
 154  3130 public RuleViolation createRuleViolation(RuleContext ctx, SimpleNode node, String specificDescription) {
 155  3130 String packageName = node.getScope().getEnclosingSourceFileScope().getPackageName() == null ? "" : node.getScope().getEnclosingSourceFileScope().getPackageName();
 156  3130 RuleViolation rv = new RuleViolation(this, node.getBeginLine(), specificDescription, ctx, packageName, findClassName(node), findMethodName(node));
 157  3130 extractNodeInfo(rv, node);
 158  3130 return rv;
 159    }
 160   
 161  0 public RuleViolation createRuleViolation(RuleContext ctx, SimpleNode node, String variableName, String specificDescription) {
 162  0 String packageName = node.getScope().getEnclosingSourceFileScope().getPackageName() == null ? "" : node.getScope().getEnclosingSourceFileScope().getPackageName();
 163  0 return new RuleViolation(this, node.getBeginLine(), node.getEndLine(), variableName, specificDescription, ctx, packageName, findClassName(node), findMethodName(node));
 164    }
 165   
 166  3218 private String findMethodName(SimpleNode node) {
 167  3218 return node.getFirstParentOfType(ASTMethodDeclaration.class) == null ? "" : ((MethodScope)node.getScope().getEnclosingMethodScope()).getName();
 168    }
 169   
 170  3218 private String findClassName(SimpleNode node) {
 171  3218 String className;
 172  3218 if (node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) == null) {
 173    // This takes care of nodes which are outside a class definition - i.e., import declarations
 174  2875 className = "";
 175    } else {
 176    // default to symbol table lookup
 177  343 className = node.getScope().getEnclosingClassScope().getClassName() == null ? "" : node.getScope().getEnclosingClassScope().getClassName();
 178    }
 179  3218 return className;
 180    }
 181   
 182  3445 public Properties getProperties() {
 183  3445 return properties;
 184    }
 185   
 186  0 public boolean include() {
 187  0 return include;
 188    }
 189   
 190  0 public void setInclude(boolean include) {
 191  0 this.include = include;
 192    }
 193   
 194  3462 public int getPriority() {
 195  3462 return priority;
 196    }
 197   
 198  0 public String getPriorityName() {
 199  0 return PRIORITIES[getPriority() - 1];
 200    }
 201   
 202  2621 public void setPriority(int priority) {
 203  2621 this.priority = priority;
 204    }
 205   
 206  2 public void setUsesDFA() {
 207  2 this.usesDFA = true;
 208    }
 209   
 210  757 public boolean usesDFA() {
 211  757 return this.usesDFA;
 212    }
 213   
 214  911 protected void visitAll(List acus, RuleContext ctx) {
 215  911 for (Iterator i = acus.iterator(); i.hasNext();) {
 216  753 ASTCompilationUnit node = (ASTCompilationUnit) i.next();
 217  753 visit(node, ctx);
 218    }
 219    }
 220   
 221    /**
 222    * Adds a violation to the report.
 223    *
 224    * @param ctx the RuleContext
 225    * @param node the node that produces the violation, may be null, in which case all line and column info will be set to zero
 226    */
 227  87 protected final void addViolation(Object data, SimpleNode node) {
 228  87 RuleContext ctx = (RuleContext)data;
 229  87 ctx.getReport().addRuleViolation(createRuleViolation(ctx, node));
 230    }
 231   
 232    /**
 233    * Adds a violation to the report.
 234    *
 235    * @param ctx the RuleContext
 236    * @param node the node that produces the violation, may be null, in which case all line and column info will be set to zero
 237    * @param embed a message to embed in the rule violation message
 238    */
 239  279 protected final void addViolation(Object data, SimpleNode node, String embed) {
 240  279 RuleContext ctx = (RuleContext)data;
 241  279 ctx.getReport().addRuleViolation(createRuleViolation(ctx, node, MessageFormat.format(getMessage(), new Object[]{embed})));
 242    }
 243   
 244    /**
 245    * Gets the Image of the first parent node of type ASTClassOrInterfaceDeclaration or <code>null</code>
 246    *
 247    * @param node the node which will be searched
 248    */
 249  12 protected final String getDeclaringType(SimpleNode node) {
 250  12 ASTClassOrInterfaceDeclaration c = (ASTClassOrInterfaceDeclaration) node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
 251  12 if (c!=null)
 252  12 return c.getImage();
 253  0 return null;
 254    }
 255   
 256  3218 private final void extractNodeInfo(RuleViolation v, SimpleNode n) {
 257  3218 if (n==null) {
 258  0 v.setLine(0);
 259  0 v.setColumnInfo(0, 0);
 260    } else {
 261  3218 v.setLine(n.getBeginLine());
 262  3218 v.setColumnInfo(n.getBeginColumn(), n.getEndColumn());
 263    }
 264    }
 265   
 266    }