Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DefaultSensorContext.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.AnalysisMode;
  25. import org.sonar.api.batch.fs.FileSystem;
  26. import org.sonar.api.batch.fs.InputFile;
  27. import org.sonar.api.batch.fs.InputModule;
  28. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  29. import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
  30. import org.sonar.api.batch.rule.ActiveRules;
  31. import org.sonar.api.batch.sensor.SensorContext;
  32. import org.sonar.api.batch.sensor.code.NewSignificantCode;
  33. import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
  34. import org.sonar.api.batch.sensor.coverage.NewCoverage;
  35. import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage;
  36. import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
  37. import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens;
  38. import org.sonar.api.batch.sensor.error.NewAnalysisError;
  39. import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
  40. import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
  41. import org.sonar.api.batch.sensor.internal.SensorStorage;
  42. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  43. import org.sonar.api.batch.sensor.issue.NewIssue;
  44. import org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue;
  45. import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
  46. import org.sonar.api.batch.sensor.measure.NewMeasure;
  47. import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
  48. import org.sonar.api.batch.sensor.rule.NewAdHocRule;
  49. import org.sonar.api.batch.sensor.rule.internal.DefaultAdHocRule;
  50. import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
  51. import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
  52. import org.sonar.api.config.Configuration;
  53. import org.sonar.api.config.Settings;
  54. import org.sonar.api.utils.Version;
  55. import org.sonar.scanner.sensor.noop.NoOpNewAdHocRule;
  56. import org.sonar.scanner.sensor.noop.NoOpNewAnalysisError;
  57. import org.sonar.scanner.sensor.noop.NoOpNewCpdTokens;
  58. import org.sonar.scanner.sensor.noop.NoOpNewExternalIssue;
  59. import org.sonar.scanner.sensor.noop.NoOpNewHighlighting;
  60. import org.sonar.scanner.sensor.noop.NoOpNewSignificantCode;
  61. import org.sonar.scanner.sensor.noop.NoOpNewSymbolTable;
  62. @ThreadSafe
  63. public class DefaultSensorContext implements SensorContext {
  64. static final NoOpNewHighlighting NO_OP_NEW_HIGHLIGHTING = new NoOpNewHighlighting();
  65. static final NoOpNewSymbolTable NO_OP_NEW_SYMBOL_TABLE = new NoOpNewSymbolTable();
  66. static final NoOpNewCpdTokens NO_OP_NEW_CPD_TOKENS = new NoOpNewCpdTokens();
  67. static final NoOpNewAnalysisError NO_OP_NEW_ANALYSIS_ERROR = new NoOpNewAnalysisError();
  68. static final NoOpNewExternalIssue NO_OP_NEW_EXTERNAL_ISSUE = new NoOpNewExternalIssue();
  69. static final NoOpNewAdHocRule NO_OP_NEW_AD_HOC_RULE = new NoOpNewAdHocRule();
  70. static final NoOpNewSignificantCode NO_OP_NEW_SIGNIFICANT_CODE = new NoOpNewSignificantCode();
  71. private final Settings mutableSettings;
  72. private final FileSystem fs;
  73. private final ActiveRules activeRules;
  74. private final SensorStorage sensorStorage;
  75. private final AnalysisMode analysisMode;
  76. private final InputModule module;
  77. private final SonarRuntime sonarRuntime;
  78. private final Configuration config;
  79. private final InputModuleHierarchy hierarchy;
  80. public DefaultSensorContext(InputModule module, Configuration config, Settings mutableSettings, FileSystem fs, ActiveRules activeRules,
  81. AnalysisMode analysisMode, SensorStorage sensorStorage, SonarRuntime sonarRuntime, InputModuleHierarchy hierarchy) {
  82. this.module = module;
  83. this.config = config;
  84. this.mutableSettings = mutableSettings;
  85. this.fs = fs;
  86. this.activeRules = activeRules;
  87. this.analysisMode = analysisMode;
  88. this.sensorStorage = sensorStorage;
  89. this.sonarRuntime = sonarRuntime;
  90. this.hierarchy = hierarchy;
  91. }
  92. @Override
  93. public Settings settings() {
  94. return mutableSettings;
  95. }
  96. @Override
  97. public Configuration config() {
  98. return config;
  99. }
  100. @Override
  101. public FileSystem fileSystem() {
  102. return fs;
  103. }
  104. @Override
  105. public ActiveRules activeRules() {
  106. return activeRules;
  107. }
  108. @Override
  109. public InputModule module() {
  110. return module;
  111. }
  112. @Override
  113. public Version getSonarQubeVersion() {
  114. return sonarRuntime.getApiVersion();
  115. }
  116. @Override
  117. public SonarRuntime runtime() {
  118. return sonarRuntime;
  119. }
  120. @Override
  121. public <G extends Serializable> NewMeasure<G> newMeasure() {
  122. return new DefaultMeasure<>(sensorStorage);
  123. }
  124. @Override
  125. public NewIssue newIssue() {
  126. return new DefaultIssue(hierarchy.root(), sensorStorage);
  127. }
  128. @Override
  129. public NewExternalIssue newExternalIssue() {
  130. if (analysisMode.isIssues()) {
  131. return NO_OP_NEW_EXTERNAL_ISSUE;
  132. }
  133. return new DefaultExternalIssue(hierarchy.root(), sensorStorage);
  134. }
  135. @Override
  136. public NewAdHocRule newAdHocRule() {
  137. if (analysisMode.isIssues()) {
  138. return NO_OP_NEW_AD_HOC_RULE;
  139. }
  140. return new DefaultAdHocRule(sensorStorage);
  141. }
  142. @Override
  143. public NewHighlighting newHighlighting() {
  144. if (analysisMode.isIssues()) {
  145. return NO_OP_NEW_HIGHLIGHTING;
  146. }
  147. return new DefaultHighlighting(sensorStorage);
  148. }
  149. @Override
  150. public NewSymbolTable newSymbolTable() {
  151. if (analysisMode.isIssues()) {
  152. return NO_OP_NEW_SYMBOL_TABLE;
  153. }
  154. return new DefaultSymbolTable(sensorStorage);
  155. }
  156. @Override
  157. public NewCoverage newCoverage() {
  158. return new DefaultCoverage(sensorStorage);
  159. }
  160. @Override
  161. public NewCpdTokens newCpdTokens() {
  162. if (analysisMode.isIssues()) {
  163. return NO_OP_NEW_CPD_TOKENS;
  164. }
  165. return new DefaultCpdTokens(config, sensorStorage);
  166. }
  167. @Override
  168. public NewAnalysisError newAnalysisError() {
  169. return NO_OP_NEW_ANALYSIS_ERROR;
  170. }
  171. @Override
  172. public boolean isCancelled() {
  173. return false;
  174. }
  175. @Override
  176. public void addContextProperty(String key, String value) {
  177. sensorStorage.storeProperty(key, value);
  178. }
  179. @Override
  180. public void markForPublishing(InputFile inputFile) {
  181. DefaultInputFile file = (DefaultInputFile) inputFile;
  182. file.setPublished(true);
  183. }
  184. @Override
  185. public NewSignificantCode newSignificantCode() {
  186. if (analysisMode.isIssues()) {
  187. return NO_OP_NEW_SIGNIFICANT_CODE;
  188. }
  189. return new DefaultSignificantCode(sensorStorage);
  190. }
  191. }