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

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