Package org.sonar.api

Interface SonarRuntime


  • @ScannerSide
    @ServerSide
    @ComputeEngineSide
    @SonarLintSide
    @Immutable
    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
         api.foo();
    
         if (sonarRuntime.getApiVersion().isGreaterThanOrEqual(Version.create(6, 1))) {
           api.bar();
         }
       }
     }
     

    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)) {
           context.newMethodIntroducedIn6_0();
         }
       }
    
     }
     

    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+:

     <packaging>sonar-plugin</packaging>
    
     <dependencies>
       <dependency>
         <groupId>org.sonarsource.sonarqube</groupId>
         <artifactId>sonar-plugin-api</artifactId>
         <version>6.1</version>
         <scope>provided</scope>
       </dependency>
     </dependencies>
    
     <build>
      <plugins>
        <plugin>
          <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
          <artifactId>sonar-packaging-maven-plugin</artifactId>
          <version>1.16</version>
          <extensions>true</extensions>
          <configuration>
            <!-- Override the default value 6.0 which is guessed from sonar-plugin-api dependency -->
            <sonarQubeMinVersion>6.0</sonarQubeMinVersion>
          </configuration>
        </plugin>
      </plugins>
     </build>
     

    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.

    Since:
    6.0
    • 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 "6.3.0.12345".
      • 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.
        Throws:
        java.lang.UnsupportedOperationException - if getProduct() is not equal to SonarProduct.SONARQUBE
        Since:
        7.8