From ec8c9e1e5be13b1e63bdbfb424462771a03770a2 Mon Sep 17 00:00:00 2001 From: Brian Remedios Date: Tue, 20 Sep 2011 15:07:07 +0000 Subject: [PATCH] Modernizing: StringBuffer -> StringBuilder switchover git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7295 51baf565-9d33-0410-a72c-fc3788e3496d --- .../sourceforge/pmd/util/StringUtilTest.java | 4 +- .../net/sourceforge/pmd/ant/Formatter.java | 2 +- .../pmd/renderers/CSVRenderer.java | 72 +++---- .../pmd/renderers/EmacsRenderer.java | 22 +- .../pmd/renderers/TextRenderer.java | 64 +++--- .../pmd/renderers/VBHTMLRenderer.java | 173 ++++++++------- .../pmd/renderers/XMLRenderer.java | 204 +++++++++--------- .../net/sourceforge/pmd/util/StringUtil.java | 6 +- 8 files changed, 272 insertions(+), 275 deletions(-) diff --git a/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java b/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java index 928dfa9556..9899e3fd39 100644 --- a/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/util/StringUtilTest.java @@ -43,7 +43,7 @@ public class StringUtilTest { */ @Test public void testUTF8NotSupported() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); String test = "é"; StringUtil.appendXmlEscaped(sb, test, false); assertEquals("é", sb.toString()); @@ -51,7 +51,7 @@ public class StringUtilTest { @Test public void testUTF8Supported() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); String test = "é"; StringUtil.appendXmlEscaped(sb, test, true); assertEquals("é", sb.toString()); diff --git a/pmd/src/net/sourceforge/pmd/ant/Formatter.java b/pmd/src/net/sourceforge/pmd/ant/Formatter.java index c5d529ce6c..2ec22cd865 100644 --- a/pmd/src/net/sourceforge/pmd/ant/Formatter.java +++ b/pmd/src/net/sourceforge/pmd/ant/Formatter.java @@ -101,7 +101,7 @@ public class Formatter { } private static String unknownRendererMessage(String userSpecifiedType) { - StringBuffer sb = new StringBuffer(100); + StringBuilder sb = new StringBuilder(100); sb.append("Formatter type must be one of: '"); String[] typeCodes = validRendererCodes(); sb.append(typeCodes[0]); diff --git a/pmd/src/net/sourceforge/pmd/renderers/CSVRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/CSVRenderer.java index e33d2083c2..eee68f3ac7 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/CSVRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/CSVRenderer.java @@ -22,7 +22,7 @@ public class CSVRenderer extends AbstractIncrementingRenderer { private int violationCount = 1; public CSVRenderer(Properties properties) { - super(NAME, "Comma-separated values tabular format.", properties); + super(NAME, "Comma-separated values tabular format.", properties); } /** @@ -30,17 +30,17 @@ public class CSVRenderer extends AbstractIncrementingRenderer { */ @Override public void start() throws IOException { - StringBuffer buf = new StringBuffer(300); - quoteAndCommify(buf, "Problem"); - quoteAndCommify(buf, "Package"); - quoteAndCommify(buf, "File"); - quoteAndCommify(buf, "Priority"); - quoteAndCommify(buf, "Line"); - quoteAndCommify(buf, "Description"); - quoteAndCommify(buf, "Rule set"); - quote(buf, "Rule"); - buf.append(PMD.EOL); - getWriter().write(buf.toString()); + StringBuilder buf = new StringBuilder(300); + quoteAndCommify(buf, "Problem"); + quoteAndCommify(buf, "Package"); + quoteAndCommify(buf, "File"); + quoteAndCommify(buf, "Priority"); + quoteAndCommify(buf, "Line"); + quoteAndCommify(buf, "Description"); + quoteAndCommify(buf, "Rule set"); + quote(buf, "Rule"); + buf.append(PMD.EOL); + getWriter().write(buf.toString()); } public String defaultFileExtension() { return "csv"; } @@ -50,33 +50,33 @@ public class CSVRenderer extends AbstractIncrementingRenderer { */ @Override public void renderFileViolations(Iterator violations) throws IOException { - StringBuffer buf = new StringBuffer(300); - Writer writer = getWriter(); - - RuleViolation rv; - while (violations.hasNext()) { - buf.setLength(0); - rv = violations.next(); - quoteAndCommify(buf, Integer.toString(violationCount)); - quoteAndCommify(buf, rv.getPackageName()); - quoteAndCommify(buf, rv.getFilename()); - quoteAndCommify(buf, Integer.toString(rv.getRule().getPriority().getPriority())); - quoteAndCommify(buf, Integer.toString(rv.getBeginLine())); - quoteAndCommify(buf, StringUtil.replaceString(rv.getDescription(), '\"', "'")); - quoteAndCommify(buf, rv.getRule().getRuleSetName()); - quote(buf, rv.getRule().getName()); - buf.append(PMD.EOL); - writer.write(buf.toString()); - violationCount++; - } + StringBuilder buf = new StringBuilder(300); + Writer writer = getWriter(); + + RuleViolation rv; + while (violations.hasNext()) { + buf.setLength(0); + rv = violations.next(); + quoteAndCommify(buf, Integer.toString(violationCount)); + quoteAndCommify(buf, rv.getPackageName()); + quoteAndCommify(buf, rv.getFilename()); + quoteAndCommify(buf, Integer.toString(rv.getRule().getPriority().getPriority())); + quoteAndCommify(buf, Integer.toString(rv.getBeginLine())); + quoteAndCommify(buf, StringUtil.replaceString(rv.getDescription(), '\"', "'")); + quoteAndCommify(buf, rv.getRule().getRuleSetName()); + quote(buf, rv.getRule().getName()); + buf.append(PMD.EOL); + writer.write(buf.toString()); + violationCount++; + } } - private void quote(StringBuffer buffer, String s) { - buffer.append('"').append(s).append('"'); + private void quote(StringBuilder buffer, String s) { + buffer.append('"').append(s).append('"'); } - private void quoteAndCommify(StringBuffer buffer, String s) { - quote(buffer, s); - buffer.append(','); + private void quoteAndCommify(StringBuilder buffer, String s) { + quote(buffer, s); + buffer.append(','); } } diff --git a/pmd/src/net/sourceforge/pmd/renderers/EmacsRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/EmacsRenderer.java index 6995b1c066..fdf55776eb 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/EmacsRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/EmacsRenderer.java @@ -20,7 +20,7 @@ public class EmacsRenderer extends AbstractIncrementingRenderer { protected static final String EOL = System.getProperty("line.separator", "\n"); public EmacsRenderer(Properties properties) { - super(NAME, "GNU Emacs integration.", properties); + super(NAME, "GNU Emacs integration.", properties); } public String defaultFileExtension() { return "emacs"; } @@ -30,15 +30,15 @@ public class EmacsRenderer extends AbstractIncrementingRenderer { */ @Override public void renderFileViolations(Iterator violations) throws IOException { - Writer writer = getWriter(); - StringBuffer buf = new StringBuffer(); - while (violations.hasNext()) { - RuleViolation rv = violations.next(); - buf.setLength(0); - buf.append(rv.getFilename()); - buf.append(':').append(Integer.toString(rv.getBeginLine())); - buf.append(": ").append(rv.getDescription()).append(EOL); - writer.write(buf.toString()); - } + Writer writer = getWriter(); + StringBuilder buf = new StringBuilder(); + while (violations.hasNext()) { + RuleViolation rv = violations.next(); + buf.setLength(0); + buf.append(rv.getFilename()); + buf.append(':').append(Integer.toString(rv.getBeginLine())); + buf.append(": ").append(rv.getDescription()).append(EOL); + writer.write(buf.toString()); + } } } diff --git a/pmd/src/net/sourceforge/pmd/renderers/TextRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/TextRenderer.java index 62504d6837..73eabca8ca 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/TextRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/TextRenderer.java @@ -20,7 +20,7 @@ public class TextRenderer extends AbstractIncrementingRenderer { public static final String NAME = "text"; public TextRenderer(Properties properties) { - super(NAME, "Text format.", properties); + super(NAME, "Text format.", properties); } @@ -38,17 +38,17 @@ public class TextRenderer extends AbstractIncrementingRenderer { */ @Override public void renderFileViolations(Iterator violations) throws IOException { - Writer writer = getWriter(); - StringBuffer buf = new StringBuffer(); - - while (violations.hasNext()) { - buf.setLength(0); - RuleViolation rv = violations.next(); - buf.append(rv.getFilename()); - buf.append(':').append(Integer.toString(rv.getBeginLine())); - buf.append('\t').append(rv.getDescription()).append(PMD.EOL); - writer.write(buf.toString()); - } + Writer writer = getWriter(); + StringBuilder buf = new StringBuilder(); + + while (violations.hasNext()) { + buf.setLength(0); + RuleViolation rv = violations.next(); + buf.append(rv.getFilename()); + buf.append(':').append(Integer.toString(rv.getBeginLine())); + buf.append('\t').append(rv.getDescription()).append(PMD.EOL); + writer.write(buf.toString()); + } } /** @@ -56,26 +56,26 @@ public class TextRenderer extends AbstractIncrementingRenderer { */ @Override public void end() throws IOException { - Writer writer = getWriter(); - StringBuffer buf = new StringBuffer(500); - if (!errors.isEmpty()) { - - for (Report.ProcessingError error : errors) { - buf.setLength(0); - buf.append(error.getFile()); - buf.append("\t-\t").append(error.getMsg()).append(PMD.EOL); - writer.write(buf.toString()); - } - } - - for (Report.SuppressedViolation excluded : suppressed) { - buf.setLength(0); - buf.append(excluded.getRuleViolation().getRule().getName()); - buf.append(" rule violation suppressed by "); - buf.append(excluded.suppressedByNOPMD() ? "//NOPMD" : "Annotation"); - buf.append(" in ").append(excluded.getRuleViolation().getFilename()).append(PMD.EOL); - writer.write(buf.toString()); - } + Writer writer = getWriter(); + StringBuilder buf = new StringBuilder(500); + if (!errors.isEmpty()) { + + for (Report.ProcessingError error : errors) { + buf.setLength(0); + buf.append(error.getFile()); + buf.append("\t-\t").append(error.getMsg()).append(PMD.EOL); + writer.write(buf.toString()); + } + } + + for (Report.SuppressedViolation excluded : suppressed) { + buf.setLength(0); + buf.append(excluded.getRuleViolation().getRule().getName()); + buf.append(" rule violation suppressed by "); + buf.append(excluded.suppressedByNOPMD() ? "//NOPMD" : "Annotation"); + buf.append(" in ").append(excluded.getRuleViolation().getFilename()).append(PMD.EOL); + writer.write(buf.toString()); + } } } diff --git a/pmd/src/net/sourceforge/pmd/renderers/VBHTMLRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/VBHTMLRenderer.java index 252c6d7de0..cbf5179d35 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/VBHTMLRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/VBHTMLRenderer.java @@ -21,7 +21,7 @@ public class VBHTMLRenderer extends AbstractIncrementingRenderer { public static final String NAME = "vbhtml"; public VBHTMLRenderer(Properties properties) { - super(NAME, "Vladimir Bossicard HTML format.", properties); + super(NAME, "Vladimir Bossicard HTML format.", properties); } public String defaultFileExtension() { return "vb.html"; } @@ -31,7 +31,7 @@ public class VBHTMLRenderer extends AbstractIncrementingRenderer { */ @Override public void start() throws IOException { - getWriter().write(header()); + getWriter().write(header()); } /** @@ -39,48 +39,48 @@ public class VBHTMLRenderer extends AbstractIncrementingRenderer { */ @Override public void renderFileViolations(Iterator violations) throws IOException { - if (!violations.hasNext()) { - return; - } - - Writer writer = getWriter(); - StringBuffer sb = new StringBuffer(500); - String filename = null; - String lineSep = PMD.EOL; - - boolean colorize = false; - while (violations.hasNext()) { - sb.setLength(0); - RuleViolation rv = violations.next(); - if (!rv.getFilename().equals(filename)) { // New File - if (filename != null) { - sb.append("
"); - colorize = false; + if (!violations.hasNext()) { + return; + } + + Writer writer = getWriter(); + StringBuilder sb = new StringBuilder(500); + String filename = null; + String lineSep = PMD.EOL; + + boolean colorize = false; + while (violations.hasNext()) { + sb.setLength(0); + RuleViolation rv = violations.next(); + if (!rv.getFilename().equals(filename)) { // New File + if (filename != null) { + sb.append("
"); + colorize = false; + } + filename = rv.getFilename(); + sb.append(""); + sb.append(""); + sb.append(lineSep); + } + + if (colorize) { + sb.append(""); + } else { + sb.append(""); + } + + colorize = !colorize; + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(lineSep); + writer.write(sb.toString()); + } + if (filename != null) { + writer.write("
 ").append(filename).append( + "
" + rv.getBeginLine() + + "   " + rv.getDescription() + "
"); } - filename = rv.getFilename(); - sb.append(""); - sb.append(""); - sb.append(lineSep); - } - - if (colorize) { - sb.append(""); - } else { - sb.append(""); - } - - colorize = !colorize; - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(lineSep); - writer.write(sb.toString()); - } - if (filename != null) { - writer.write("
 ").append(filename).append( - "
" + rv.getBeginLine() - + "   " + rv.getDescription() + "
"); - } } /** @@ -88,58 +88,55 @@ public class VBHTMLRenderer extends AbstractIncrementingRenderer { */ @Override public void end() throws IOException { - Writer writer = getWriter(); - StringBuffer sb = new StringBuffer(); - - writer.write("
"); - - // output the problems - if (!errors.isEmpty()) { - sb.setLength(0); - sb.append(""); - sb.append(""); - boolean colorize = false; - for (Report.ProcessingError error : errors) { - if (colorize) { - sb.append(""); - } else { - sb.append(""); + Writer writer = getWriter(); + StringBuilder sb = new StringBuilder(); + + writer.write("
"); + + // output the problems + if (!errors.isEmpty()) { + sb.setLength(0); + sb.append("
 Problems found
"); + sb.append(""); + boolean colorize = false; + for (Report.ProcessingError error : errors) { + if (colorize) { + sb.append(""); + } else { + sb.append(""); + } + colorize = !colorize; + sb.append(""); + } + sb.append("
 Problems found
").append(error).append("\"
"); + writer.write(sb.toString()); } - colorize = !colorize; - sb.append("").append(error).append("\""); - } - sb.append(""); - writer.write(sb.toString()); - } - - writer.write(footer()); + + writer.write(footer()); } private String header() { - StringBuffer sb = new StringBuffer(600); - sb.append("PMD"); - sb.append(""); - sb.append("
"); - return sb.toString(); + StringBuilder sb = new StringBuilder(600); + sb.append("PMD"); + sb.append(""); + sb.append("
"); + return sb.toString(); } private String footer() { - return "
" + PMD.EOL; + return "
" + PMD.EOL; } } \ No newline at end of file diff --git a/pmd/src/net/sourceforge/pmd/renderers/XMLRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/XMLRenderer.java index f9a84b7a9c..6fd3c7eb16 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/XMLRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/XMLRenderer.java @@ -28,12 +28,12 @@ public class XMLRenderer extends AbstractIncrementingRenderer { protected String encoding = "UTF-8"; public XMLRenderer(Properties properties) { - super(NAME, "XML format.", properties); - defineProperty(ENCODING, "XML encoding format, defaults to UTF-8."); - - if (properties.containsKey(ENCODING)) { - this.encoding = properties.getProperty(ENCODING); - } + super(NAME, "XML format.", properties); + defineProperty(ENCODING, "XML encoding format, defaults to UTF-8."); + + if (properties.containsKey(ENCODING)) { + encoding = properties.getProperty(ENCODING); + } } public String defaultFileExtension() { return "xml"; } @@ -43,15 +43,15 @@ public class XMLRenderer extends AbstractIncrementingRenderer { */ @Override public void start() throws IOException { - Writer writer = getWriter(); - StringBuffer buf = new StringBuffer(500); - buf.append("").append(PMD.EOL); - createVersionAttr(buf); - createTimestampAttr(buf); - // FIXME: elapsed time not available until the end of the processing - //buf.append(createTimeElapsedAttr(report)); - buf.append('>').append(PMD.EOL); - writer.write(buf.toString()); + Writer writer = getWriter(); + StringBuilder buf = new StringBuilder(500); + buf.append("").append(PMD.EOL); + createVersionAttr(buf); + createTimestampAttr(buf); + // FIXME: elapsed time not available until the end of the processing + //buf.append(createTimeElapsedAttr(report)); + buf.append('>').append(PMD.EOL); + writer.write(buf.toString()); } /** @@ -59,52 +59,52 @@ public class XMLRenderer extends AbstractIncrementingRenderer { */ @Override public void renderFileViolations(Iterator violations) throws IOException { - Writer writer = getWriter(); - StringBuffer buf = new StringBuffer(500); - String filename = null; - - // rule violations - while (violations.hasNext()) { - buf.setLength(0); - RuleViolation rv = violations.next(); - if (!rv.getFilename().equals(filename)) { // New File - if (filename != null) {// Not first file ? - buf.append("").append(PMD.EOL); + Writer writer = getWriter(); + StringBuilder buf = new StringBuilder(500); + String filename = null; + + // rule violations + while (violations.hasNext()) { + buf.setLength(0); + RuleViolation rv = violations.next(); + if (!rv.getFilename().equals(filename)) { // New File + if (filename != null) {// Not first file ? + buf.append("").append(PMD.EOL); + } + filename = rv.getFilename(); + buf.append("").append(PMD.EOL); + } + + buf.append("").append(PMD.EOL); + StringUtil.appendXmlEscaped(buf, rv.getDescription()); + + buf.append(PMD.EOL); + buf.append(""); + buf.append(PMD.EOL); + writer.write(buf.toString()); + } + if (filename != null) { // Not first file ? + writer.write(""); + writer.write(PMD.EOL); } - filename = rv.getFilename(); - buf.append("").append(PMD.EOL); - } - - buf.append("").append(PMD.EOL); - StringUtil.appendXmlEscaped(buf, rv.getDescription()); - - buf.append(PMD.EOL); - buf.append(""); - buf.append(PMD.EOL); - writer.write(buf.toString()); - } - if (filename != null) { // Not first file ? - writer.write(""); - writer.write(PMD.EOL); - } } /** @@ -112,54 +112,54 @@ public class XMLRenderer extends AbstractIncrementingRenderer { */ @Override public void end() throws IOException { - Writer writer = getWriter(); - StringBuffer buf = new StringBuffer(500); - // errors - for (Report.ProcessingError pe : errors) { - buf.setLength(0); - buf.append("").append(PMD.EOL); - writer.write(buf.toString()); - } - - // suppressed violations - if (showSuppressedViolations) { - for (Report.SuppressedViolation s : suppressed) { - buf.setLength(0); - buf.append("").append(PMD.EOL); - writer.write(buf.toString()); - } - } - - writer.write("" + PMD.EOL); + Writer writer = getWriter(); + StringBuilder buf = new StringBuilder(500); + // errors + for (Report.ProcessingError pe : errors) { + buf.setLength(0); + buf.append("").append(PMD.EOL); + writer.write(buf.toString()); + } + + // suppressed violations + if (showSuppressedViolations) { + for (Report.SuppressedViolation s : suppressed) { + buf.setLength(0); + buf.append("").append(PMD.EOL); + writer.write(buf.toString()); + } + } + + writer.write("" + PMD.EOL); } - private void maybeAdd(String attr, String value, StringBuffer buf) { - if (value != null && value.length() > 0) { - buf.append(' ').append(attr).append("=\""); - StringUtil.appendXmlEscaped(buf, value); - buf.append('"'); - } + private void maybeAdd(String attr, String value, StringBuilder buf) { + if (value != null && value.length() > 0) { + buf.append(' ').append(attr).append("=\""); + StringUtil.appendXmlEscaped(buf, value); + buf.append('"'); + } } - private void createVersionAttr(StringBuffer buffer) { - buffer.append("