您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SonarRuntime.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.api;
  21. import javax.annotation.concurrent.Immutable;
  22. import org.sonar.api.batch.sensor.Sensor;
  23. import org.sonar.api.batch.sensor.SensorContext;
  24. import org.sonar.api.ce.ComputeEngineSide;
  25. import org.sonar.api.scanner.ScannerSide;
  26. import org.sonar.api.server.ServerSide;
  27. import org.sonar.api.utils.Version;
  28. import org.sonarsource.api.sonarlint.SonarLintSide;
  29. /**
  30. * Information about runtime environment.
  31. *
  32. * <p>
  33. * A usage for plugins is to benefit from new APIs
  34. * while keeping backward-compatibility with previous versions of SonarQube
  35. * or SonarLint.
  36. * </p>
  37. *
  38. * <p>
  39. * Example: a plugin extension wants to use a new feature of API 6.1 without
  40. * breaking compatibility with version 6.0 at runtime. This new feature
  41. * would be enabled only in 6.1 and greater runtimes.
  42. * </p>
  43. * <pre>
  44. * // Component provided by sonar-plugin-api
  45. * // @since 6.0
  46. * public interface AnApi {
  47. * // implicitly since 6.0
  48. * public void foo();
  49. *
  50. * // @since 6.1
  51. * public void bar();
  52. * }
  53. *
  54. * // Plugin extension
  55. * public class MyExtension {
  56. * private final SonarRuntime sonarRuntime;
  57. * private final AnApi api;
  58. *
  59. * public MyExtension(SonarRuntime sonarRuntime, AnApi api) {
  60. * this.sonarRuntime = sonarRuntime;
  61. * this.api = api;
  62. * }
  63. *
  64. * public void doSomething() {
  65. * // assume that minimal supported runtime is 6.0
  66. * api.foo();
  67. *
  68. * if (sonarRuntime.getApiVersion().isGreaterThanOrEqual(Version.create(6, 1))) {
  69. * api.bar();
  70. * }
  71. * }
  72. * }
  73. * </pre>
  74. *
  75. *
  76. * <p>
  77. * Note that {@link Sensor} extensions can directly get {@link SonarRuntime} through
  78. * {@link SensorContext#runtime()}, without using constructor injection:
  79. * </p>
  80. * <pre>
  81. * public class MySensor implements Sensor {
  82. *
  83. * public void execute(SensorContext context) {
  84. * if (context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(6, 1)) {
  85. * context.newMethodIntroducedIn6_0();
  86. * }
  87. * }
  88. *
  89. * }
  90. * </pre>
  91. *
  92. * <p>
  93. * The minimal supported version of plugin API is verified at runtime. As plugin is built
  94. * with sonar-plugin-api 6.1, we assume that the plugin requires v6.1 or greater at runtime.
  95. * For this reason the plugin must override the minimal supported version
  96. * in the configuration of sonar-packaging-maven-plugin 1.16+:
  97. * <p>
  98. * <pre>
  99. * &lt;packaging&gt;sonar-plugin&lt;/packaging&gt;
  100. *
  101. * &lt;dependencies&gt;
  102. * &lt;dependency&gt;
  103. * &lt;groupId&gt;org.sonarsource.sonarqube&lt;/groupId&gt;
  104. * &lt;artifactId&gt;sonar-plugin-api&lt;/artifactId&gt;
  105. * &lt;version&gt;6.1&lt;/version&gt;
  106. * &lt;scope&gt;provided&lt;/scope&gt;
  107. * &lt;/dependency&gt;
  108. * &lt;/dependencies&gt;
  109. *
  110. * &lt;build&gt;
  111. * &lt;plugins&gt;
  112. * &lt;plugin&gt;
  113. * &lt;groupId&gt;org.sonarsource.sonar-packaging-maven-plugin&lt;/groupId&gt;
  114. * &lt;artifactId&gt;sonar-packaging-maven-plugin&lt;/artifactId&gt;
  115. * &lt;version&gt;1.16&lt;/version&gt;
  116. * &lt;extensions&gt;true&lt;/extensions&gt;
  117. * &lt;configuration&gt;
  118. * &lt;!-- Override the default value 6.0 which is guessed from sonar-plugin-api dependency --&gt;
  119. * &lt;sonarQubeMinVersion&gt;6.0&lt;/sonarQubeMinVersion&gt;
  120. * &lt;/configuration&gt;
  121. * &lt;/plugin&gt;
  122. * &lt;/plugins&gt;
  123. * &lt;/build&gt;
  124. * </pre>
  125. *
  126. * <p>
  127. * As this component was introduced in version 6.0, the pattern described above can't be
  128. * exactly applied when plugin must support version 5.6 Long Term Support. In this case plugin
  129. * should use {@link SonarQubeVersion}, for example through {@link Plugin.Context#getSonarQubeVersion()} or
  130. * {@link SensorContext#getSonarQubeVersion()}.
  131. * </p>
  132. *
  133. * <p>
  134. * Unit tests of plugin extensions can create instances of {@link SonarRuntime}
  135. * via {@link org.sonar.api.internal.SonarRuntimeImpl}.
  136. * </p>
  137. *
  138. * @since 6.0
  139. */
  140. @ScannerSide
  141. @ServerSide
  142. @ComputeEngineSide
  143. @SonarLintSide
  144. @Immutable
  145. public interface SonarRuntime {
  146. /**
  147. * Version of API (sonar-plugin-api artifact) at runtime.
  148. * It can be helpful to call some API classes/methods without checking their availability at
  149. * runtime by using reflection.
  150. * <br/>
  151. * Since 6.3, the returned version includes the build number in the fourth field, for
  152. * example {@code "6.3.0.12345"}.
  153. */
  154. Version getApiVersion();
  155. /**
  156. * The product being executed at runtime. It targets analysers so that they can implement
  157. * different behaviours in SonarQube/SonarCloud and SonarLint.
  158. */
  159. SonarProduct getProduct();
  160. /**
  161. * The SonarQube/SonarCloud stack being executed at runtime.
  162. *
  163. * @throws UnsupportedOperationException if {@link #getProduct()} is not equal to {@link SonarProduct#SONARQUBE}
  164. */
  165. SonarQubeSide getSonarQubeSide();
  166. /**
  167. * The SonarQube/SonarCloud edition being executed at runtime.
  168. * Note that there is a specific edition for SonarCloud.
  169. *
  170. * @throws UnsupportedOperationException if {@link #getProduct()} is not equal to {@link SonarProduct#SONARQUBE}
  171. * @since 7.8
  172. */
  173. SonarEdition getEdition();
  174. }