From 1ed5e370a7384a0fb4caa8b6471e18fe96bbde08 Mon Sep 17 00:00:00 2001 From: Brian Remedios Date: Sun, 25 Sep 2011 03:57:33 +0000 Subject: [PATCH] misc cleanup git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7343 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/renderers/SummaryHTMLRenderer.java | 34 +++++++++---------- .../net/sourceforge/pmd/util/Benchmark.java | 11 +++--- .../pmd/util/ClasspathClassLoader.java | 21 ++---------- .../net/sourceforge/pmd/util/StringUtil.java | 21 ++++++++++++ 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/renderers/SummaryHTMLRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/SummaryHTMLRenderer.java index 39574dab58..a2a8fbaa8a 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/SummaryHTMLRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/SummaryHTMLRenderer.java @@ -48,22 +48,22 @@ public class SummaryHTMLRenderer extends AbstractAccumulatingRenderer { * @throws IOException */ public void renderSummary() throws IOException { - StringBuffer buf = new StringBuffer(500); - buf.append("

Summary

"); - buf.append(""); - buf.append(""); - buf.append(""); - writer.write(buf.toString()); - Map summary = report.getSummary(); - for (Map.Entry entry : summary.entrySet()) { - String ruleName = entry.getKey(); - buf.setLength(0); - buf.append(""); - buf.append(""); - buf.append(""); - buf.append(""); - writer.write(buf.toString()); - } - writer.write("
Rule nameNumber of violations
" + ruleName + "" + entry.getValue().intValue() + "
"); + StringBuilder buf = new StringBuilder(500); + buf.append("

Summary

"); + buf.append(""); + buf.append(""); + buf.append(""); + writer.write(buf.toString()); + Map summary = report.getSummary(); + for (Map.Entry entry : summary.entrySet()) { + String ruleName = entry.getKey(); + buf.setLength(0); + buf.append(""); + buf.append(""); + buf.append(""); + buf.append(""); + writer.write(buf.toString()); + } + writer.write("
Rule nameNumber of violations
" + ruleName + "" + entry.getValue().intValue() + "
"); } } diff --git a/pmd/src/net/sourceforge/pmd/util/Benchmark.java b/pmd/src/net/sourceforge/pmd/util/Benchmark.java index 252475a786..8f9a38734c 100644 --- a/pmd/src/net/sourceforge/pmd/util/Benchmark.java +++ b/pmd/src/net/sourceforge/pmd/util/Benchmark.java @@ -147,12 +147,13 @@ public class Benchmark { p.getConfiguration().setDefaultLanguageVersion(languageVersion); RuleContext ctx = new RuleContext(); long start = System.currentTimeMillis(); + Reader reader = null; for (DataSource dataSource: dataSources) { - Reader reader = new InputStreamReader(dataSource.getInputStream()); - ctx.setSourceCodeFilename(dataSource.getNiceFileName(false, null)); - p.processFile(reader, ruleSets, ctx); - reader.close(); - } + reader = new InputStreamReader(dataSource.getInputStream()); + ctx.setSourceCodeFilename(dataSource.getNiceFileName(false, null)); + p.processFile(reader, ruleSets, ctx); + IOUtil.closeQuietly(reader); + } long end = System.currentTimeMillis(); long elapsed = end - start; results.add(new Result(elapsed, rule)); diff --git a/pmd/src/net/sourceforge/pmd/util/ClasspathClassLoader.java b/pmd/src/net/sourceforge/pmd/util/ClasspathClassLoader.java index 6b5f8d9a79..fe48c5bb5d 100644 --- a/pmd/src/net/sourceforge/pmd/util/ClasspathClassLoader.java +++ b/pmd/src/net/sourceforge/pmd/util/ClasspathClassLoader.java @@ -65,15 +65,8 @@ public class ClasspathClassLoader extends URLClassLoader { urls.add(createURLFromPath(line)); } } - in.close(); } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - LOG.log(Level.SEVERE, "IOException while closing InputStream", e); - } - } + IOUtil.closeQuietly(in); } } @@ -87,17 +80,9 @@ public class ClasspathClassLoader extends URLClassLoader { */ @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName()); + StringBuilder sb = new StringBuilder(getClass().getSimpleName()); sb.append("[["); - boolean first = true; - for (URL url : getURLs()) { - if (!first) { - sb.append(':'); - } - first = false; - sb.append(url); - } + StringUtil.asStringOn(sb, getURLs(), ":"); sb.append("] parent: "); sb.append(getParent()); sb.append(']'); diff --git a/pmd/src/net/sourceforge/pmd/util/StringUtil.java b/pmd/src/net/sourceforge/pmd/util/StringUtil.java index 9fbae1760a..9dc98ad1c5 100644 --- a/pmd/src/net/sourceforge/pmd/util/StringUtil.java +++ b/pmd/src/net/sourceforge/pmd/util/StringUtil.java @@ -304,6 +304,27 @@ public final class StringUtil { sb.append(iter.next()); } } + + /** + * Copies the array items onto the string builder each delimited by the separator. + * Does nothing if the array is null or empty. + * + * @param sb StringBuilder + * @param items Object[] + * @param separator String + */ + public static void asStringOn(StringBuilder sb, Object[] items, String separator) { + + if (items == null | items.length == 0) { return; } + + sb.append(items[0]); + + for (int i=1; i