+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.rule.RuleKey;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * Builds instances of {@link org.sonar.api.batch.rule.ActiveRules}.
- * <b>For unit testing and internal use only</b>.
- *
- * @since 4.2
- */
-public class ActiveRulesBuilder {
-
- private final Map<RuleKey, NewActiveRule> map = new LinkedHashMap<>();
-
- public ActiveRulesBuilder addRule(NewActiveRule newActiveRule) {
- if (map.containsKey(newActiveRule.ruleKey)) {
- throw new IllegalStateException(String.format("Rule '%s' is already activated", newActiveRule.ruleKey));
- }
- map.put(newActiveRule.ruleKey, newActiveRule);
- return this;
- }
-
- public ActiveRules build() {
- return new DefaultActiveRules(map.values());
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import javax.annotation.concurrent.Immutable;
-import org.sonar.api.batch.rule.ActiveRule;
-import org.sonar.api.rule.RuleKey;
-
-@Immutable
-public class DefaultActiveRule implements ActiveRule {
- private final RuleKey ruleKey;
- private final String severity;
- private final String internalKey;
- private final String language;
- private final String templateRuleKey;
- private final Map<String, String> params;
- private final long createdAt;
- private final long updatedAt;
- private final String qProfileKey;
-
- DefaultActiveRule(NewActiveRule newActiveRule) {
- this.severity = newActiveRule.severity;
- this.internalKey = newActiveRule.internalKey;
- this.templateRuleKey = newActiveRule.templateRuleKey;
- this.ruleKey = newActiveRule.ruleKey;
- this.params = Collections.unmodifiableMap(new HashMap<>(newActiveRule.params));
- this.language = newActiveRule.language;
- this.createdAt = newActiveRule.createdAt;
- this.updatedAt = newActiveRule.updatedAt;
- this.qProfileKey = newActiveRule.qProfileKey;
- }
-
- @Override
- public RuleKey ruleKey() {
- return ruleKey;
- }
-
- @Override
- public String severity() {
- return severity;
- }
-
- @Override
- public String language() {
- return language;
- }
-
- @Override
- public String param(String key) {
- return params.get(key);
- }
-
- @Override
- public Map<String, String> params() {
- // already immutable
- return params;
- }
-
- @Override
- public String internalKey() {
- return internalKey;
- }
-
- @Override
- public String templateRuleKey() {
- return templateRuleKey;
- }
-
- public long createdAt() {
- return createdAt;
- }
-
- public long updatedAt() {
- return updatedAt;
- }
-
- @Override
- public String qpKey() {
- return qProfileKey;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-import javax.annotation.concurrent.Immutable;
-import org.sonar.api.batch.rule.ActiveRule;
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.rule.RuleKey;
-
-@Immutable
-public class DefaultActiveRules implements ActiveRules {
- private final Map<String, List<ActiveRule>> activeRulesByRepository = new HashMap<>();
- private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndKey = new HashMap<>();
- private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndInternalKey = new HashMap<>();
- private final Map<String, List<ActiveRule>> activeRulesByLanguage = new HashMap<>();
-
- public DefaultActiveRules(Collection<NewActiveRule> newActiveRules) {
- for (NewActiveRule newAR : newActiveRules) {
- DefaultActiveRule ar = new DefaultActiveRule(newAR);
- String repo = ar.ruleKey().repository();
- activeRulesByRepository.computeIfAbsent(repo, x -> new ArrayList<>()).add(ar);
- if (ar.language() != null) {
- activeRulesByLanguage.computeIfAbsent(ar.language(), x -> new ArrayList<>()).add(ar);
- }
-
- activeRulesByRepositoryAndKey.computeIfAbsent(repo, r -> new HashMap<>()).put(ar.ruleKey().rule(), ar);
- String internalKey = ar.internalKey();
- if (internalKey != null) {
- activeRulesByRepositoryAndInternalKey.computeIfAbsent(repo, r -> new HashMap<>()).put(internalKey, ar);
- }
- }
- }
-
- @Override
- public ActiveRule find(RuleKey ruleKey) {
- return activeRulesByRepositoryAndKey.getOrDefault(ruleKey.repository(), Collections.emptyMap())
- .get(ruleKey.rule());
- }
-
- @Override
- public Collection<ActiveRule> findAll() {
- return activeRulesByRepository.entrySet().stream().flatMap(x -> x.getValue().stream()).collect(Collectors.toList());
- }
-
- @Override
- public Collection<ActiveRule> findByRepository(String repository) {
- return activeRulesByRepository.getOrDefault(repository, Collections.emptyList());
- }
-
- @Override
- public Collection<ActiveRule> findByLanguage(String language) {
- return activeRulesByLanguage.getOrDefault(language, Collections.emptyList());
- }
-
- @Override
- public ActiveRule findByInternalKey(String repository, String internalKey) {
- return activeRulesByRepositoryAndInternalKey.containsKey(repository) ? activeRulesByRepositoryAndInternalKey.get(repository).get(internalKey) : null;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.concurrent.Immutable;
-import org.sonar.api.batch.rule.Rule;
-import org.sonar.api.batch.rule.RuleParam;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
-
-@Immutable
-public class DefaultRule implements Rule {
-
- private final RuleKey key;
- private final Integer id;
- private final String name;
- private final String severity;
- private final String type;
- private final String description;
- private final String internalKey;
- private final RuleStatus status;
- private final Map<String, RuleParam> params;
-
- DefaultRule(NewRule newRule) {
- this.key = newRule.key;
- this.id = newRule.id;
- this.name = newRule.name;
- this.severity = newRule.severity;
- this.type = newRule.type;
- this.description = newRule.description;
- this.internalKey = newRule.internalKey;
- this.status = newRule.status;
-
- Map<String, RuleParam> builder = new HashMap<>();
- for (NewRuleParam newRuleParam : newRule.params.values()) {
- builder.put(newRuleParam.key, new DefaultRuleParam(newRuleParam));
- }
- params = Collections.unmodifiableMap(builder);
- }
-
- @Override
- public RuleKey key() {
- return key;
- }
-
- @CheckForNull
- public Integer id() {
- return id;
- }
-
- @Override
- public String name() {
- return name;
- }
-
- @Override
- public String severity() {
- return severity;
- }
-
- @CheckForNull
- public String type() {
- return type;
- }
-
- @Override
- public String description() {
- return description;
- }
-
- @Override
- public String internalKey() {
- return internalKey;
- }
-
- @Override
- public RuleStatus status() {
- return status;
- }
-
- @Override
- public RuleParam param(String paramKey) {
- return params.get(paramKey);
- }
-
- @Override
- public Collection<RuleParam> params() {
- return params.values();
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import org.sonar.api.batch.rule.RuleParam;
-
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
-
-@Immutable
-class DefaultRuleParam implements RuleParam {
-
- private final String key;
- private final String description;
-
- DefaultRuleParam(NewRuleParam p) {
- this.key = p.key;
- this.description = p.description;
- }
-
- @Override
- public String key() {
- return key;
- }
-
- @Override
- @Nullable
- public String description() {
- return description;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import java.util.HashMap;
-import java.util.Map;
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-
-/**
- * @since 4.2
- */
-@Immutable
-public class NewActiveRule {
- final RuleKey ruleKey;
- final String name;
- final String severity;
- final Map<String, String> params;
- final long createdAt;
- final long updatedAt;
- final String internalKey;
- final String language;
- final String templateRuleKey;
- final String qProfileKey;
-
- NewActiveRule(Builder builder) {
- this.ruleKey = builder.ruleKey;
- this.name = builder.name;
- this.severity = builder.severity;
- this.params = builder.params;
- this.createdAt = builder.createdAt;
- this.updatedAt = builder.updatedAt;
- this.internalKey = builder.internalKey;
- this.language = builder.language;
- this.templateRuleKey = builder.templateRuleKey;
- this.qProfileKey = builder.qProfileKey;
- }
-
- public static class Builder {
- private RuleKey ruleKey;
- private String name;
- private String severity = Severity.defaultSeverity();
- private Map<String, String> params = new HashMap<>();
- private long createdAt;
- private long updatedAt;
- private String internalKey;
- private String language;
- private String templateRuleKey;
- private String qProfileKey;
-
- public Builder setRuleKey(RuleKey ruleKey) {
- this.ruleKey = ruleKey;
- return this;
- }
-
- public Builder setName(String name) {
- this.name = name;
- return this;
- }
-
- public Builder setSeverity(@Nullable String severity) {
- this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
- return this;
- }
-
- public Builder setParam(String key, @Nullable String value) {
- // possible improvement : check that the param key exists in rule definition
- if (value == null) {
- params.remove(key);
- } else {
- params.put(key, value);
- }
- return this;
- }
-
- public Builder setCreatedAt(long createdAt) {
- this.createdAt = createdAt;
- return this;
- }
-
- public Builder setUpdatedAt(long updatedAt) {
- this.updatedAt = updatedAt;
- return this;
- }
-
- public Builder setInternalKey(@Nullable String internalKey) {
- this.internalKey = internalKey;
- return this;
- }
-
- public Builder setLanguage(@Nullable String language) {
- this.language = language;
- return this;
- }
-
- public Builder setTemplateRuleKey(@Nullable String templateRuleKey) {
- this.templateRuleKey = templateRuleKey;
- return this;
- }
-
- public Builder setQProfileKey(String qProfileKey) {
- this.qProfileKey = qProfileKey;
- return this;
- }
-
- public NewActiveRule build() {
- return new NewActiveRule(this);
- }
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import java.util.HashMap;
-import java.util.Map;
-import javax.annotation.Nullable;
-import org.apache.commons.lang.ObjectUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.rule.Severity;
-
-public class NewRule {
-
- private static final String DEFAULT_SEVERITY = Severity.defaultSeverity();
-
- final RuleKey key;
- Integer id;
- String name;
- String description;
- String severity = DEFAULT_SEVERITY;
- String type;
- String internalKey;
- RuleStatus status = RuleStatus.defaultStatus();
- Map<String, NewRuleParam> params = new HashMap<>();
-
- NewRule(RuleKey key) {
- this.key = key;
- }
-
- public NewRule setId(@Nullable Integer id) {
- this.id = id;
- return this;
- }
-
- public NewRule setDescription(@Nullable String description) {
- this.description = description;
- return this;
- }
-
- public NewRule setName(@Nullable String s) {
- this.name = s;
- return this;
- }
-
- public NewRule setSeverity(@Nullable String severity) {
- this.severity = StringUtils.defaultIfBlank(severity, DEFAULT_SEVERITY);
- return this;
- }
-
- public NewRule setType(@Nullable String type) {
- this.type = type;
- return this;
- }
-
- public NewRule setStatus(@Nullable RuleStatus s) {
- this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
- return this;
- }
-
- public NewRule setInternalKey(@Nullable String s) {
- this.internalKey = s;
- return this;
- }
-
- public NewRuleParam addParam(String paramKey) {
- if (params.containsKey(paramKey)) {
- throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key));
- }
- NewRuleParam param = new NewRuleParam(paramKey);
- params.put(paramKey, param);
- return param;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import javax.annotation.Nullable;
-
-public class NewRuleParam {
- final String key;
- String description;
-
- NewRuleParam(String key) {
- this.key = key;
- }
-
- public NewRuleParam setDescription(@Nullable String s) {
- description = s;
- return this;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.api.batch.rule.internal;
-
-import javax.annotation.ParametersAreNonnullByDefault;
package org.sonar.scanner.report;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.DefaultActiveRule;
+import org.sonar.scanner.rule.DefaultActiveRule;
import org.sonar.scanner.protocol.Constants;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Builds instances of {@link org.sonar.api.batch.rule.ActiveRules}.
+ * <b>For unit testing and internal use only</b>.
+ *
+ * @since 4.2
+ */
+public class ActiveRulesBuilder {
+
+ private final Map<RuleKey, NewActiveRule> map = new LinkedHashMap<>();
+
+ public ActiveRulesBuilder addRule(NewActiveRule newActiveRule) {
+ if (map.containsKey(newActiveRule.ruleKey)) {
+ throw new IllegalStateException(String.format("Rule '%s' is already activated", newActiveRule.ruleKey));
+ }
+ map.put(newActiveRule.ruleKey, newActiveRule);
+ return this;
+ }
+
+ public ActiveRules build() {
+ return new DefaultActiveRules(map.values());
+ }
+}
import java.util.Set;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
-import org.sonar.api.batch.rule.internal.NewActiveRule;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.concurrent.Immutable;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.rule.RuleKey;
+
+@Immutable
+public class DefaultActiveRule implements ActiveRule {
+ private final RuleKey ruleKey;
+ private final String severity;
+ private final String internalKey;
+ private final String language;
+ private final String templateRuleKey;
+ private final Map<String, String> params;
+ private final long createdAt;
+ private final long updatedAt;
+ private final String qProfileKey;
+
+ DefaultActiveRule(NewActiveRule newActiveRule) {
+ this.severity = newActiveRule.severity;
+ this.internalKey = newActiveRule.internalKey;
+ this.templateRuleKey = newActiveRule.templateRuleKey;
+ this.ruleKey = newActiveRule.ruleKey;
+ this.params = Collections.unmodifiableMap(new HashMap<>(newActiveRule.params));
+ this.language = newActiveRule.language;
+ this.createdAt = newActiveRule.createdAt;
+ this.updatedAt = newActiveRule.updatedAt;
+ this.qProfileKey = newActiveRule.qProfileKey;
+ }
+
+ @Override
+ public RuleKey ruleKey() {
+ return ruleKey;
+ }
+
+ @Override
+ public String severity() {
+ return severity;
+ }
+
+ @Override
+ public String language() {
+ return language;
+ }
+
+ @Override
+ public String param(String key) {
+ return params.get(key);
+ }
+
+ @Override
+ public Map<String, String> params() {
+ // already immutable
+ return params;
+ }
+
+ @Override
+ public String internalKey() {
+ return internalKey;
+ }
+
+ @Override
+ public String templateRuleKey() {
+ return templateRuleKey;
+ }
+
+ public long createdAt() {
+ return createdAt;
+ }
+
+ public long updatedAt() {
+ return updatedAt;
+ }
+
+ @Override
+ public String qpKey() {
+ return qProfileKey;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import javax.annotation.concurrent.Immutable;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+
+@Immutable
+public class DefaultActiveRules implements ActiveRules {
+ private final Map<String, List<ActiveRule>> activeRulesByRepository = new HashMap<>();
+ private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndKey = new HashMap<>();
+ private final Map<String, Map<String, ActiveRule>> activeRulesByRepositoryAndInternalKey = new HashMap<>();
+ private final Map<String, List<ActiveRule>> activeRulesByLanguage = new HashMap<>();
+
+ public DefaultActiveRules(Collection<NewActiveRule> newActiveRules) {
+ for (NewActiveRule newAR : newActiveRules) {
+ DefaultActiveRule ar = new DefaultActiveRule(newAR);
+ String repo = ar.ruleKey().repository();
+ activeRulesByRepository.computeIfAbsent(repo, x -> new ArrayList<>()).add(ar);
+ if (ar.language() != null) {
+ activeRulesByLanguage.computeIfAbsent(ar.language(), x -> new ArrayList<>()).add(ar);
+ }
+
+ activeRulesByRepositoryAndKey.computeIfAbsent(repo, r -> new HashMap<>()).put(ar.ruleKey().rule(), ar);
+ String internalKey = ar.internalKey();
+ if (internalKey != null) {
+ activeRulesByRepositoryAndInternalKey.computeIfAbsent(repo, r -> new HashMap<>()).put(internalKey, ar);
+ }
+ }
+ }
+
+ @Override
+ public ActiveRule find(RuleKey ruleKey) {
+ return activeRulesByRepositoryAndKey.getOrDefault(ruleKey.repository(), Collections.emptyMap())
+ .get(ruleKey.rule());
+ }
+
+ @Override
+ public Collection<ActiveRule> findAll() {
+ return activeRulesByRepository.entrySet().stream().flatMap(x -> x.getValue().stream()).collect(Collectors.toList());
+ }
+
+ @Override
+ public Collection<ActiveRule> findByRepository(String repository) {
+ return activeRulesByRepository.getOrDefault(repository, Collections.emptyList());
+ }
+
+ @Override
+ public Collection<ActiveRule> findByLanguage(String language) {
+ return activeRulesByLanguage.getOrDefault(language, Collections.emptyList());
+ }
+
+ @Override
+ public ActiveRule findByInternalKey(String repository, String internalKey) {
+ return activeRulesByRepositoryAndInternalKey.containsKey(repository) ? activeRulesByRepositoryAndInternalKey.get(repository).get(internalKey) : null;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.CheckForNull;
+import javax.annotation.concurrent.Immutable;
+import org.sonar.api.batch.rule.Rule;
+import org.sonar.api.batch.rule.RuleParam;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+
+@Immutable
+public class DefaultRule implements Rule {
+
+ private final RuleKey key;
+ private final Integer id;
+ private final String name;
+ private final String severity;
+ private final String type;
+ private final String description;
+ private final String internalKey;
+ private final RuleStatus status;
+ private final Map<String, RuleParam> params;
+
+ DefaultRule(NewRule newRule) {
+ this.key = newRule.key;
+ this.id = newRule.id;
+ this.name = newRule.name;
+ this.severity = newRule.severity;
+ this.type = newRule.type;
+ this.description = newRule.description;
+ this.internalKey = newRule.internalKey;
+ this.status = newRule.status;
+
+ Map<String, RuleParam> builder = new HashMap<>();
+ for (NewRuleParam newRuleParam : newRule.params.values()) {
+ builder.put(newRuleParam.key, new DefaultRuleParam(newRuleParam));
+ }
+ params = Collections.unmodifiableMap(builder);
+ }
+
+ @Override
+ public RuleKey key() {
+ return key;
+ }
+
+ @CheckForNull
+ public Integer id() {
+ return id;
+ }
+
+ @Override
+ public String name() {
+ return name;
+ }
+
+ @Override
+ public String severity() {
+ return severity;
+ }
+
+ @CheckForNull
+ public String type() {
+ return type;
+ }
+
+ @Override
+ public String description() {
+ return description;
+ }
+
+ @Override
+ public String internalKey() {
+ return internalKey;
+ }
+
+ @Override
+ public RuleStatus status() {
+ return status;
+ }
+
+ @Override
+ public RuleParam param(String paramKey) {
+ return params.get(paramKey);
+ }
+
+ @Override
+ public Collection<RuleParam> params() {
+ return params.values();
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import org.sonar.api.batch.rule.RuleParam;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.Immutable;
+
+@Immutable
+class DefaultRuleParam implements RuleParam {
+
+ private final String key;
+ private final String description;
+
+ DefaultRuleParam(NewRuleParam p) {
+ this.key = p.key;
+ this.description = p.description;
+ }
+
+ @Override
+ public String key() {
+ return key;
+ }
+
+ @Override
+ @Nullable
+ public String description() {
+ return description;
+ }
+}
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-package org.sonar.api.batch.rule.internal;
+package org.sonar.scanner.rule;
import java.util.ArrayList;
import java.util.Collection;
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.Immutable;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+
+/**
+ * @since 4.2
+ */
+@Immutable
+public class NewActiveRule {
+ final RuleKey ruleKey;
+ final String name;
+ final String severity;
+ final Map<String, String> params;
+ final long createdAt;
+ final long updatedAt;
+ final String internalKey;
+ final String language;
+ final String templateRuleKey;
+ final String qProfileKey;
+
+ NewActiveRule(Builder builder) {
+ this.ruleKey = builder.ruleKey;
+ this.name = builder.name;
+ this.severity = builder.severity;
+ this.params = builder.params;
+ this.createdAt = builder.createdAt;
+ this.updatedAt = builder.updatedAt;
+ this.internalKey = builder.internalKey;
+ this.language = builder.language;
+ this.templateRuleKey = builder.templateRuleKey;
+ this.qProfileKey = builder.qProfileKey;
+ }
+
+ public static class Builder {
+ private RuleKey ruleKey;
+ private String name;
+ private String severity = Severity.defaultSeverity();
+ private Map<String, String> params = new HashMap<>();
+ private long createdAt;
+ private long updatedAt;
+ private String internalKey;
+ private String language;
+ private String templateRuleKey;
+ private String qProfileKey;
+
+ public Builder setRuleKey(RuleKey ruleKey) {
+ this.ruleKey = ruleKey;
+ return this;
+ }
+
+ public Builder setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder setSeverity(@Nullable String severity) {
+ this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
+ return this;
+ }
+
+ public Builder setParam(String key, @Nullable String value) {
+ // possible improvement : check that the param key exists in rule definition
+ if (value == null) {
+ params.remove(key);
+ } else {
+ params.put(key, value);
+ }
+ return this;
+ }
+
+ public Builder setCreatedAt(long createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+
+ public Builder setUpdatedAt(long updatedAt) {
+ this.updatedAt = updatedAt;
+ return this;
+ }
+
+ public Builder setInternalKey(@Nullable String internalKey) {
+ this.internalKey = internalKey;
+ return this;
+ }
+
+ public Builder setLanguage(@Nullable String language) {
+ this.language = language;
+ return this;
+ }
+
+ public Builder setTemplateRuleKey(@Nullable String templateRuleKey) {
+ this.templateRuleKey = templateRuleKey;
+ return this;
+ }
+
+ public Builder setQProfileKey(String qProfileKey) {
+ this.qProfileKey = qProfileKey;
+ return this;
+ }
+
+ public NewActiveRule build() {
+ return new NewActiveRule(this);
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rule.Severity;
+
+public class NewRule {
+
+ private static final String DEFAULT_SEVERITY = Severity.defaultSeverity();
+
+ final RuleKey key;
+ Integer id;
+ String name;
+ String description;
+ String severity = DEFAULT_SEVERITY;
+ String type;
+ String internalKey;
+ RuleStatus status = RuleStatus.defaultStatus();
+ Map<String, NewRuleParam> params = new HashMap<>();
+
+ public NewRule(RuleKey key) {
+ this.key = key;
+ }
+
+ public NewRule setId(@Nullable Integer id) {
+ this.id = id;
+ return this;
+ }
+
+ public NewRule setDescription(@Nullable String description) {
+ this.description = description;
+ return this;
+ }
+
+ public NewRule setName(@Nullable String s) {
+ this.name = s;
+ return this;
+ }
+
+ public NewRule setSeverity(@Nullable String severity) {
+ this.severity = StringUtils.defaultIfBlank(severity, DEFAULT_SEVERITY);
+ return this;
+ }
+
+ public NewRule setType(@Nullable String type) {
+ this.type = type;
+ return this;
+ }
+
+ public NewRule setStatus(@Nullable RuleStatus s) {
+ this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
+ return this;
+ }
+
+ public NewRule setInternalKey(@Nullable String s) {
+ this.internalKey = s;
+ return this;
+ }
+
+ public NewRuleParam addParam(String paramKey) {
+ if (params.containsKey(paramKey)) {
+ throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key));
+ }
+ NewRuleParam param = new NewRuleParam(paramKey);
+ params.put(paramKey, param);
+ return param;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.rule;
+
+import javax.annotation.Nullable;
+
+public class NewRuleParam {
+ final String key;
+ String description;
+
+ NewRuleParam(String key) {
+ this.key = key;
+ }
+
+ public NewRuleParam setDescription(@Nullable String s) {
+ description = s;
+ return this;
+ }
+}
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-package org.sonar.api.batch.rule.internal;
+package org.sonar.scanner.rule;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.rule.RuleKey;
import java.util.List;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.batch.rule.Rules;
-import org.sonar.api.batch.rule.internal.NewRule;
-import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.DefaultTextPointer;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.code.NewSignificantCode;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
-import org.sonar.api.batch.rule.internal.NewActiveRule;
-import org.sonar.api.batch.rule.internal.RulesBuilder;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
+import org.sonar.scanner.rule.NewActiveRule;
+import org.sonar.scanner.rule.RulesBuilder;
import org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.DefaultActiveRules;
-import org.sonar.api.batch.rule.internal.NewActiveRule;
+import org.sonar.scanner.rule.DefaultActiveRules;
+import org.sonar.scanner.rule.NewActiveRule;
import org.sonar.api.rule.RuleKey;
import org.sonar.core.util.CloseableIterator;
import org.sonar.scanner.protocol.Constants;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
-import org.sonar.api.batch.rule.internal.NewActiveRule;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
+import org.sonar.scanner.rule.NewActiveRule;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.rule.RuleKey;