Package org.sonar.api

Interface SonarRuntime

  • @ScannerSide
    public interface SonarRuntime
    Information about runtime environment.

    A usage for plugins is to benefit from new APIs while keeping backward-compatibility with previous versions of SonarQube or SonarLint.

    Example: a plugin extension wants to use a new feature of API 6.1 without breaking compatibility with version 6.0 at runtime. This new feature would be enabled only in 6.1 and greater runtimes.

     // Component provided by sonar-plugin-api
     // @since 6.0
     public interface AnApi {
       // implicitly since 6.0
       public void foo();
       // @since 6.1
       public void bar();
     // Plugin extension
     public class MyExtension {
       private final SonarRuntime sonarRuntime;
       private final AnApi api;
       public MyExtension(SonarRuntime sonarRuntime, AnApi api) {
         this.sonarRuntime = sonarRuntime;
         this.api = api;
       public void doSomething() {
         // assume that minimal supported runtime is 6.0;
         if (sonarRuntime.getApiVersion().isGreaterThanOrEqual(Version.create(6, 1))) {

    Note that Sensor extensions can directly get SonarRuntime through SensorContext.runtime(), without using constructor injection:

     public class MySensor implements Sensor {
       public void execute(SensorContext context) {
         if (context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(6, 1)) {

    The minimal supported version of plugin API is verified at runtime. As plugin is built with sonar-plugin-api 6.1, we assume that the plugin requires v6.1 or greater at runtime. For this reason the plugin must override the minimal supported version in the configuration of sonar-packaging-maven-plugin 1.16+:

            <!-- Override the default value 6.0 which is guessed from sonar-plugin-api dependency -->

    As this component was introduced in version 6.0, the pattern described above can't be exactly applied when plugin must support version 5.6 Long Term Support. In this case plugin should use SonarQubeVersion, for example through Plugin.Context.getSonarQubeVersion() or SensorContext.getSonarQubeVersion().

    Unit tests of plugin extensions can add a test dependency to available in sonar-plugin-api-impl and create instances of SonarRuntime via org.sonar.api.internal.SonarRuntimeImpl.

    • Method Detail

      • getApiVersion

        Version getApiVersion()
        Version of API (sonar-plugin-api artifact) at runtime. It can be helpful to call some API classes/methods without checking their availability at runtime by using reflection.
        Since 6.3, the returned version includes the build number in the fourth field, for example "".
      • getProduct

        SonarProduct getProduct()
        The product being executed at runtime. It targets analysers so that they can implement different behaviours in SonarQube/SonarCloud and SonarLint.
      • getEdition

        SonarEdition getEdition()
        The SonarQube/SonarCloud edition being executed at runtime. Note that there is a specific edition for SonarCloud.
        java.lang.UnsupportedOperationException - if getProduct() is not equal to SonarProduct.SONARQUBE