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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.scanner.fs.InputProject;
  53. import org.sonar.api.utils.Version;
  54. import org.sonar.scanner.sensor.noop.NoOpNewAnalysisError;
  55. @ThreadSafe
  56. public class ProjectSensorContext implements SensorContext {
  57. static final NoOpNewAnalysisError NO_OP_NEW_ANALYSIS_ERROR = new NoOpNewAnalysisError();
  58. private final FileSystem fs;
  59. private final ActiveRules activeRules;
  60. private final SensorStorage sensorStorage;
  61. private final DefaultInputProject project;
  62. private final SonarRuntime sonarRuntime;
  63. private final Configuration config;
  64. public ProjectSensorContext(DefaultInputProject project, Configuration config, FileSystem fs, ActiveRules activeRules,
  65. SensorStorage sensorStorage, SonarRuntime sonarRuntime) {
  66. this.project = project;
  67. this.config = config;
  68. this.fs = fs;
  69. this.activeRules = activeRules;
  70. this.sensorStorage = sensorStorage;
  71. this.sonarRuntime = sonarRuntime;
  72. }
  73. @Override
  74. public Configuration config() {
  75. return config;
  76. }
  77. @Override
  78. public FileSystem fileSystem() {
  79. return fs;
  80. }
  81. @Override
  82. public ActiveRules activeRules() {
  83. return activeRules;
  84. }
  85. @Override
  86. public InputModule module() {
  87. throw new UnsupportedOperationException("No modules for global Sensors");
  88. }
  89. @Override
  90. public InputProject project() {
  91. return project;
  92. }
  93. @Override
  94. public Version getSonarQubeVersion() {
  95. return sonarRuntime.getApiVersion();
  96. }
  97. @Override
  98. public SonarRuntime runtime() {
  99. return sonarRuntime;
  100. }
  101. @Override
  102. public <G extends Serializable> NewMeasure<G> newMeasure() {
  103. return new DefaultMeasure<>(sensorStorage);
  104. }
  105. @Override
  106. public NewIssue newIssue() {
  107. return new DefaultIssue(project, sensorStorage);
  108. }
  109. @Override
  110. public NewExternalIssue newExternalIssue() {
  111. return new DefaultExternalIssue(project, sensorStorage);
  112. }
  113. @Override
  114. public NewAdHocRule newAdHocRule() {
  115. return new DefaultAdHocRule(sensorStorage);
  116. }
  117. @Override
  118. public NewHighlighting newHighlighting() {
  119. return new DefaultHighlighting(sensorStorage);
  120. }
  121. @Override
  122. public NewSymbolTable newSymbolTable() {
  123. return new DefaultSymbolTable(sensorStorage);
  124. }
  125. @Override
  126. public NewCoverage newCoverage() {
  127. return new DefaultCoverage(sensorStorage);
  128. }
  129. @Override
  130. public NewCpdTokens newCpdTokens() {
  131. return new DefaultCpdTokens(sensorStorage);
  132. }
  133. @Override
  134. public NewAnalysisError newAnalysisError() {
  135. return NO_OP_NEW_ANALYSIS_ERROR;
  136. }
  137. @Override
  138. public boolean isCancelled() {
  139. return false;
  140. }
  141. @Override
  142. public void addContextProperty(String key, String value) {
  143. sensorStorage.storeProperty(key, value);
  144. }
  145. @Override
  146. public void markForPublishing(InputFile inputFile) {
  147. DefaultInputFile file = (DefaultInputFile) inputFile;
  148. file.setPublished(true);
  149. }
  150. @Override
  151. public NewSignificantCode newSignificantCode() {
  152. return new DefaultSignificantCode(sensorStorage);
  153. }
  154. }