From 79890d4312a6797a96624498545e02c1c35f98c1 Mon Sep 17 00:00:00 2001 From: Felix Schumacher Date: Wed, 10 Oct 2018 21:43:01 +0200 Subject: [PATCH] Use simpler code for "static" String concatenation Java is smart enough to compile simple string concatenation to the same code as `new StringBuilder("something").append("other thing").toString()`. The latter is really hard to read, so use the simpler one. While at it, use string formats for the logger (as it is some sort of string concatenation, too). And since we use the string formatter, we don't have to guard the log level anymore for the simple case. --- .../org/apache/jmeter/assertions/XPathAssertion.java | 7 +++---- .../org/apache/jmeter/extractor/RegexExtractor.java | 4 ++-- .../org/apache/jmeter/extractor/XPath2Extractor.java | 4 ++-- .../org/apache/jmeter/extractor/XPathExtractor.java | 6 +++--- .../org/apache/jmeter/functions/XPathFileContainer.java | 8 +++----- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/components/org/apache/jmeter/assertions/XPathAssertion.java b/src/components/org/apache/jmeter/assertions/XPathAssertion.java index a0c548fcf95..ddb71ee9cfc 100644 --- a/src/components/org/apache/jmeter/assertions/XPathAssertion.java +++ b/src/components/org/apache/jmeter/assertions/XPathAssertion.java @@ -104,18 +104,17 @@ public AssertionResult getResult(SampleResult response) { } catch (SAXException e) { log.debug("Caught sax exception.", e); result.setError(true); - result.setFailureMessage(new StringBuilder("SAXException: ").append(e.getMessage()).toString()); + result.setFailureMessage("SAXException: " + e.getMessage()); return result; } catch (IOException e) { log.warn("Cannot parse result content.", e); result.setError(true); - result.setFailureMessage(new StringBuilder("IOException: ").append(e.getMessage()).toString()); + result.setFailureMessage("IOException: " + e.getMessage()); return result; } catch (ParserConfigurationException e) { log.warn("Cannot parse result content.", e); result.setError(true); - result.setFailureMessage(new StringBuilder("ParserConfigurationException: ").append(e.getMessage()) - .toString()); + result.setFailureMessage("ParserConfigurationException: " + e.getMessage()); return result; } catch (TidyException e) { result.setError(true); diff --git a/src/components/org/apache/jmeter/extractor/RegexExtractor.java b/src/components/org/apache/jmeter/extractor/RegexExtractor.java index 523e772d567..19a3037e99a 100644 --- a/src/components/org/apache/jmeter/extractor/RegexExtractor.java +++ b/src/components/org/apache/jmeter/extractor/RegexExtractor.java @@ -153,7 +153,7 @@ public void process() { for (int i = 1; i <= matchCount; i++) { match = getCorrectMatch(matches, i); if (match != null) { - final String refName_n = new StringBuilder(refName).append(UNDERSCORE).append(i).toString(); + final String refName_n = refName + UNDERSCORE + i; vars.put(refName_n, generateResult(match)); saveGroups(vars, refName_n, match); } @@ -161,7 +161,7 @@ public void process() { } // Remove any left-over variables for (int i = matchCount + 1; i <= prevCount; i++) { - final String refName_n = new StringBuilder(refName).append(UNDERSCORE).append(i).toString(); + final String refName_n = refName + UNDERSCORE + i; vars.remove(refName_n); removeGroups(vars, refName_n); } diff --git a/src/components/org/apache/jmeter/extractor/XPath2Extractor.java b/src/components/org/apache/jmeter/extractor/XPath2Extractor.java index 5d908e4d9c2..f52604f96ee 100644 --- a/src/components/org/apache/jmeter/extractor/XPath2Extractor.java +++ b/src/components/org/apache/jmeter/extractor/XPath2Extractor.java @@ -75,11 +75,11 @@ public class XPath2Extractor extends AbstractScopedTestElement implements //- JMX file attributes private String concat(String s1,String s2){ - return new StringBuilder(s1).append("_").append(s2).toString(); // $NON-NLS-1$ + return s1 + "_" + s2; // $NON-NLS-1$ } private String concat(String s1, int i){ - return new StringBuilder(s1).append("_").append(i).toString(); // $NON-NLS-1$ + return s1 + "_" + i; // $NON-NLS-1$ } diff --git a/src/components/org/apache/jmeter/extractor/XPathExtractor.java b/src/components/org/apache/jmeter/extractor/XPathExtractor.java index eff4fa088d3..579e607c97f 100644 --- a/src/components/org/apache/jmeter/extractor/XPathExtractor.java +++ b/src/components/org/apache/jmeter/extractor/XPathExtractor.java @@ -91,11 +91,11 @@ public class XPathExtractor extends AbstractScopedTestElement implements private String concat(String s1,String s2){ - return new StringBuilder(s1).append("_").append(s2).toString(); // $NON-NLS-1$ + return s1 + "_" + s2; // $NON-NLS-1$ } private String concat(String s1, int i){ - return new StringBuilder(s1).append("_").append(i).toString(); // $NON-NLS-1$ + return s1 + "_" + i; // $NON-NLS-1$ } /** @@ -169,7 +169,7 @@ public void process() { log.error("IOException on ({})", getXPathQuery(), e); AssertionResult ass = new AssertionResult(getName()); ass.setError(true); - ass.setFailureMessage(new StringBuilder("IOException: ").append(e.getLocalizedMessage()).toString()); + ass.setFailureMessage("IOException: " + e.getLocalizedMessage()); previousResult.addAssertionResult(ass); previousResult.setSuccessful(false); } catch (ParserConfigurationException e) {// Should not happen diff --git a/src/functions/org/apache/jmeter/functions/XPathFileContainer.java b/src/functions/org/apache/jmeter/functions/XPathFileContainer.java index 89f069b9aa0..8be69e5d91a 100644 --- a/src/functions/org/apache/jmeter/functions/XPathFileContainer.java +++ b/src/functions/org/apache/jmeter/functions/XPathFileContainer.java @@ -56,9 +56,7 @@ int getNextRow(){// give access to Test code public XPathFileContainer(String file, String xpath) throws FileNotFoundException, IOException, ParserConfigurationException, SAXException, TransformerException { - if(log.isDebugEnabled()) { - log.debug("XPath(" + file + ") xpath " + xpath); - } + log.debug("XPath({}) xpath {}", file, xpath); fileName = file; nextRow = 0; nodeList=load(xpath); @@ -72,7 +70,7 @@ private NodeList load(String xpath) throws IOException, FileNotFoundException, P DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); nl = XPathUtil.selectNodeList(builder.parse(bis), xpath); if(log.isDebugEnabled()) { - log.debug("found " + nl.getLength()); + log.debug("found {}", nl.getLength()); } } catch (TransformerException | SAXException | ParserConfigurationException | IOException e) { @@ -100,7 +98,7 @@ public int nextRow() { { nextRow = 0; } - log.debug(new StringBuilder("Row: ").append(row).toString()); + log.debug("Row: {}", row); return row; }