From 5434327a2cbdd535cd28e5abbd70ac89d5f7b5b5 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Sun, 6 Mar 2011 06:50:24 +0300 Subject: [PATCH] Fix violations reported by Sonar Eclipse --- .../checkstyle/CheckstyleConfiguration.java | 16 +++++++----- .../checkstyle/CheckstyleConstants.java | 3 +++ .../checkstyle/CheckstyleExecutor.java | 10 +++----- .../checkstyle/CheckstyleProfileImporter.java | 21 ++++++++-------- .../org/sonar/plugins/pmd/PmdConstants.java | 10 +++++--- .../org/sonar/plugins/pmd/PmdExecutor.java | 25 +++++++++++++++---- .../sonar/plugins/pmd/PmdProfileExporter.java | 15 ++++++----- .../sonar/plugins/pmd/PmdProfileImporter.java | 5 ++-- 8 files changed, 63 insertions(+), 42 deletions(-) diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java index 970d50019f0..57ef5053ddc 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java @@ -19,6 +19,15 @@ */ package org.sonar.plugins.checkstyle; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.Charset; +import java.util.List; +import java.util.Properties; + import com.puppycrawl.tools.checkstyle.ConfigurationLoader; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; import com.puppycrawl.tools.checkstyle.PropertiesExpander; @@ -34,14 +43,9 @@ import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; -import java.io.*; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Properties; - public class CheckstyleConfiguration implements BatchExtension { - private static Logger LOG = LoggerFactory.getLogger(CheckstyleConfiguration.class); + private static final Logger LOG = LoggerFactory.getLogger(CheckstyleConfiguration.class); private CheckstyleProfileExporter confExporter; private RulesProfile profile; diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConstants.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConstants.java index 78ec934681f..e110efcba84 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConstants.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConstants.java @@ -32,4 +32,7 @@ public final class CheckstyleConstants { public static final String FILTERS_DEFAULT_VALUE = ""; public static final String GENERATE_XML_KEY = "sonar.checkstyle.generateXml"; public static final boolean GENERATE_XML_DEFAULT_VALUE = false; + + private CheckstyleConstants() { + } } diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java index 2ec7fcf737d..2b1f165540d 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java @@ -19,6 +19,9 @@ */ package org.sonar.plugins.checkstyle; +import java.io.File; +import java.io.OutputStream; + import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.PackageNamesLoader; import com.puppycrawl.tools.checkstyle.XMLLogger; @@ -31,11 +34,8 @@ import org.sonar.api.batch.ProjectClasspath; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TimeProfiler; -import java.io.File; -import java.io.OutputStream; - public class CheckstyleExecutor implements BatchExtension { - private static Logger LOG = LoggerFactory.getLogger(CheckstyleExecutor.class); + private static final Logger LOG = LoggerFactory.getLogger(CheckstyleExecutor.class); private CheckstyleConfiguration configuration; private ClassLoader projectClassloader; @@ -47,7 +47,6 @@ public class CheckstyleExecutor implements BatchExtension { this.projectClassloader = classpath.getClassloader(); } - CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, ClassLoader projectClassloader) { this.configuration = configuration; this.listener = listener; @@ -96,5 +95,4 @@ public class CheckstyleExecutor implements BatchExtension { } } - } \ No newline at end of file diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java index 2ea3f1b6232..02e251b4dc4 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java @@ -19,6 +19,12 @@ */ package org.sonar.plugins.checkstyle; +import java.io.Reader; +import java.util.Map; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; + import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.codehaus.stax2.XMLInputFactory2; @@ -34,12 +40,6 @@ import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleQuery; import org.sonar.api.utils.ValidationMessages; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; - -import java.io.Reader; -import java.util.Map; - public class CheckstyleProfileImporter extends ProfileImporter { private static final String CHECKER_MODULE = "Checker"; @@ -84,11 +84,11 @@ public class CheckstyleProfileImporter extends ProfileImporter { xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); - SMInputFactory inputFactory = new SMInputFactory(xmlFactory); - return inputFactory; + return new SMInputFactory(xmlFactory); } - private void processModule(RulesProfile profile, String path, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { + private void processModule(RulesProfile profile, String path, SMInputCursor moduleCursor, ValidationMessages messages) + throws XMLStreamException { String moduleName = moduleCursor.getAttrValue("name"); if (isFilter(moduleName)) { messages.addWarningText("Checkstyle filters are not imported: " + moduleName); @@ -109,7 +109,8 @@ public class CheckstyleProfileImporter extends ProfileImporter { StringUtils.equals(configKey, "SuppressWithNearbyCommentFilter"); } - private void processRule(RulesProfile profile, String path, String moduleName, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { + private void processRule(RulesProfile profile, String path, String moduleName, SMInputCursor moduleCursor, ValidationMessages messages) + throws XMLStreamException { Map properties = processProps(moduleCursor); Rule rule; diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java index cf6c72c3f35..33249919d4c 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java @@ -22,12 +22,14 @@ package org.sonar.plugins.pmd; import org.sonar.api.CoreProperties; public final class PmdConstants { - public static final String REPOSITORY_KEY = CoreProperties.PMD_PLUGIN; public static final String REPOSITORY_NAME = "PMD"; public static final String PLUGIN_NAME = "PMD"; public static final String PLUGIN_KEY = CoreProperties.PMD_PLUGIN; - public static final String XPATH_CLASS ="net.sourceforge.pmd.rules.XPathRule"; - public static final String XPATH_EXPRESSION_PARAM ="xpath"; - public static final String XPATH_MESSAGE_PARAM ="message"; + public static final String XPATH_CLASS = "net.sourceforge.pmd.rules.XPathRule"; + public static final String XPATH_EXPRESSION_PARAM = "xpath"; + public static final String XPATH_MESSAGE_PARAM = "message"; + + private PmdConstants() { + } } diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java index 64b0005b9cc..1103d9fccb4 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java @@ -19,7 +19,25 @@ */ package org.sonar.plugins.pmd; -import net.sourceforge.pmd.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.List; + +import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.PMDException; +import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.RuleSetFactory; +import net.sourceforge.pmd.RuleSets; +import net.sourceforge.pmd.SourceType; import net.sourceforge.pmd.renderers.Renderer; import net.sourceforge.pmd.renderers.XMLRenderer; import org.apache.commons.io.IOUtils; @@ -32,12 +50,9 @@ import org.sonar.api.resources.Project; import org.sonar.api.utils.TimeProfiler; import org.sonar.java.api.JavaUtils; -import java.io.*; -import java.util.List; - public class PmdExecutor implements BatchExtension { - private static Logger LOG = LoggerFactory.getLogger(PmdExecutor.class); + private static final Logger LOG = LoggerFactory.getLogger(PmdExecutor.class); private PmdConfiguration configuration; private Project project; diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java index a7024f01eb8..febf761b6a9 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java @@ -19,12 +19,17 @@ */ package org.sonar.plugins.pmd; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + import org.jdom.CDATA; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; -import org.sonar.api.CoreProperties; import org.sonar.api.profiles.ProfileExporter; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; @@ -35,12 +40,6 @@ import org.sonar.plugins.pmd.xml.PmdProperty; import org.sonar.plugins.pmd.xml.PmdRule; import org.sonar.plugins.pmd.xml.PmdRuleset; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.List; - public class PmdProfileExporter extends ProfileExporter { public PmdProfileExporter() { @@ -63,7 +62,7 @@ public class PmdProfileExporter extends ProfileExporter { protected PmdRuleset createPmdRuleset(List activeRules, String profileName) { PmdRuleset ruleset = new PmdRuleset(profileName); for (ActiveRule activeRule : activeRules) { - if (activeRule.getRule().getPluginName().equals(CoreProperties.PMD_PLUGIN)) { + if (activeRule.getRule().getRepositoryKey().equals(PmdConstants.REPOSITORY_KEY)) { String configKey = activeRule.getRule().getConfigKey(); PmdRule rule = new PmdRule(configKey, PmdLevelUtils.toLevel(activeRule.getSeverity())); if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) { diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java index 1c87b66ac6e..d2daf47e24f 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java @@ -43,7 +43,7 @@ import org.sonar.plugins.pmd.xml.PmdRuleset; public class PmdProfileImporter extends ProfileImporter { private final RuleFinder ruleFinder; - private static Logger LOG = LoggerFactory.getLogger(PmdProfileImporter.class); + private static final Logger LOG = LoggerFactory.getLogger(PmdProfileImporter.class); public PmdProfileImporter(RuleFinder ruleFinder) { super(PmdConstants.REPOSITORY_KEY, PmdConstants.PLUGIN_NAME); @@ -54,8 +54,7 @@ public class PmdProfileImporter extends ProfileImporter { @Override public RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages) { PmdRuleset pmdRuleset = parsePmdRuleset(pmdConfigurationFile, messages); - RulesProfile profile = createRuleProfile(pmdRuleset, messages); - return profile; + return createRuleProfile(pmdRuleset, messages); } protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { -- 2.39.5