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.

SensorContext.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.api.batch.sensor;
  21. import java.io.Serializable;
  22. import org.sonar.api.SonarRuntime;
  23. import org.sonar.api.batch.fs.FileSystem;
  24. import org.sonar.api.batch.fs.InputFile;
  25. import org.sonar.api.batch.fs.InputModule;
  26. import org.sonar.api.batch.rule.ActiveRules;
  27. import org.sonar.api.batch.sensor.code.NewSignificantCode;
  28. import org.sonar.api.batch.sensor.coverage.NewCoverage;
  29. import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
  30. import org.sonar.api.batch.sensor.error.NewAnalysisError;
  31. import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
  32. import org.sonar.api.batch.sensor.issue.ExternalIssue;
  33. import org.sonar.api.batch.sensor.issue.Issue;
  34. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  35. import org.sonar.api.batch.sensor.issue.NewIssue;
  36. import org.sonar.api.batch.sensor.measure.Measure;
  37. import org.sonar.api.batch.sensor.measure.NewMeasure;
  38. import org.sonar.api.batch.sensor.rule.AdHocRule;
  39. import org.sonar.api.batch.sensor.rule.NewAdHocRule;
  40. import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
  41. import org.sonar.api.config.Configuration;
  42. import org.sonar.api.scanner.fs.InputProject;
  43. import org.sonar.api.scanner.sensor.ProjectSensor;
  44. import org.sonar.api.utils.Version;
  45. /**
  46. * See {@link Sensor#execute(SensorContext)}
  47. * In order to write unit tests you can use SensorContextTester
  48. * @since 5.1
  49. */
  50. public interface SensorContext {
  51. /**
  52. * Get settings of the project.
  53. * @since 6.5
  54. */
  55. Configuration config();
  56. /**
  57. * Get filesystem of the project.
  58. */
  59. FileSystem fileSystem();
  60. /**
  61. * Get list of active rules.
  62. */
  63. ActiveRules activeRules();
  64. /**
  65. * @since 5.5
  66. * @deprecated since 7.6 modules are deprecated. Use {@link #project()} instead.
  67. * @throws UnsupportedOperationException for global {@link ProjectSensor}s
  68. */
  69. @Deprecated
  70. InputModule module();
  71. /**
  72. * The current project.
  73. * @since 7.6
  74. */
  75. InputProject project();
  76. /**
  77. * Version of API at runtime, not at compilation time. It's a shortcut on
  78. * {@code runtime().getApiVersion()} since 6.0.
  79. * @since 5.5
  80. * @see #runtime() since version 6.0.
  81. */
  82. Version getSonarQubeVersion();
  83. /**
  84. * Runtime information, mainly:
  85. * <ul>
  86. * <li>to be able to have different behaviours between SonarQube and SonarLint</li>
  87. * <li>to enable new features depending on version of API available at runtime</li>
  88. * </ul>
  89. * @since 6.0
  90. */
  91. SonarRuntime runtime();
  92. /**
  93. * Test if a cancellation of the analysis was requested. Sensors should periodically test this flag
  94. * and gracefully stop if value is {@code true}. For example it could be tested between each processed file.
  95. * @since 6.0
  96. */
  97. boolean isCancelled();
  98. // ----------- MEASURES --------------
  99. /**
  100. * Fluent builder to create a new {@link Measure}. Don't forget to call {@link NewMeasure#save()} once all parameters are provided.
  101. */
  102. <G extends Serializable> NewMeasure<G> newMeasure();
  103. // ----------- ISSUES --------------
  104. /**
  105. * Fluent builder to create a new {@link Issue}. Don't forget to call {@link NewIssue#save()} once all parameters are provided.
  106. */
  107. NewIssue newIssue();
  108. /**
  109. * Fluent builder to create a new {@link ExternalIssue}. Don't forget to call {@link NewExternalIssue#save()} once all parameters are provided.
  110. * @since 7.2
  111. */
  112. NewExternalIssue newExternalIssue();
  113. /**
  114. * Fluent builder to create a new {@link AdHocRule}. Don't forget to call {@link NewAdHocRule#save()} once all parameters are provided.
  115. * @since 7.4
  116. */
  117. NewAdHocRule newAdHocRule();
  118. // ------------ HIGHLIGHTING ------------
  119. /**
  120. * Builder to define highlighting of a file. Don't forget to call {@link NewHighlighting#save()} once all elements are provided.
  121. */
  122. NewHighlighting newHighlighting();
  123. // ------------ SYMBOL TABLE ------------
  124. /**
  125. * Builder to define symbol table of a file. Don't forget to call {@link NewSymbolTable#save()} once all symbols are provided.
  126. * @since 5.6
  127. */
  128. NewSymbolTable newSymbolTable();
  129. // ------------ COVERAGE ------------
  130. /**
  131. * Builder to define coverage in a file.
  132. * Don't forget to call {@link NewCoverage#save()}.
  133. */
  134. NewCoverage newCoverage();
  135. // ------------ CPD ------------
  136. /**
  137. * Builder to define CPD tokens in a file.
  138. * Don't forget to call {@link NewCpdTokens#save()}.
  139. * @since 5.5
  140. */
  141. NewCpdTokens newCpdTokens();
  142. // ------------ ANALYSIS ERROR ------------
  143. /**
  144. * Builder to declare errors that happened while processing a source file.
  145. * Don't forget to call {@link NewAnalysisError#save()}.
  146. * @since 6.0
  147. */
  148. NewAnalysisError newAnalysisError();
  149. /**
  150. * Builder to declare which parts of the code is significant code.
  151. * Ranges that are not reported as significant code will be ignored and won't be considered when calculating which lines were modified.
  152. *
  153. * If the significant code is not reported for a file, it is assumed that the entire file is significant code.
  154. *
  155. * @since 7.2
  156. */
  157. NewSignificantCode newSignificantCode();
  158. /**
  159. * Add a property to the scanner context. This context is available
  160. * in Compute Engine when processing the report.
  161. * <br/>
  162. * The properties starting with {@code "sonar.analysis."} are included to the
  163. * payload of webhooks.
  164. *
  165. * @throws IllegalArgumentException if key or value parameter is null
  166. * @see org.sonar.api.ce.posttask.PostProjectAnalysisTask.ProjectAnalysis#getScannerContext()
  167. * @since 6.1
  168. */
  169. void addContextProperty(String key, String value);
  170. /**
  171. * Indicate that a file should be published in the report sent to SonarQube.
  172. * Files are automatically marked if any data is created for it (issues, highlighting, coverage, etc.).
  173. * @since 6.3
  174. */
  175. void markForPublishing(InputFile inputFile);
  176. }