You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultSensorDescriptor.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.batch.sensor.internal;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Set;
  25. import java.util.function.Predicate;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.Stream;
  28. import javax.annotation.Nullable;
  29. import org.sonar.api.batch.fs.InputFile;
  30. import org.sonar.api.batch.sensor.SensorDescriptor;
  31. import org.sonar.api.config.Configuration;
  32. public class DefaultSensorDescriptor implements SensorDescriptor {
  33. public static final Set<String> HARDCODED_INDEPENDENT_FILE_SENSORS = Collections.unmodifiableSet(Stream.of(
  34. "CSS Metrics",
  35. "CSS Rules",
  36. "HTML",
  37. "XML Sensor"
  38. ).collect(Collectors.toSet()));
  39. private String name;
  40. private String[] languages = new String[0];
  41. private InputFile.Type type = null;
  42. private String[] ruleRepositories = new String[0];
  43. private boolean global = false;
  44. private Predicate<Configuration> configurationPredicate;
  45. private boolean processesFilesIndependently = false;
  46. public String name() {
  47. return name;
  48. }
  49. public Collection<String> languages() {
  50. return Arrays.asList(languages);
  51. }
  52. @Nullable
  53. public InputFile.Type type() {
  54. return type;
  55. }
  56. public Collection<String> ruleRepositories() {
  57. return Arrays.asList(ruleRepositories);
  58. }
  59. public Predicate<Configuration> configurationPredicate() {
  60. return configurationPredicate;
  61. }
  62. public boolean isGlobal() {
  63. return global;
  64. }
  65. public boolean isProcessesFilesIndependently() {
  66. return processesFilesIndependently;
  67. }
  68. @Override
  69. public DefaultSensorDescriptor name(String name) {
  70. // TODO: Remove this hardcoded list once all plugins will implement the new API "processFilesIndependently"
  71. if (HARDCODED_INDEPENDENT_FILE_SENSORS.contains(name)) {
  72. processesFilesIndependently = true;
  73. }
  74. this.name = name;
  75. return this;
  76. }
  77. @Override
  78. public DefaultSensorDescriptor onlyOnLanguage(String languageKey) {
  79. return onlyOnLanguages(languageKey);
  80. }
  81. @Override
  82. public DefaultSensorDescriptor onlyOnLanguages(String... languageKeys) {
  83. this.languages = languageKeys;
  84. return this;
  85. }
  86. @Override
  87. public DefaultSensorDescriptor onlyOnFileType(InputFile.Type type) {
  88. this.type = type;
  89. return this;
  90. }
  91. @Override
  92. public DefaultSensorDescriptor createIssuesForRuleRepository(String... repositoryKey) {
  93. return createIssuesForRuleRepositories(repositoryKey);
  94. }
  95. @Override
  96. public DefaultSensorDescriptor createIssuesForRuleRepositories(String... repositoryKeys) {
  97. this.ruleRepositories = repositoryKeys;
  98. return this;
  99. }
  100. @Override
  101. public SensorDescriptor global() {
  102. this.global = true;
  103. return this;
  104. }
  105. @Override
  106. public SensorDescriptor onlyWhenConfiguration(Predicate<Configuration> configurationPredicate) {
  107. this.configurationPredicate = configurationPredicate;
  108. return this;
  109. }
  110. @Override
  111. public SensorDescriptor processesFilesIndependently() {
  112. this.processesFilesIndependently = true;
  113. return this;
  114. }
  115. }