From: Julien HENRY Date: Fri, 10 Jun 2016 07:45:15 +0000 (+0200) Subject: SONAR-7751, SONAR-7756 SonarLint API X-Git-Tag: 6.0-RC1~136 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2d20c0f3f27356ad1f9aa430f3e5925c50756cfc;p=sonarqube.git SONAR-7751, SONAR-7756 SonarLint API --- diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java index 8c83a20bd32..138ba56fb5b 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java @@ -25,17 +25,17 @@ import org.sonar.api.utils.Version; import org.sonar.xoo.lang.CpdTokenizerSensor; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.api.SonarQubeVersion.V5_5; +import static org.sonar.api.RuntimeApiVersion.V5_5; public class XooPluginTest { @Test public void provide_extensions_for_5_5() { - Plugin.Context context = new Plugin.Context(V5_5); + Plugin.Context context = new Plugin.Context(V5_5, false); new XooPlugin().define(context); assertThat(context.getExtensions()).hasSize(40).contains(CpdTokenizerSensor.class); - context = new Plugin.Context(Version.parse("5.4")); + context = new Plugin.Context(Version.parse("5.4"), false); new XooPlugin().define(context); assertThat(context.getExtensions()).hasSize(39).doesNotContain(CpdTokenizerSensor.class); } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java index f182004de3e..091c20b42f3 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java @@ -104,7 +104,7 @@ public class OneIssuePerLineSensorTest { SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); - context.setSonarQubeVersion(Version.parse("5.4")); + context.setRuntime(Version.parse("5.4"), false); sensor.execute(context); diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 689e56ee4d7..fe1e41c5c6c 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -23,7 +23,7 @@ import com.google.common.annotations.VisibleForTesting; import java.util.List; import javax.annotation.CheckForNull; import org.sonar.api.config.EmailSettings; -import org.sonar.api.internal.SonarQubeVersionFactory; +import org.sonar.api.internal.RuntimeApiVersionFactory; import org.sonar.api.profiles.AnnotationProfileParser; import org.sonar.api.profiles.XMLProfileParser; import org.sonar.api.profiles.XMLProfileSerializer; @@ -135,7 +135,7 @@ import org.sonarqube.ws.Rules; public class ComputeEngineContainerImpl implements ComputeEngineContainer { private static final Object[] LEVEL_1_COMPONENTS = new Object[] { ComputeEngineSettings.class, - SonarQubeVersionFactory.create(System2.INSTANCE), + RuntimeApiVersionFactory.create(System2.INSTANCE, false), ServerImpl.class, UuidFactoryImpl.INSTANCE, // no EmbeddedDatabaseFactory.class, creating H2 DB if responsibility of WebServer diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java b/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java index dce28460820..7a8b85536ce 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/platform/ComputeEngineExtensionInstaller.java @@ -19,14 +19,14 @@ */ package org.sonar.ce.platform; -import org.sonar.api.SonarQubeVersion; +import org.sonar.api.RuntimeApiVersion; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.core.platform.PluginRepository; import org.sonar.server.plugins.ServerExtensionInstaller; public class ComputeEngineExtensionInstaller extends ServerExtensionInstaller { - public ComputeEngineExtensionInstaller(SonarQubeVersion sonarQubeVersion, PluginRepository pluginRepository) { - super(sonarQubeVersion, pluginRepository, ServerSide.class, ComputeEngineSide.class); + public ComputeEngineExtensionInstaller(RuntimeApiVersion runtimeApiVersion, PluginRepository pluginRepository) { + super(runtimeApiVersion, pluginRepository, ServerSide.class, ComputeEngineSide.class); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java index 94d41fd68d9..861f6375b39 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java @@ -21,7 +21,7 @@ package org.sonar.server.platform.platformlevel; import java.util.Properties; import javax.annotation.Nullable; -import org.sonar.api.internal.SonarQubeVersionFactory; +import org.sonar.api.internal.RuntimeApiVersionFactory; import org.sonar.api.utils.System2; import org.sonar.api.utils.internal.TempFolderCleaner; import org.sonar.ce.property.CePropertyDefinitions; @@ -68,7 +68,7 @@ public class PlatformLevel1 extends PlatformLevel { add(platform, properties); addExtraRootComponents(); add( - SonarQubeVersionFactory.create(System2.INSTANCE), + RuntimeApiVersionFactory.create(System2.INSTANCE, false), ProcessCommandWrapperImpl.class, RestartFlagHolderImpl.class, WebServerSettings.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java index fef6ed8a484..e481935ee65 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java @@ -23,10 +23,9 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; import java.lang.annotation.Annotation; import java.util.Map; -import org.sonar.api.Extension; import org.sonar.api.ExtensionProvider; import org.sonar.api.Plugin; -import org.sonar.api.SonarQubeVersion; +import org.sonar.api.RuntimeApiVersion; import org.sonar.api.utils.AnnotationUtils; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PluginInfo; @@ -39,14 +38,14 @@ import static java.util.Objects.requireNonNull; */ public abstract class ServerExtensionInstaller { - private final SonarQubeVersion sonarQubeVersion; + private final RuntimeApiVersion runtimeApiVersion; private final PluginRepository pluginRepository; private final Class[] supportedAnnotationTypes; - protected ServerExtensionInstaller(SonarQubeVersion sonarQubeVersion, PluginRepository pluginRepository, + protected ServerExtensionInstaller(RuntimeApiVersion runtimeApiVersion, PluginRepository pluginRepository, Class... supportedAnnotationTypes) { requireNonNull(supportedAnnotationTypes, "At least one supported annotation type must be specified"); - this.sonarQubeVersion = sonarQubeVersion; + this.runtimeApiVersion = runtimeApiVersion; this.pluginRepository = pluginRepository; this.supportedAnnotationTypes = supportedAnnotationTypes; } @@ -60,7 +59,7 @@ public abstract class ServerExtensionInstaller { Plugin plugin = pluginRepository.getPluginInstance(pluginKey); container.addExtension(pluginInfo, plugin); - Plugin.Context context = new Plugin.Context(sonarQubeVersion.get()); + Plugin.Context context = new Plugin.Context(runtimeApiVersion.get(), runtimeApiVersion.isSonarlintRuntime()); plugin.define(context); for (Object extension : context.getExtensions()) { if (installExtension(container, pluginInfo, extension, true) != null) { @@ -119,7 +118,7 @@ public abstract class ServerExtensionInstaller { return isType(extension, ExtensionProvider.class) || extension instanceof ExtensionProvider; } - static boolean isType(Object extension, Class extensionClass) { + static boolean isType(Object extension, Class extensionClass) { Class clazz = extension instanceof Class ? (Class) extension : extension.getClass(); return extensionClass.isAssignableFrom(clazz); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ExtensionProvider.java b/sonar-plugin-api/src/main/java/org/sonar/api/ExtensionProvider.java index 5602be54411..b0e19e236b4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ExtensionProvider.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ExtensionProvider.java @@ -19,16 +19,20 @@ */ package org.sonar.api; +import org.sonar.api.batch.BatchSide; +import org.sonar.api.ce.ComputeEngineSide; +import org.sonar.api.server.ServerSide; + /** - * Factory of extensions. It allows to dynamically create extensions depending upon runtime context. A use-case is + * Factory of extensions. It allows to dynamically create extensions depending upon runtime context. One use-case is * to create one rule repository by language. * *

Notes : *

* @@ -36,7 +40,8 @@ package org.sonar.api; *

Example: *

  * {@code
- * public class RuleRepositoryProvider extends ExtensionProvider implements ServerExtension {
+ * {@literal @}ServerSide
+ * public class RuleRepositoryProvider extends ExtensionProvider {
  *   private Language[] languages;
  *
  *   public RuleRepositoryProvider(Language[] languages) {
@@ -59,7 +64,8 @@ package org.sonar.api;
  * @deprecated since 6.0 should no more be used
  */
 @Deprecated
-public abstract class ExtensionProvider implements Extension {
+@ExtensionPoint
+public abstract class ExtensionProvider {
 
   public abstract Object provide();
 }
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java b/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java
index 21832e1754e..aa1a6f9511a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/Plugin.java
@@ -85,18 +85,29 @@ import static java.util.Objects.requireNonNull;
 public interface Plugin {
 
   class Context {
-    private final Version version;
+    private final Version runtimeApiVersion;
     private final List extensions = new ArrayList();
+    private final boolean sonarlintRuntime;
 
-    public Context(Version version) {
-      this.version = version;
+    public Context(Version runtimeApiVersion, boolean sonarlintRuntime) {
+      this.runtimeApiVersion = runtimeApiVersion;
+      this.sonarlintRuntime = sonarlintRuntime;
     }
 
     /**
-     * Runtime version of SonarQube
+     * @deprecated since 6.0
      */
+    @Deprecated
     public Version getSonarQubeVersion() {
-      return version;
+      return runtimeApiVersion;
+    }
+
+    /**
+     * Runtime API version. Can be use to conditionnaly add some extensions.
+     * @since 6.0
+     */
+    public Version getRuntimeApiVersion() {
+      return runtimeApiVersion;
     }
 
     /**
@@ -141,6 +152,14 @@ public interface Plugin {
     public List getExtensions() {
       return extensions;
     }
+
+    /**
+     * Test if plugin is currently executed in SonarLint. Can be use to conditionnaly add some extensions.
+     * @since 6.0
+     */
+    public boolean isSonarlintRuntime() {
+      return sonarlintRuntime;
+    }
   }
 
   /**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java b/sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java
new file mode 100644
index 00000000000..e221f6f9f8e
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/RuntimeApiVersion.java
@@ -0,0 +1,161 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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 javax.annotation.concurrent.Immutable;
+import org.sonar.api.batch.BatchSide;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.Version;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Version of SonarQube at runtime. This component can be injected as a dependency
+ * of plugin extensions. The main usage for a plugin is to benefit from new APIs
+ * while keeping backward-compatibility with previous versions of SonarQube.
+ * 

+ * + * Example 1: a {@link Sensor} wants to use an API introduced in version 5.5 and still requires to support older versions + * at runtime. + *
+ * public class MySensor implements Sensor {
+ *
+ *   public void execute(SensorContext context) {
+ *     if (context.getRuntimeApiVersion().isGreaterThanOrEqual(RuntimeApiVersion.V5_5)) {
+ *       context.newMethodIntroducedIn5_5();
+ *     }
+ *   }
+ * }
+ * 
+ * + * Example 2: a plugin needs to use an API introduced in version 5.6 ({@code AnApi} in the following + * snippet) and still requires to support version 5.5 at runtime. + *
+ *
+ * // Component provided by sonar-plugin-api
+ * // @since 5.5
+ * public interface AnApi {
+ *   // implicitly since 5.5
+ *   public void foo();
+ *
+ *   // @since 5.6
+ *   public void bar();
+ * }
+ * 
+ * // Component provided by plugin
+ * public class MyExtension {
+ *   private final RuntimeApiVersion runtimeApiVersion;
+ *   private final AnApi api;
+ *
+ *   public MyExtension(RuntimeApiVersion runtimeApiVersion, AnApi api) {
+ *     this.runtimeApiVersion = runtimeApiVersion;
+ *     this.api = api;
+ *   }
+ *
+ *   public void doSomething() {
+ *     // assume that runtime is 5.5+
+ *     api.foo();
+ *
+ *     if (runtimeApiVersion.isGreaterThanOrEqual(SonarQubeVersion.V5_6)) {
+ *       api.bar();
+ *     }
+ *   }
+ * }
+ * 
+ *

+ * The minimal supported version of plugin API is verified at runtime. As plugin is built + * with sonar-plugin-api 5.6, we assume that the plugin requires v5.6 or greater at runtime. + * For this reason the plugin must default which is 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>5.6</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 5.6 which is guessed from sonar-plugin-api dependency -->
+ *        <sonarQubeMinVersion>5.5</sonarQubeMinVersion>
+ *      </configuration>
+ *    </plugin>
+ *  </plugins>
+ * </build>
+ * 
+ * + * + * @since 6.0 + */ +@BatchSide +@ServerSide +@ComputeEngineSide +@Immutable +public class RuntimeApiVersion { + + /** + * Constant for version 5.5 + */ + public static final Version V5_5 = Version.create(5, 5); + + /** + * Constant for version 5.6 + */ + public static final Version V5_6 = Version.create(5, 6); + + private final Version version; + private final boolean sonarlint; + + public RuntimeApiVersion(Version version, boolean sonarlint) { + requireNonNull(version); + this.version = version; + this.sonarlint = sonarlint; + } + + public Version get() { + return this.version; + } + + public boolean isGreaterThanOrEqual(Version than) { + return this.version.isGreaterThanOrEqual(than); + } + + /** + * @since 6.0 Test if current runtime is SonarLint. Can be used to implement a different behavior. + */ + public boolean isSonarlintRuntime() { + return sonarlint; + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java b/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java index cbd08aa0090..6b8a1e5cdbd 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/SonarQubeVersion.java @@ -26,8 +26,6 @@ import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.Version; -import static java.util.Objects.requireNonNull; - /** * Version of SonarQube at runtime. This component can be injected as a dependency * of plugin extensions. The main usage for a plugin is to benefit from new APIs @@ -117,36 +115,17 @@ import static java.util.Objects.requireNonNull; * * * @since 5.5 + * @deprecated since 6.0 replaced by {@link RuntimeApiVersion} */ @ScannerSide @ServerSide @ComputeEngineSide @Immutable -public class SonarQubeVersion { - - /** - * Constant for version 5.5 - */ - public static final Version V5_5 = Version.create(5, 5); - - /** - * Constant for version 5.6 - */ - public static final Version V5_6 = Version.create(5, 6); - - private final Version version; - - public SonarQubeVersion(Version version) { - requireNonNull(version); - this.version = version; - } - - public Version get() { - return this.version; - } +@Deprecated +public class SonarQubeVersion extends RuntimeApiVersion { - public boolean isGreaterThanOrEqual(Version than) { - return this.version.isGreaterThanOrEqual(than); + public SonarQubeVersion(Version version, boolean sonarlint) { + super(version, sonarlint); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java index 2eae58b055a..6a776b5974c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java @@ -22,6 +22,7 @@ package org.sonar.api.batch.sensor; import org.sonar.api.ExtensionPoint; import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.sensor.internal.SensorContextTester; +import org.sonarsource.api.sonarlint.SonarLintSide; /** *

@@ -34,6 +35,7 @@ import org.sonar.api.batch.sensor.internal.SensorContextTester; * @since 5.1 */ @ScannerSide +@SonarLintSide @ExtensionPoint public interface Sensor { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index bb1aa0fd922..4970132e71a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -64,9 +64,22 @@ public interface SensorContext { /** * @since 5.5 + * @deprecated since 6.0 replaced by {@link #getRuntimeApiVersion()} */ + @Deprecated Version getSonarQubeVersion(); + /** + * @since 6.0 + */ + Version getRuntimeApiVersion(); + + /** + * Test if plugin is currently executed in SonarLint. This can allow to implement a different behavior. + * @since 6.0 + */ + boolean isSonarLintRuntime(); + // ----------- MEASURES -------------- /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 12cd04842db..9c3c8476028 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -58,7 +58,7 @@ import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.batch.sensor.symbol.NewSymbolTable; import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; import org.sonar.api.config.Settings; -import org.sonar.api.internal.SonarQubeVersionFactory; +import org.sonar.api.internal.RuntimeApiVersionFactory; import org.sonar.api.measures.Metric; import org.sonar.api.utils.System2; import org.sonar.api.utils.Version; @@ -95,7 +95,7 @@ public class SensorContextTester implements SensorContext { this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.module = new DefaultInputModule("projectKey"); - this.sqVersion = SonarQubeVersionFactory.create(System2.INSTANCE); + this.sqVersion = RuntimeApiVersionFactory.create(System2.INSTANCE, false); } public static SensorContextTester create(File moduleBaseDir) { @@ -145,8 +145,18 @@ public class SensorContextTester implements SensorContext { return sqVersion.get(); } - public SensorContextTester setSonarQubeVersion(Version version) { - this.sqVersion = new SonarQubeVersion(version); + @Override + public Version getRuntimeApiVersion() { + return sqVersion.get(); + } + + @Override + public boolean isSonarLintRuntime() { + return sqVersion.isSonarlintRuntime(); + } + + public SensorContextTester setRuntime(Version version, boolean isSonarLint) { + this.sqVersion = new SonarQubeVersion(version, isSonarLint); return this; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java new file mode 100644 index 00000000000..fd098bd4a64 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/internal/RuntimeApiVersionFactory.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.internal; + +import com.google.common.io.Resources; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import org.sonar.api.SonarQubeVersion; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.Version; + +/** + * For internal use only. + */ +public class RuntimeApiVersionFactory { + + private static final String FILE_PATH = "/sq-version.txt"; + + private RuntimeApiVersionFactory() { + // prevents instantiation + } + + public static SonarQubeVersion create(System2 system, boolean isSonarLint) { + try { + URL url = system.getResource(FILE_PATH); + String versionInFile = Resources.toString(url, StandardCharsets.UTF_8); + return new SonarQubeVersion(Version.parse(versionInFile), isSonarLint); + } catch (IOException e) { + throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e); + } + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarQubeVersionFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarQubeVersionFactory.java deleted file mode 100644 index db85c3feaf1..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/internal/SonarQubeVersionFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact 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.internal; - -import com.google.common.io.Resources; -import java.io.IOException; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import org.sonar.api.SonarQubeVersion; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.Version; - -/** - * For internal use only. - */ -public class SonarQubeVersionFactory { - - private static final String FILE_PATH = "/sq-version.txt"; - - private SonarQubeVersionFactory() { - // prevents instantiation - } - - public static SonarQubeVersion create(System2 system) { - try { - URL url = system.getResource(FILE_PATH); - String versionInFile = Resources.toString(url, StandardCharsets.UTF_8); - return new SonarQubeVersion(Version.parse(versionInFile)); - } catch (IOException e) { - throw new IllegalStateException("Can not load " + FILE_PATH + " from classpath", e); - } - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java index 4143453f79e..cb46f76912a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java @@ -24,6 +24,7 @@ import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; +import org.sonarsource.api.sonarlint.SonarLintSide; /** * The extension point to define a new language @@ -35,6 +36,7 @@ import org.sonar.api.server.ServerSide; @ScannerSide @InstantiationStrategy(InstantiationStrategy.PER_BATCH) @ServerSide +@SonarLintSide @ComputeEngineSide @ExtensionPoint public interface Language { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java index a09bd9df96b..722b7ed6233 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java @@ -40,13 +40,14 @@ import javax.annotation.concurrent.Immutable; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.ExtensionPoint; +import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; import org.sonar.api.rules.RuleType; -import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.utils.log.Loggers; +import org.sonarsource.api.sonarlint.SonarLintSide; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; @@ -149,6 +150,7 @@ import static org.apache.commons.lang.StringUtils.trimToNull; */ @ServerSide @ComputeEngineSide +@SonarLintSide @ExtensionPoint public interface RulesDefinition { @@ -672,6 +674,7 @@ public interface RulesDefinition { private final Set tags = Sets.newTreeSet(); private final Map paramsByKey = Maps.newHashMap(); private final DebtRemediationFunctions functions; + private boolean activatedByDefault; private NewRule(String repoKey, String key) { this.repoKey = repoKey; @@ -696,6 +699,15 @@ public interface RulesDefinition { return this; } + /** + * Should this rule be enabled by default. For example in SonarLint standalone. + * @since 6.0 + */ + public NewRule setActivatedByDefault(boolean activatedByDefault) { + this.activatedByDefault = activatedByDefault; + return this; + } + public NewRule setSeverity(String s) { checkArgument(Severity.ALL.contains(s), "Severity of rule %s is not correct: %s", this, s); this.severity = s; @@ -915,6 +927,7 @@ public interface RulesDefinition { private final Set tags; private final Map params; private final RuleStatus status; + private final boolean activatedByDefault; private Rule(Repository repository, NewRule newRule) { this.repository = repository; @@ -936,6 +949,7 @@ public interface RulesDefinition { paramsBuilder.put(newParam.key, new Param(newParam)); } this.params = paramsBuilder.build(); + this.activatedByDefault = newRule.activatedByDefault; } public Repository repository() { @@ -976,6 +990,14 @@ public interface RulesDefinition { return template; } + /** + * Should this rule be enabled by default. For example in SonarLint standalone. + * @since 6.0 + */ + public boolean activatedByDefault() { + return activatedByDefault; + } + public RuleStatus status() { return status; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoader.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoader.java index 9be365feb29..d1d1e7399db 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionAnnotationLoader.java @@ -54,8 +54,7 @@ public class RulesDefinitionAnnotationLoader { .put(Boolean.class, RuleParamType.BOOLEAN) .put(boolean.class, RuleParamType.BOOLEAN) .build(), - RuleParamType.STRING - ); + RuleParamType.STRING); public void load(RulesDefinition.NewExtendedRepository repo, Class... annotatedClasses) { for (Class annotatedClass : annotatedClasses) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionI18nLoader.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionI18nLoader.java index 8876b6b4cc8..0094564287b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionI18nLoader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionI18nLoader.java @@ -21,8 +21,6 @@ package org.sonar.api.server.rule; import org.apache.commons.lang.StringUtils; import org.sonar.api.i18n.RuleI18n; -import org.sonar.api.ce.ComputeEngineSide; -import org.sonar.api.server.ServerSide; /** * Loads the English bundles of rules (name, description and parameters) that are @@ -36,8 +34,6 @@ import org.sonar.api.server.ServerSide; * @see org.sonar.api.server.rule.RulesDefinition for an example * @since 4.3 */ -@ServerSide -@ComputeEngineSide public class RulesDefinitionI18nLoader { private final RuleI18n i18n; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java index 71020e0b28d..0f21dd71306 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java @@ -32,13 +32,14 @@ import javax.xml.stream.XMLStreamException; import org.codehaus.staxmate.SMInputFactory; import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; +import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; import org.sonar.api.rules.RuleType; -import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.check.Cardinality; +import org.sonarsource.api.sonarlint.SonarLintSide; import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.equalsIgnoreCase; @@ -180,6 +181,7 @@ import static org.apache.commons.lang.StringUtils.trim; */ @ServerSide @ComputeEngineSide +@SonarLintSide public class RulesDefinitionXmlLoader { private enum DescriptionFormat { diff --git a/sonar-plugin-api/src/main/java/org/sonarsource/api/sonarlint/SonarLintSide.java b/sonar-plugin-api/src/main/java/org/sonarsource/api/sonarlint/SonarLintSide.java new file mode 100644 index 00000000000..3be3aa27477 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonarsource/api/sonarlint/SonarLintSide.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.sonarsource.api.sonarlint; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marker annotation for all the components available in container of sonarlint. Note that + * injection of dependencies by constructor is used : + *

+ *   {@literal @}SonarLintSide
+ *   public class Foo {
+ *
+ *   }
+ *   {@literal @}SonarLintSide
+ *   public class Bar {
+ *     private final Foo foo;
+ *     public Bar(Foo f) {
+ *       this.foo = f;
+ *     }
+ *   }
+ *
+ * 
+ * + * @since 6.0 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface SonarLintSide { +} diff --git a/sonar-plugin-api/src/main/java/org/sonarsource/api/sonarlint/package-info.java b/sonar-plugin-api/src/main/java/org/sonarsource/api/sonarlint/package-info.java new file mode 100644 index 00000000000..2806d2a47da --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonarsource/api/sonarlint/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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. + */ +@ParametersAreNonnullByDefault +package org.sonarsource.api.sonarlint; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java index 6a2602a5450..853a48bd59c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/PluginTest.java @@ -23,13 +23,13 @@ import java.util.Arrays; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.api.SonarQubeVersion.V5_5; +import static org.sonar.api.RuntimeApiVersion.V5_5; public class PluginTest { @Test public void test_context() { - Plugin.Context context = new Plugin.Context(V5_5); + Plugin.Context context = new Plugin.Context(V5_5, true); assertThat(context.getSonarQubeVersion()).isEqualTo(V5_5); assertThat(context.getExtensions()).isEmpty(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java new file mode 100644 index 00000000000..e7fb658e2eb --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/RuntimeApiVersionTest.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.Version; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RuntimeApiVersionTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void isGte() { + Version version = Version.parse("1.2.3"); + RuntimeApiVersion apiVersion = new RuntimeApiVersion(version, false); + assertThat(apiVersion.get()).isEqualTo(version); + assertThat(apiVersion.isSonarlintRuntime()).isFalse(); + assertThat(apiVersion.isGreaterThanOrEqual(version)).isTrue(); + assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue(); + assertThat(apiVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse(); + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java index 50bc4b1c2ec..e5ef829db69 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/SonarQubeVersionTest.java @@ -34,8 +34,9 @@ public class SonarQubeVersionTest { @Test public void isGte() { Version version = Version.parse("1.2.3"); - SonarQubeVersion qubeVersion = new SonarQubeVersion(version); + SonarQubeVersion qubeVersion = new SonarQubeVersion(version, true); assertThat(qubeVersion.get()).isEqualTo(version); + assertThat(qubeVersion.isSonarlintRuntime()).isTrue(); assertThat(qubeVersion.isGreaterThanOrEqual(version)).isTrue(); assertThat(qubeVersion.isGreaterThanOrEqual(Version.parse("1.1"))).isTrue(); assertThat(qubeVersion.isGreaterThanOrEqual(Version.parse("1.3"))).isFalse(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java new file mode 100644 index 00000000000..69b3cca8c54 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/internal/RuntimeApiVersionFactoryTest.java @@ -0,0 +1,57 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.internal; + +import java.io.File; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.SonarQubeVersion; +import org.sonar.api.utils.System2; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class RuntimeApiVersionFactoryTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void create() { + SonarQubeVersion version = RuntimeApiVersionFactory.create(System2.INSTANCE, true); + assertThat(version).isNotNull(); + assertThat(version.get().major()).isGreaterThanOrEqualTo(5); + assertThat(version.isSonarlintRuntime()).isTrue(); + } + + @Test + public void throw_ISE_if_fail_to_load_version() throws Exception { + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can not load /sq-version.txt from classpath"); + + System2 system = spy(System2.class); + when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); + RuntimeApiVersionFactory.create(system, false); + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarQubeVersionFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarQubeVersionFactoryTest.java deleted file mode 100644 index d37f7693d10..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/internal/SonarQubeVersionFactoryTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact 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.internal; - -import java.io.File; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.SonarQubeVersion; -import org.sonar.api.utils.System2; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -public class SonarQubeVersionFactoryTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void create() { - SonarQubeVersion version = SonarQubeVersionFactory.create(System2.INSTANCE); - assertThat(version).isNotNull(); - assertThat(version.get().major()).isGreaterThanOrEqualTo(5); - } - - @Test - public void throw_ISE_if_fail_to_load_version() throws Exception { - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Can not load /sq-version.txt from classpath"); - - System2 system = spy(System2.class); - when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); - SonarQubeVersionFactory.create(system); - } - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java index 7fcece413b0..f3d60147662 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java @@ -23,7 +23,7 @@ import java.util.List; import javax.annotation.Nullable; import org.sonar.api.ExtensionProvider; import org.sonar.api.Plugin; -import org.sonar.api.SonarQubeVersion; +import org.sonar.api.RuntimeApiVersion; import org.sonar.api.batch.AnalysisMode; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PluginInfo; @@ -31,12 +31,12 @@ import org.sonar.core.platform.PluginRepository; public class ExtensionInstaller { - private final SonarQubeVersion sonarQubeVersion; + private final RuntimeApiVersion runtimeApiVersionVersion; private final PluginRepository pluginRepository; private final AnalysisMode analysisMode; - public ExtensionInstaller(SonarQubeVersion sonarQubeVersion, PluginRepository pluginRepository, AnalysisMode analysisMode) { - this.sonarQubeVersion = sonarQubeVersion; + public ExtensionInstaller(RuntimeApiVersion sonarQubeVersion, PluginRepository pluginRepository, AnalysisMode analysisMode) { + this.runtimeApiVersionVersion = sonarQubeVersion; this.pluginRepository = pluginRepository; this.analysisMode = analysisMode; } @@ -51,7 +51,7 @@ public class ExtensionInstaller { // plugin extensions for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { Plugin plugin = pluginRepository.getPluginInstance(pluginInfo.getKey()); - Plugin.Context context = new Plugin.Context(sonarQubeVersion.get()); + Plugin.Context context = new Plugin.Context(runtimeApiVersionVersion.get(), runtimeApiVersionVersion.isSonarlintRuntime()); plugin.define(context); for (Object extension : context.getExtensions()) { doInstall(container, matcher, pluginInfo, extension); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java index 47a9422efd8..fcd089cee54 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java @@ -23,7 +23,7 @@ import java.util.List; import java.util.Map; import org.sonar.api.Plugin; -import org.sonar.api.internal.SonarQubeVersionFactory; +import org.sonar.api.internal.RuntimeApiVersionFactory; import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.log.Logger; @@ -76,7 +76,7 @@ public class GlobalContainer extends ComponentContainer { BatchPluginPredicate.class, ExtensionInstaller.class, - SonarQubeVersionFactory.create(System2.INSTANCE), + RuntimeApiVersionFactory.create(System2.INSTANCE, false), CachesManager.class, GlobalSettings.class, new BatchWsClientProvider(), diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java index 422cedf7205..876e3062d94 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java @@ -21,7 +21,7 @@ package org.sonar.scanner.deprecated; import java.io.Serializable; import java.util.Collection; -import org.sonar.api.SonarQubeVersion; +import org.sonar.api.RuntimeApiVersion; import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.FileSystem; @@ -51,7 +51,7 @@ public class DeprecatedSensorContext extends DefaultSensorContext implements Sen private final CoverageExclusions coverageFilter; public DeprecatedSensorContext(InputModule module, DefaultIndex index, Project project, Settings settings, FileSystem fs, ActiveRules activeRules, - AnalysisMode analysisMode, CoverageExclusions coverageFilter, SensorStorage sensorStorage, SonarQubeVersion sqVersion) { + AnalysisMode analysisMode, CoverageExclusions coverageFilter, SensorStorage sensorStorage, RuntimeApiVersion sqVersion) { super(module, settings, fs, activeRules, analysisMode, sensorStorage, sqVersion); this.index = index; this.project = project; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java index 801224b4450..55ac8d2161b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java @@ -20,7 +20,7 @@ package org.sonar.scanner.sensor; import java.io.Serializable; -import org.sonar.api.SonarQubeVersion; +import org.sonar.api.RuntimeApiVersion; import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputModule; @@ -57,10 +57,10 @@ public class DefaultSensorContext implements SensorContext { private final SensorStorage sensorStorage; private final AnalysisMode analysisMode; private final InputModule module; - private final SonarQubeVersion sqVersion; + private final RuntimeApiVersion sqVersion; public DefaultSensorContext(InputModule module, Settings settings, FileSystem fs, ActiveRules activeRules, AnalysisMode analysisMode, SensorStorage sensorStorage, - SonarQubeVersion sqVersion) { + RuntimeApiVersion sqVersion) { this.module = module; this.settings = settings; this.fs = fs; @@ -95,6 +95,16 @@ public class DefaultSensorContext implements SensorContext { return sqVersion.get(); } + @Override + public Version getRuntimeApiVersion() { + return sqVersion.get(); + } + + @Override + public boolean isSonarLintRuntime() { + return sqVersion.isSonarlintRuntime(); + } + @Override public NewMeasure newMeasure() { return new DefaultMeasure<>(sensorStorage); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java index 0f28d6e42f8..caf7dd58f7a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java @@ -67,7 +67,7 @@ public class DefaultSensorContextTest { settings = new Settings(); sensorStorage = mock(SensorStorage.class); analysisMode = mock(AnalysisMode.class); - sqVersion = new SonarQubeVersion(Version.parse("5.5")); + sqVersion = new SonarQubeVersion(Version.parse("5.5"), false); adaptor = new DefaultSensorContext(mock(InputModule.class), settings, fs, activeRules, analysisMode, sensorStorage, sqVersion); } @@ -77,6 +77,7 @@ public class DefaultSensorContextTest { assertThat(adaptor.fileSystem()).isEqualTo(fs); assertThat(adaptor.settings()).isEqualTo(settings); assertThat(adaptor.getSonarQubeVersion()).isEqualTo(Version.parse("5.5")); + assertThat(adaptor.isSonarLintRuntime()).isFalse(); assertThat(adaptor.newIssue()).isNotNull(); assertThat(adaptor.newMeasure()).isNotNull();