Modified Ant task; changed attribute name to clarify its purpose. Added errors to XML report, too; thx to Vladimir for the idea and some code.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1390 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-01-30 15:50:02 +00:00
parent 5fcc8fd851
commit cf2f1ef061
6 changed files with 69 additions and 14 deletions

View File

@ -57,10 +57,10 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<pmd rulesetfiles="rulesets/tmp.xml" failOnError="true" shortFilenames="true">
<formatter type="html" toFile="c:\foo.html" isReportFilePathAbsolute="true"/>
<fileset dir="C:\data\pmd\pmd\test-data\">
<include name="Foo.java"/>
<pmd rulesetfiles="rulesets/imports.xml" shortFilenames="false" failOnRuleViolation="true">
<formatter type="xml" toFile="foo.html"/>
<fileset dir="C:\j2sdk1.4.1_01\src\java\lang">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>

View File

@ -3,10 +3,11 @@ Added numbering to the HTMLRenderer; thx to Luke Francl for the code.
Fixed bug 672742 - grammar typo was hosing up ASTConstructorDeclaration which was hosing up UseSingletonRule
Fixed bug 674393 - OnlyOneReturn rule no longer counts returns that are inside anonymous inner classes as being inside the containing method. Thx to C. Lamont Gilbert for the bug report.
Fixed bug 674420 - AvoidReassigningParametersRule no longer counts parameter field reassignment as a violation. Thx to C. Lamont Gilbert for the bug report.
Fixed bug 673662 - The Ant task's "failOnError" attribute works again.
Fixed bug 673662 - The Ant task's "failOnError" attribute works again. Changed the semantics of this attribute, though, so it fails the build if errors occurred. A new attribute 'failOnRuleViolation' serves the purpose of stopping the build if rule violations are found.
Fixed bug 676340 - Symbol table now creates new scope level when it encounters a switch statement. See the bug for code details; generally, this bug would have triggered runtime exceptions on certain blocks of code.
Fixed bug in OverrideBothEqualsAndHashcodeRule - it no longer bails out with a NullPtrException on interfaces that declare a method signature "equals(Object)". Thx to Don Leckie for catching that.
Added an optional Ant task formatter attribute 'isReportFilePathAbsolute'. Thx to Andriy Rozeluk for the feedback.
Added an optional Ant task attribute 'failOnRuleViolation'. This stops the build if any rule violations are found.
January 22, 2003 - 1.02:
Added new rules: ImportFromSamePackageRule, SwitchDensityRule, NullAssignmentRule, UnusedModifierRule, ForLoopShouldBeWhileLoopRule

View File

@ -1,4 +1,14 @@
UPDATE THE ANT XDOCS WITH isReportFilePathAbsolute ATTRIBUTE!
>>>>>>>>>>>>> UPDATE THE ANT XDOCS WITH isReportFilePathAbsolute ATTRIBUTE!
>>>>>>>>>>>>> UPDATE THE ANT XDOCS WITH failOnRuleViolation ATTRIBUTE!
Move rulesets/tmp.xml somewhere else

View File

@ -16,9 +16,21 @@ import java.util.TreeSet;
public class Report {
public static class ProcessingError {
private String msg;
private String file;
public ProcessingError(String msg, String file) {
this.msg = msg;
this.file = file;
}
public String getMsg() {return msg;}
public String getFile() {return file;}
}
private Set violations = new TreeSet(new RuleViolation.RuleViolationComparator());
private Set metrics = new HashSet();
private List listeners = new ArrayList();
private List errors = new ArrayList();
public void addListener(ReportListener listener) {
listeners.add(listener);
@ -39,7 +51,11 @@ public class Report {
listener.metricAdded( metric );
}
}
public void addError(ProcessingError error) {
errors.add(error);
}
public boolean hasMetrics() {
return !metrics.isEmpty();
}
@ -56,6 +72,10 @@ public class Report {
return violations.iterator();
}
public Iterator errors() {
return errors.iterator();
}
public int size() {
return violations.size();
}

View File

@ -66,6 +66,7 @@ public class PMDTask extends Task {
private boolean printToConsole;
private String ruleSetFiles;
private boolean failOnError;
private boolean failOnRuleViolation;
/**
* The end of line string for this machine.
@ -76,8 +77,12 @@ public class PMDTask extends Task {
this.shortFilenames = value;
}
public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
public void setFailOnError(boolean fail) {
this.failOnError = fail;
}
public void setFailOnRuleViolation(boolean fail) {
this.failOnRuleViolation = fail;
}
public void setVerbose(boolean verbose) {
@ -125,10 +130,10 @@ public class PMDTask extends Task {
DirectoryScanner ds = fs.getDirectoryScanner(project);
String[] srcFiles = ds.getIncludedFiles();
for (int j=0; j<srcFiles.length; j++) {
File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
printIfVerbose (file.getAbsoluteFile().toString());
ctx.setSourceCodeFilename(shortFilenames ? srcFiles[j] : file.getAbsolutePath());
try {
File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
printIfVerbose (file.getAbsoluteFile().toString());
ctx.setSourceCodeFilename(shortFilenames ? srcFiles[j] : file.getAbsolutePath());
pmd.processFile(new FileInputStream(file), rules, ctx);
} catch (FileNotFoundException fnfe) {
if (failOnError) {
@ -138,6 +143,7 @@ public class PMDTask extends Task {
if (failOnError) {
throw new BuildException(pmde);
}
ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), ctx.getSourceCodeFilename()));
}
}
}
@ -161,8 +167,8 @@ public class PMDTask extends Task {
System.out.println(r.render(report));
}
if (failOnError && !ctx.getReport().isEmpty()) {
throw new BuildException("Stopping build since PMD found problems in the code");
if (failOnRuleViolation && !ctx.getReport().isEmpty()) {
throw new BuildException("Stopping build since PMD found " + ctx.getReport().size() + " rule violations in the code");
}
}

View File

@ -22,6 +22,7 @@ public class XMLRenderer implements Renderer {
String filename = "*start*";
String lineSep = EOL;
// rule violations
for (Iterator i = report.iterator(); i.hasNext();) {
RuleViolation rv = (RuleViolation) i.next();
if (!rv.getFilename().equals(filename)) { // New File
@ -51,6 +52,23 @@ public class XMLRenderer implements Renderer {
if (!filename.equals("*start*")) {
buf.append("</file>");
}
// errors
for (Iterator i = report.errors(); i.hasNext();) {
Report.ProcessingError pe = (Report.ProcessingError)i.next();
buf.append(lineSep);
buf.append("<error ");
buf.append(lineSep);
String attrs = "filename=\"" + pe.getFile() +"\" msg=\"" + pe.getMsg() + "\"";
attrs = replaceString(attrs, '&', "&amp;");
attrs = replaceString(attrs, '<', "&lt;");
attrs = replaceString(attrs, '>', "&gt;");
buf.append(attrs);
buf.append(lineSep);
buf.append("/>");
buf.append(lineSep);
}
buf.append("</pmd>");
return buf.toString();
}