From d20b6a3c19ea1f7c7432544b68134c38dabde2e9 Mon Sep 17 00:00:00 2001 From: Roman Ivanov Date: Tue, 7 Nov 2017 03:43:09 -0800 Subject: [PATCH] Issue #130: fix ReturnCount checkstyle violations --- .../plugins/checkstyle/CheckstyleAuditListener.java | 6 ++++-- .../plugins/checkstyle/CheckstyleConfiguration.java | 5 +++-- .../plugins/checkstyle/CheckstyleProfileImporter.java | 6 ++++-- .../sonar/plugins/checkstyle/internal/CheckUtil.java | 10 ++++++---- .../org/sonar/plugins/checkstyle/internal/XmlUtil.java | 5 +++-- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java index b51b3448..b06b1732 100644 --- a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java +++ b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java @@ -127,14 +127,16 @@ static String getRuleKey(AuditEvent event) { @VisibleForTesting static String getMessage(AuditEvent event) { + String result; try { - return event.getMessage(); + result = event.getMessage(); } catch (Exception ex) { LOG.warn("AuditEvent is created incorrectly. Exception happen during getMessage()", ex); - return null; + result = null; } + return result; } @VisibleForTesting diff --git a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java index c66fcdfa..1b4be49a 100644 --- a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java +++ b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java @@ -94,10 +94,11 @@ public List getSourceFiles() { } public File getTargetXmlReport() { + File result = null; if (conf.getBoolean(PROPERTY_GENERATE_XML)) { - return new File(fileSystem.workDir(), "checkstyle-result.xml"); + result = new File(fileSystem.workDir(), "checkstyle-result.xml"); } - return null; + return result; } public Configuration getCheckstyleConfiguration() throws CheckstyleException { diff --git a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java index 9ddeeafe..49bd018a 100644 --- a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java +++ b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java @@ -154,12 +154,14 @@ static boolean isIgnored(String configKey) { @VisibleForTesting static boolean isFilter(String configKey) { + boolean result = false; for (String filter : FILTERS) { if (StringUtils.equals(configKey, filter)) { - return true; + result = true; + break; } } - return false; + return result; } private void processRule(RulesProfile profile, String path, String moduleName, diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java index ed09c0cc..6fd26691 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/CheckUtil.java @@ -201,16 +201,18 @@ else if (module == RegexpSinglelineCheck.class */ public static String getCheckMessage(Class module, String messageKey, Object... arguments) { + String result; final Properties pr = new Properties(); try { pr.load(module.getResourceAsStream("messages.properties")); + final MessageFormat formatter = + new MessageFormat(pr.getProperty(messageKey), Locale.ENGLISH); + result = formatter.format(arguments); } catch (IOException ex) { - return null; + result = null; } - final MessageFormat formatter = - new MessageFormat(pr.getProperty(messageKey), Locale.ENGLISH); - return formatter.format(arguments); + return result; } public static String getTokenText(int[] tokens, int... subtractions) { diff --git a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/XmlUtil.java b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/XmlUtil.java index c2a8a97f..19d93d9d 100644 --- a/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/XmlUtil.java +++ b/checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/internal/XmlUtil.java @@ -41,6 +41,7 @@ private XmlUtil() { public static Document getRawXml(String fileName, String code, String unserializedSource) throws ParserConfigurationException { + Document result = null; try { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); @@ -48,14 +49,14 @@ public static Document getRawXml(String fileName, String code, String unserializ final DocumentBuilder builder = factory.newDocumentBuilder(); - return builder.parse(new InputSource(new StringReader(code))); + result = builder.parse(new InputSource(new StringReader(code))); } catch (IOException | SAXException ex) { Assert.fail(fileName + " has invalid xml (" + ex.getMessage() + "): " + unserializedSource); } - return null; + return result; } public static Set getChildrenElements(Node node) {