diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-06-04 09:48:31 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-07-12 20:21:13 +0200 |
commit | be4e3879670cfe7c26bd1cfed28e29a7b20d2e2a (patch) | |
tree | 5ed538118878fb6c49698082a09b8f8b8775ec48 /sonar-plugin-api/src/main/java/org/sonar/api/batch | |
parent | e3a0108ef0dd26689dc58198e85ba8655c77fb1f (diff) | |
download | sonarqube-be4e3879670cfe7c26bd1cfed28e29a7b20d2e2a.tar.gz sonarqube-be4e3879670cfe7c26bd1cfed28e29a7b20d2e2a.zip |
Extract implementation from plugin API - Scanner rules
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar/api/batch')
9 files changed, 0 insertions, 674 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java deleted file mode 100644 index e27daa0f72f..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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()); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java deleted file mode 100644 index 9d4af800978..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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; - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java deleted file mode 100644 index 845137acf60..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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; - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRule.java deleted file mode 100644 index b4edd1f23ce..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRule.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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(); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRuleParam.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRuleParam.java deleted file mode 100644 index a1a1d9acbb1..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRuleParam.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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; - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java deleted file mode 100644 index 653c333a394..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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); - } - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java deleted file mode 100644 index 44135366318..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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; - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewRuleParam.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewRuleParam.java deleted file mode 100644 index a274b56042a..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewRuleParam.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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; - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/package-info.java deleted file mode 100644 index 30974c64071..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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; |