Skip to content

Commit

Permalink
Issue #502: update to LTS 9.9; cleanup deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlba91 committed Mar 1, 2024
1 parent 8462c9e commit a73c88b
Show file tree
Hide file tree
Showing 37 changed files with 124 additions and 1,109 deletions.
21 changes: 11 additions & 10 deletions .github/workflows/sonar-checkstyle-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ jobs:
- name: install
run: "./.ci/ci.sh install"

integration-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: run integration tests
run: "./.ci/ci.sh integration-tests"
# depreacted for now since SQ removed the application ZIP files needed for the integration tests
# integration-tests:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Set up JDK 11
# uses: actions/setup-java@v1
# with:
# java-version: 11
# - name: run integration tests
# run: "./.ci/ci.sh integration-tests"

nondex:
runs-on: ubuntu-latest
Expand Down
3 changes: 0 additions & 3 deletions config/sevntu-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<suppress checks="AvoidModifiersForTypes" files=".*[\\/]src[\\/]main[\\/]"/>
<!-- END of legacy code -->


<!-- parsing of xml imply reference attribute names, does not make sense to move all to special variables -->
<suppress checks="MultipleStringLiteralsExtended" files="CheckstyleProfileImporter\.java"/>
<!-- till https://github.com/checkstyle/sonar-checkstyle/issues/135 -->
<suppress checks="MultipleStringLiteralsExtended" files="CheckstylePlugin\.java"/>

Expand Down
2 changes: 0 additions & 2 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<suppress checks="IllegalCatch" files=".*[\\/]src[\\/]main[\\/]"/>
<!-- END of legacy code -->

<!-- parsing of xml imply reference attribute names, does not make sense to move all to special variables -->
<suppress checks="MultipleStringLiterals" files="CheckstyleProfileImporter\.java"/>
<!-- till https://github.com/checkstyle/sonar-checkstyle/issues/135 -->
<suppress checks="MultipleStringLiterals" files=".*[\\/]CheckstylePlugin\.java"/>
<!-- messing test code with such optimization does not make sense , readability will decrease -->
Expand Down
27 changes: 4 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@

<properties>
<checkstyle.version>10.12.5</checkstyle.version>
<sonar.version>8.9.0.43852</sonar.version>
<sonar-java.version>7.2.0.26923</sonar-java.version>
<sonar.version>9.9.4.87374</sonar.version>
<sonar.api-version>9.9.0.229</sonar.api-version>
<maven.sevntu.checkstyle.plugin.version>1.44.1</maven.sevntu.checkstyle.plugin.version>
<!-- it should be a version of checkstyle that is compatible/compiled with sevntu -->
<maven.sevntu.checkstyle.plugin.checkstyle.version>
Expand Down Expand Up @@ -158,9 +158,9 @@
<version>32.0.0-jre</version>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>${sonar.version}</version>
<version>${sonar.api-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -573,7 +573,6 @@
<exclude>org.sonar.plugins.checkstyle.CheckstyleExecutor</exclude>
<exclude>org.sonar.plugins.checkstyle.CheckstyleSeverityUtils</exclude>
<exclude>org.sonar.plugins.checkstyle.CheckstyleConfiguration</exclude>
<exclude>org.sonar.plugins.checkstyle.CheckstyleProfileImporter</exclude>
<exclude>org.sonar.plugins.checkstyle.CheckstyleRulesDefinition</exclude>
<exclude>org.sonar.plugins.checkstyle.metadata.CheckstyleMetadata</exclude>
<exclude>org.sonar.plugins.checkstyle.metadata.CheckUtil</exclude>
Expand Down Expand Up @@ -682,24 +681,6 @@
</limit>
</limits>
</rule>
<rule>
<element>CLASS</element>
<includes>
<include>org.sonar.plugins.checkstyle.CheckstyleProfileImporter</include>
</includes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>1.0</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.96</minimum>
</limit>
</limits>
</rule>
<rule>
<element>CLASS</element>
<includes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.ActiveRule;
Expand All @@ -34,6 +33,7 @@
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.scanner.ScannerSide;

import com.google.common.annotations.VisibleForTesting;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.scanner.ScannerSide;

import com.google.common.annotations.VisibleForTesting;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
Expand All @@ -59,6 +59,17 @@ public class CheckstyleConfiguration {
private final FileSystem fileSystem;

public CheckstyleConfiguration(
org.sonar.api.config.Configuration conf,
ActiveRules activeRules,
FileSystem fileSystem) {
this.conf = conf;
this.confExporter = new CheckstyleProfileExporter(conf);
this.activeRules = activeRules;
this.fileSystem = fileSystem;
}

// used for unit testing
protected CheckstyleConfiguration(
org.sonar.api.config.Configuration conf,
CheckstyleProfileExporter confExporter,
ActiveRules activeRules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.scanner.ScannerSide;

import com.google.common.annotations.VisibleForTesting;
import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
import com.puppycrawl.tools.checkstyle.XMLLogger;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;

@ExtensionPoint
@ScannerSide
Expand Down Expand Up @@ -98,16 +97,16 @@ private void executeWithClassLoader() {
LOG.info("Checkstyle output report: {}", xmlReport.getAbsolutePath());
xmlOutput = FileUtils.openOutputStream(xmlReport);
checker.addListener(
new XMLLogger(xmlOutput, AutomaticBean.OutputStreamOptions.CLOSE));
new XMLLogger(xmlOutput, AbstractAutomaticBean.OutputStreamOptions.CLOSE));
}

checker.setCharset(configuration.getCharset().name());
checker.configure(configuration.getCheckstyleConfiguration());
checker.process(configuration
.getSourceFiles()
.stream()
.map(InputFile::file)
.collect(Collectors.toList()));
.stream()
.map(inputFile -> new File(inputFile.uri()))
.collect(Collectors.toList()));
}
catch (Exception ex) {
throw new IllegalStateException("Can not execute Checkstyle", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ public static List getExtensions() {
.name("Checker Filters")
.description(CHECKER_FILTERS_DESCRIPTION)
.type(PropertyType.TEXT)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
.onQualifiers(Qualifiers.PROJECT, Qualifiers.PROJECT).build(),
PropertyDefinition.builder(CheckstyleConstants.TREEWALKER_FILTERS_KEY)
.defaultValue(CheckstyleConstants.TREEWALKER_FILTERS_DEFAULT_VALUE)
.category(CHECKSTYLE_CATEGORY_NAME)
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
.name("Treewalker Filters")
.description(TREEWALKER_FILTERS_DESCRIPTION)
.type(PropertyType.TEXT)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
.onQualifiers(Qualifiers.PROJECT, Qualifiers.PROJECT).build(),
PropertyDefinition.builder(CheckstyleConstants.CHECKER_TAB_WIDTH)
.category(CHECKSTYLE_CATEGORY_NAME)
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
.name("Tab Width")
.description(CHECKER_TAB_WIDTH_DESCRIPTION)
.type(PropertyType.INTEGER)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.onQualifiers(Qualifiers.PROJECT, Qualifiers.PROJECT)
.build(),
PropertyDefinition.builder(CheckstyleConfiguration.PROPERTY_GENERATE_XML)
.defaultValue("false").category(CHECKSTYLE_CATEGORY_NAME)
Expand All @@ -101,7 +101,6 @@ public static List getExtensions() {

CheckstyleSensor.class, CheckstyleConfiguration.class,
CheckstyleExecutor.class, CheckstyleAuditListener.class,
CheckstyleProfileExporter.class, CheckstyleProfileImporter.class,
CheckstyleRulesDefinition.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,13 @@

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Configuration;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapper;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapperScannerImpl;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapperServerImpl;

import com.google.common.annotations.VisibleForTesting;

@ExtensionPoint
@ScannerSide
public class CheckstyleProfileExporter extends ProfileExporter {
public class CheckstyleProfileExporter {

public static final String DOCTYPE_DECLARATION =
"<!DOCTYPE module PUBLIC \"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN\" "
Expand All @@ -57,30 +48,7 @@ public class CheckstyleProfileExporter extends ProfileExporter {
private final Configuration configuration;

public CheckstyleProfileExporter(Configuration configuration) {
super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME);
this.configuration = configuration;
setSupportedLanguages(CheckstyleConstants.JAVA_KEY);
setMimeType("application/xml");
}

@Override
public void exportProfile(RulesProfile profile, Writer writer) {
try {
final List<ActiveRule> activeRules = profile
.getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY);
if (activeRules != null) {
final List<ActiveRuleWrapper> activeRuleWrappers = new ArrayList<>();
for (ActiveRule activeRule : activeRules) {
activeRuleWrappers.add(new ActiveRuleWrapperServerImpl(activeRule));
}
final Map<String, List<ActiveRuleWrapper>> activeRulesByConfigKey =
arrangeByConfigKey(activeRuleWrappers);
generateXml(writer, activeRulesByConfigKey);
}
}
catch (IOException ex) {
throw new IllegalStateException("Fail to export the profile " + profile, ex);
}
}

/**
Expand All @@ -92,13 +60,9 @@ public void exportProfile(RulesProfile profile, Writer writer) {
*/
public void exportProfile(ActiveRules activeRules, Writer writer) {
try {
final List<ActiveRuleWrapper> activeRuleWrappers = new ArrayList<>();
for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules
.findByRepository(CheckstyleConstants.REPOSITORY_KEY)) {
activeRuleWrappers.add(new ActiveRuleWrapperScannerImpl(activeRule));
}
final Map<String, List<ActiveRuleWrapper>> activeRulesByConfigKey =
arrangeByConfigKey(activeRuleWrappers);
final Map<String, List<ActiveRule>> activeRulesByConfigKey =
arrangeByConfigKey(activeRules
.findByRepository(CheckstyleConstants.REPOSITORY_KEY));
generateXml(writer, activeRulesByConfigKey);
}
catch (IOException ex) {
Expand All @@ -108,7 +72,7 @@ public void exportProfile(ActiveRules activeRules, Writer writer) {
}

private void generateXml(Writer writer, Map<String,
List<ActiveRuleWrapper>> activeRulesByConfigKey) throws IOException {
List<ActiveRule>> activeRulesByConfigKey) throws IOException {
appendXmlHeader(writer);
appendTabWidth(writer);
appendCustomFilters(writer);
Expand All @@ -135,20 +99,20 @@ private void appendTabWidth(Writer writer) throws IOException {
}

private static void appendCheckerModules(Writer writer,
Map<String, List<ActiveRuleWrapper>> activeRulesByConfigKey) throws IOException {
for (Map.Entry<String, List<ActiveRuleWrapper>> entry : activeRulesByConfigKey.entrySet()) {
Map<String, List<ActiveRule>> activeRulesByConfigKey) throws IOException {
for (Map.Entry<String, List<ActiveRule>> entry : activeRulesByConfigKey.entrySet()) {
final String configKey = entry.getKey();
if (!isInTreeWalker(configKey)) {
final List<ActiveRuleWrapper> activeRules = entry.getValue();
for (ActiveRuleWrapper activeRule : activeRules) {
final List<ActiveRule> activeRules = entry.getValue();
for (ActiveRule activeRule : activeRules) {
appendModule(writer, activeRule);
}
}
}
}

private void appendTreeWalker(Writer writer,
Map<String, List<ActiveRuleWrapper>> activeRulesByConfigKey) throws IOException {
Map<String, List<ActiveRule>> activeRulesByConfigKey) throws IOException {
writer.append("<module name=\"TreeWalker\">");
if (isSuppressWarningsEnabled()) {
writer.append("<module name=\"SuppressWarningsHolder\"/> ");
Expand All @@ -157,8 +121,8 @@ private void appendTreeWalker(Writer writer,
Collections.sort(ruleSet);
for (String configKey : ruleSet) {
if (isInTreeWalker(configKey)) {
final List<ActiveRuleWrapper> activeRules = activeRulesByConfigKey.get(configKey);
for (ActiveRuleWrapper activeRule : activeRules) {
final List<ActiveRule> activeRules = activeRulesByConfigKey.get(configKey);
for (ActiveRule activeRule : activeRules) {
appendModule(writer, activeRule);
}
}
Expand Down Expand Up @@ -191,41 +155,43 @@ static boolean isInTreeWalker(String configKey) {
return StringUtils.startsWithIgnoreCase(configKey, "Checker/TreeWalker/");
}

private static Map<String, List<ActiveRuleWrapper>> arrangeByConfigKey(
Collection<ActiveRuleWrapper> activeRules) {
final Map<String, List<ActiveRuleWrapper>> result = new HashMap<>();
for (ActiveRuleWrapper activeRule : activeRules) {
final String key = activeRule.getInternalKey();
private static Map<String, List<ActiveRule>> arrangeByConfigKey(
Collection<ActiveRule> activeRules) {
final Map<String, List<ActiveRule>> result = new HashMap<>();
for (ActiveRule activeRule : activeRules) {
final String key = activeRule.internalKey();
if (result.containsKey(key)) {
final List<ActiveRuleWrapper> rules = result.get(key);
final List<ActiveRule> rules = result.get(key);
rules.add(activeRule);
}
else {
final List<ActiveRuleWrapper> rules = new ArrayList<>();
final List<ActiveRule> rules = new ArrayList<>();
rules.add(activeRule);
result.put(key, rules);
}
}
return result;
}

private static void appendModule(Writer writer, ActiveRuleWrapper activeRule)
private static void appendModule(Writer writer, ActiveRule activeRule)
throws IOException {
final String moduleName = StringUtils.substringAfterLast(activeRule.getInternalKey(), "/");
final String moduleName = StringUtils.substringAfterLast(activeRule.internalKey(), "/");
writer.append("<module name=\"");
StringEscapeUtils.escapeXml(writer, moduleName);
writer.append("\">");
if (activeRule.getTemplateRuleKey() != null) {
appendModuleProperty(writer, "id", activeRule.getRuleKey());
if (activeRule.templateRuleKey() != null) {
appendModuleProperty(writer, "id",
activeRule.ruleKey().rule());
}
appendModuleProperty(writer, "severity", activeRule.getSeverity());
appendModuleProperty(writer, "severity",
CheckstyleSeverityUtils.toSeverity(activeRule.severity()));
appendRuleParameters(writer, activeRule);
writer.append(CLOSE_MODULE);
}

private static void appendRuleParameters(Writer writer, ActiveRuleWrapper activeRule)
private static void appendRuleParameters(Writer writer, ActiveRule activeRule)
throws IOException {
for (Map.Entry<String, String> param : activeRule.getParams().entrySet()) {
for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
if (StringUtils.isNotBlank(param.getValue())) {
appendModuleProperty(writer, param.getKey(), param.getValue());
}
Expand Down
Loading

0 comments on commit a73c88b

Please sign in to comment.