aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/batch
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2019-06-19 13:56:51 -0500
committerSonarTech <sonartech@sonarsource.com>2019-07-12 20:21:14 +0200
commit93dc9770902dc7e168869d88b5ad731bfc0bedd9 (patch)
tree97ba885661d5cd9a2115fe212df31bacec9f9947 /sonar-plugin-api/src/main/java/org/sonar/api/batch
parent7c7d9b6b90244d2c974207862071caccdb2c9bb5 (diff)
downloadsonarqube-93dc9770902dc7e168869d88b5ad731bfc0bedd9.tar.gz
sonarqube-93dc9770902dc7e168869d88b5ad731bfc0bedd9.zip
Extract implementation from plugin API and create new module sonar-plugin-api-impl
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar/api/batch')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/Metadata.java71
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java136
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java41
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/LoadedActiveRule.java111
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewActiveRule.java134
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewRule.java92
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewRuleParam.java (renamed from sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/package-info.java)18
7 files changed, 352 insertions, 251 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/Metadata.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/Metadata.java
deleted file mode 100644
index 50c0d753bcd..00000000000
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/Metadata.java
+++ /dev/null
@@ -1,71 +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.fs.internal;
-
-import java.util.Arrays;
-
-import javax.annotation.concurrent.Immutable;
-
-@Immutable
-public class Metadata {
- private final int lines;
- private final int nonBlankLines;
- private final String hash;
- private final int[] originalLineStartOffsets;
- private final int[] originalLineEndOffsets;
- private final int lastValidOffset;
-
- public Metadata(int lines, int nonBlankLines, String hash, int[] originalLineStartOffsets, int[] originalLineEndOffsets, int lastValidOffset) {
- this.lines = lines;
- this.nonBlankLines = nonBlankLines;
- this.hash = hash;
- this.originalLineStartOffsets = Arrays.copyOf(originalLineStartOffsets, originalLineStartOffsets.length);
- this.originalLineEndOffsets = Arrays.copyOf(originalLineEndOffsets, originalLineEndOffsets.length);
- this.lastValidOffset = lastValidOffset;
- }
-
- public int lines() {
- return lines;
- }
-
- public int nonBlankLines() {
- return nonBlankLines;
- }
-
- public String hash() {
- return hash;
- }
-
- public int[] originalLineStartOffsets() {
- return originalLineStartOffsets;
- }
-
- public int[] originalLineEndOffsets() {
- return originalLineEndOffsets;
- }
-
- public int lastValidOffset() {
- return lastValidOffset;
- }
-
- public boolean isEmpty() {
- return lastValidOffset == 0;
- }
-}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java
deleted file mode 100644
index 41287d42011..00000000000
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java
+++ /dev/null
@@ -1,136 +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.fs.internal;
-
-import java.nio.file.Path;
-import javax.annotation.concurrent.ThreadSafe;
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.utils.PathUtils;
-import org.sonar.api.utils.WildcardPattern;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-
-@ThreadSafe
-public abstract class PathPattern {
-
- private static final Logger LOG = Loggers.get(PathPattern.class);
-
- /**
- * @deprecated since 6.6
- */
- @Deprecated
- private static final String ABSOLUTE_PATH_PATTERN_PREFIX = "file:";
- final WildcardPattern pattern;
-
- PathPattern(String pattern) {
- this.pattern = WildcardPattern.create(pattern);
- }
-
- public abstract boolean match(Path absolutePath, Path relativePath);
-
- public abstract boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension);
-
- public static PathPattern create(String s) {
- String trimmed = StringUtils.trim(s);
- if (StringUtils.startsWithIgnoreCase(trimmed, ABSOLUTE_PATH_PATTERN_PREFIX)) {
- LOG.warn("Using absolute path pattern is deprecated. Please use relative path instead of '" + trimmed + "'");
- return new AbsolutePathPattern(StringUtils.substring(trimmed, ABSOLUTE_PATH_PATTERN_PREFIX.length()));
- }
- return new RelativePathPattern(trimmed);
- }
-
- public static PathPattern[] create(String[] s) {
- PathPattern[] result = new PathPattern[s.length];
- for (int i = 0; i < s.length; i++) {
- result[i] = create(s[i]);
- }
- return result;
- }
-
- /**
- * @deprecated since 6.6
- */
- @Deprecated
- private static class AbsolutePathPattern extends PathPattern {
- private AbsolutePathPattern(String pattern) {
- super(pattern);
- }
-
- @Override
- public boolean match(Path absolutePath, Path relativePath) {
- return match(absolutePath, relativePath, true);
- }
-
- @Override
- public boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension) {
- String path = PathUtils.sanitize(absolutePath.toString());
- if (!caseSensitiveFileExtension) {
- String extension = sanitizeExtension(FilenameUtils.getExtension(path));
- if (StringUtils.isNotBlank(extension)) {
- path = StringUtils.removeEndIgnoreCase(path, extension);
- path = path + extension;
- }
- }
- return pattern.match(path);
- }
-
- @Override
- public String toString() {
- return ABSOLUTE_PATH_PATTERN_PREFIX + pattern.toString();
- }
- }
-
- /**
- * Path relative to module basedir
- */
- private static class RelativePathPattern extends PathPattern {
- private RelativePathPattern(String pattern) {
- super(pattern);
- }
-
- @Override
- public boolean match(Path absolutePath, Path relativePath) {
- return match(absolutePath, relativePath, true);
- }
-
- @Override
- public boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension) {
- String path = PathUtils.sanitize(relativePath.toString());
- if (!caseSensitiveFileExtension) {
- String extension = sanitizeExtension(FilenameUtils.getExtension(path));
- if (StringUtils.isNotBlank(extension)) {
- path = StringUtils.removeEndIgnoreCase(path, extension);
- path = path + extension;
- }
- }
- return path != null && pattern.match(path);
- }
-
- @Override
- public String toString() {
- return pattern.toString();
- }
- }
-
- static String sanitizeExtension(String suffix) {
- return StringUtils.lowerCase(StringUtils.removeStart(suffix, "."));
- }
-}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java
deleted file mode 100644
index adde73809f6..00000000000
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java
+++ /dev/null
@@ -1,41 +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.fs.internal;
-
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * A shared, mutable object in the project container.
- * It's used during the execution of sensors to decide whether
- * sensors should be executed once for the entire project, or per-module.
- * It is also injected into each InputFile to change the behavior of {@link InputFile#relativePath()}
- */
-public class SensorStrategy {
-
- private boolean global = true;
-
- public boolean isGlobal() {
- return global;
- }
-
- public void setGlobal(boolean global) {
- this.global = global;
- }
-}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/LoadedActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/LoadedActiveRule.java
new file mode 100644
index 00000000000..fa22fb46f13
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/LoadedActiveRule.java
@@ -0,0 +1,111 @@
+/*
+ * 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;
+
+import java.util.Map;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+import org.sonar.api.rule.RuleKey;
+
+public class LoadedActiveRule {
+ private RuleKey ruleKey;
+ private String severity;
+ private String name;
+ private String language;
+ private Map<String, String> params;
+ private long createdAt;
+ private long updatedAt;
+ private String templateRuleKey;
+ private String internalKey;
+
+ public RuleKey getRuleKey() {
+ return ruleKey;
+ }
+
+ public void setRuleKey(RuleKey ruleKey) {
+ this.ruleKey = ruleKey;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getSeverity() {
+ return severity;
+ }
+
+ public void setSeverity(String severity) {
+ this.severity = severity;
+ }
+
+ public String getLanguage() {
+ return language;
+ }
+
+ public void setLanguage(String language) {
+ this.language = language;
+ }
+
+ public Map<String, String> getParams() {
+ return params;
+ }
+
+ public void setParams(Map<String, String> params) {
+ this.params = params;
+ }
+
+ public long getCreatedAt() {
+ return createdAt;
+ }
+
+ public void setCreatedAt(long createdAt) {
+ this.createdAt = createdAt;
+ }
+
+ public long getUpdatedAt() {
+ return updatedAt;
+ }
+
+ public void setUpdatedAt(long updatedAt) {
+ this.updatedAt = updatedAt;
+ }
+
+ @CheckForNull
+ public String getTemplateRuleKey() {
+ return templateRuleKey;
+ }
+
+ public void setTemplateRuleKey(@Nullable String templateRuleKey) {
+ this.templateRuleKey = templateRuleKey;
+ }
+
+ public String getInternalKey() {
+ return internalKey;
+ }
+
+ public void setInternalKey(String internalKey) {
+ this.internalKey = internalKey;
+ }
+
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewActiveRule.java
new file mode 100644
index 00000000000..6f14a13a990
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewActiveRule.java
@@ -0,0 +1,134 @@
+/*
+ * 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;
+
+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 RuleKey ruleKey() {
+ return this.ruleKey;
+ }
+
+ 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/NewRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewRule.java
new file mode 100644
index 00000000000..edcf70f8ce6
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewRule.java
@@ -0,0 +1,92 @@
+/*
+ * 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;
+
+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;
+ }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewRuleParam.java
index 4af8d775684..f06830d9245 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/package-info.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/NewRuleParam.java
@@ -17,8 +17,20 @@
* 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.fs.internal;
+package org.sonar.api.batch.rule;
-import javax.annotation.ParametersAreNonnullByDefault;
+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;
+ }
+}