aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/SonarRuntime.java
blob: bea756e4bc2bb3d67ef150de09c43df1966c649c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api;

import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.Version;
import org.sonarsource.api.sonarlint.SonarLintSide;

/**
 * Information about runtime environment.
 *
 * <p>
 * A usage for plugins is to benefit from new APIs
 * while keeping backward-compatibility with previous versions of SonarQube
 * or SonarLint.
 * </p>
 *
 * <p>
 * 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.
 * </p>
 * <pre>
 * // 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();
 *     }
 *   }
 * }
 * </pre>
 *
 *
 * <p>
 *   Note that {@link Sensor} extensions can directly get {@link SonarRuntime} through
 * {@link SensorContext#runtime()}, without using constructor injection:
 * </p>
 * <pre>
 * public class MySensor implements Sensor {
 *
 *   public void execute(SensorContext context) {
 *     if (context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(6, 1)) {
 *       context.newMethodIntroducedIn6_0();
 *     }
 *   }
 *
 * }
 * </pre>
 *
 * <p>
 * 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+:
 * <p>
 * <pre>
 * &lt;packaging&gt;sonar-plugin&lt;/packaging&gt;
 *
 * &lt;dependencies&gt;
 *   &lt;dependency&gt;
 *     &lt;groupId&gt;org.sonarsource.sonarqube&lt;/groupId&gt;
 *     &lt;artifactId&gt;sonar-plugin-api&lt;/artifactId&gt;
 *     &lt;version&gt;6.1&lt;/version&gt;
 *     &lt;scope&gt;provided&lt;/scope&gt;
 *   &lt;/dependency&gt;
 * &lt;/dependencies&gt;
 *
 * &lt;build&gt;
 *  &lt;plugins&gt;
 *    &lt;plugin&gt;
 *      &lt;groupId&gt;org.sonarsource.sonar-packaging-maven-plugin&lt;/groupId&gt;
 *      &lt;artifactId&gt;sonar-packaging-maven-plugin&lt;/artifactId&gt;
 *      &lt;version&gt;1.16&lt;/version&gt;
 *      &lt;extensions&gt;true&lt;/extensions&gt;
 *      &lt;configuration&gt;
 *        &lt;!-- Override the default value 6.0 which is guessed from sonar-plugin-api dependency --&gt;
 *        &lt;sonarQubeMinVersion&gt;6.0&lt;/sonarQubeMinVersion&gt;
 *      &lt;/configuration&gt;
 *    &lt;/plugin&gt;
 *  &lt;/plugins&gt;
 * &lt;/build&gt;
 * </pre>
 *
 * <p>
 *   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 {@link SonarQubeVersion}, for example through {@link Plugin.Context#getSonarQubeVersion()} or
 *   {@link SensorContext#getSonarQubeVersion()}.
 * </p>
 *
 * <p>
 * Unit tests of plugin extensions can create instances of {@link SonarRuntime}
 * via {@link org.sonar.api.internal.SonarRuntimeImpl}.
 * </p>
 * @since 6.0
 */
@ScannerSide
@ServerSide
@ComputeEngineSide
@SonarLintSide
public interface SonarRuntime {

  /**
   * 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.
   * <br/>
   * Since 6.3, the returned version includes the build number in the fourth field, for
   * example {@code "6.3.0.12345"}.
  */
  Version getApiVersion();

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

  /**
   * The SonarQube stack being executed at runtime.
   * @throws UnsupportedOperationException if {@link #getProduct()} is not equal to {@link SonarProduct#SONARQUBE}
   */
  SonarQubeSide getSonarQubeSide();

}