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.

ProjectSensorContext.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scanner.sensor;
  21. import java.io.Serializable;
  22. import javax.annotation.concurrent.ThreadSafe;
  23. import org.sonar.api.SonarRuntime;
  24. import org.sonar.api.batch.fs.FileSystem;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.fs.InputModule;
  27. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  28. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  29. import org.sonar.api.batch.rule.ActiveRules;
  30. import org.sonar.api.batch.sensor.SensorContext;
  31. import org.sonar.api.batch.sensor.code.NewSignificantCode;
  32. import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
  33. import org.sonar.api.batch.sensor.coverage.NewCoverage;
  34. import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage;
  35. import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
  36. import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens;
  37. import org.sonar.api.batch.sensor.error.NewAnalysisError;
  38. import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
  39. import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
  40. import org.sonar.api.batch.sensor.internal.SensorStorage;
  41. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  42. import org.sonar.api.batch.sensor.issue.NewIssue;
  43. import org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue;
  44. import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
  45. import org.sonar.api.batch.sensor.measure.NewMeasure;
  46. import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
  47. import org.sonar.api.batch.sensor.rule.NewAdHocRule;
  48. import org.sonar.api.batch.sensor.rule.internal.DefaultAdHocRule;
  49. import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
  50. import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
  51. import org.sonar.api.config.Configuration;
  52. import org.sonar.api.config.Settings;
  53. import org.sonar.api.scanner.fs.InputProject;
  54. import org.sonar.api.utils.Version;
  55. import org.sonar.scanner.sensor.noop.NoOpNewAnalysisError;
  56. @ThreadSafe
  57. public class ProjectSensorContext implements SensorContext {
  58. static final NoOpNewAnalysisError NO_OP_NEW_ANALYSIS_ERROR = new NoOpNewAnalysisError();
  59. private final Settings mutableSettings;
  60. private final FileSystem fs;
  61. private final ActiveRules activeRules;
  62. private final SensorStorage sensorStorage;
  63. private final DefaultInputProject project;
  64. private final SonarRuntime sonarRuntime;
  65. private final Configuration config;
  66. public ProjectSensorContext(DefaultInputProject project, Configuration config, Settings mutableSettings, FileSystem fs, ActiveRules activeRules,
  67. SensorStorage sensorStorage, SonarRuntime sonarRuntime) {
  68. this.project = project;
  69. this.config = config;
  70. this.mutableSettings = mutableSettings;
  71. this.fs = fs;
  72. this.activeRules = activeRules;
  73. this.sensorStorage = sensorStorage;
  74. this.sonarRuntime = sonarRuntime;
  75. }
  76. @Override
  77. public Settings settings() {
  78. return mutableSettings;
  79. }
  80. @Override
  81. public Configuration config() {
  82. return config;
  83. }
  84. @Override
  85. public FileSystem fileSystem() {
  86. return fs;
  87. }
  88. @Override
  89. public ActiveRules activeRules() {
  90. return activeRules;
  91. }
  92. @Override
  93. public InputModule module() {
  94. throw new UnsupportedOperationException("No modules for global Sensors");
  95. }
  96. @Override
  97. public InputProject project() {
  98. return project;
  99. }
  100. @Override
  101. public Version getSonarQubeVersion() {
  102. return sonarRuntime.getApiVersion();
  103. }
  104. @Override
  105. public SonarRuntime runtime() {
  106. return sonarRuntime;
  107. }
  108. @Override
  109. public <G extends Serializable> NewMeasure<G> newMeasure() {
  110. return new DefaultMeasure<>(sensorStorage);
  111. }
  112. @Override
  113. public NewIssue newIssue() {
  114. return new DefaultIssue(project, sensorStorage);
  115. }
  116. @Override
  117. public NewExternalIssue newExternalIssue() {
  118. return new DefaultExternalIssue(project, sensorStorage);
  119. }
  120. @Override
  121. public NewAdHocRule newAdHocRule() {
  122. return new DefaultAdHocRule(sensorStorage);
  123. }
  124. @Override
  125. public NewHighlighting newHighlighting() {
  126. return new DefaultHighlighting(sensorStorage);
  127. }
  128. @Override
  129. public NewSymbolTable newSymbolTable() {
  130. return new DefaultSymbolTable(sensorStorage);
  131. }
  132. @Override
  133. public NewCoverage newCoverage() {
  134. return new DefaultCoverage(sensorStorage);
  135. }
  136. @Override
  137. public NewCpdTokens newCpdTokens() {
  138. return new DefaultCpdTokens(sensorStorage);
  139. }
  140. @Override
  141. public NewAnalysisError newAnalysisError() {
  142. return NO_OP_NEW_ANALYSIS_ERROR;
  143. }
  144. @Override
  145. public boolean isCancelled() {
  146. return false;
  147. }
  148. @Override
  149. public void addContextProperty(String key, String value) {
  150. sensorStorage.storeProperty(key, value);
  151. }
  152. @Override
  153. public void markForPublishing(InputFile inputFile) {
  154. DefaultInputFile file = (DefaultInputFile) inputFile;
  155. file.setPublished(true);
  156. }
  157. @Override
  158. public NewSignificantCode newSignificantCode() {
  159. return new DefaultSignificantCode(sensorStorage);
  160. }
  161. }