]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 Improve FileLineContextFactory to support InputFile
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 28 Jan 2014 19:57:40 +0000 (20:57 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 29 Jan 2014 15:01:07 +0000 (16:01 +0100)
sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java
sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java
sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/Sensor.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/FileLinesContextFactory.java

index 22ca76671f422ade5be26f3b5a40cffd8655c26c..35e841b39d39ddd4e76dd4383179e22a6bffa7e0 100644 (file)
@@ -118,8 +118,8 @@ public class DefaultFileLinesContext implements FileLinesContext {
       if (shouldSave(lines)) {
         String data = KeyValueFormat.format(lines);
         Measure measure = new Measure(metricKey)
-            .setPersistenceMode(PersistenceMode.DATABASE)
-            .setData(data);
+          .setPersistenceMode(PersistenceMode.DATABASE)
+          .setData(data);
         index.addMeasure(resource, measure);
         entry.setValue(ImmutableMap.copyOf(lines));
       }
@@ -149,8 +149,8 @@ public class DefaultFileLinesContext implements FileLinesContext {
   @Override
   public String toString() {
     return Objects.toStringHelper(this)
-        .add("map", map)
-        .toString();
+      .add("map", map)
+      .toString();
   }
 
 }
index 13d53e1ba9a9396fe9655d3fa4a81af993f23382..bc05a5d6004437cbec7dfcf78edd1492d4bfb5a9 100644 (file)
@@ -22,20 +22,43 @@ package org.sonar.batch;
 import org.sonar.api.batch.SonarIndex;
 import org.sonar.api.measures.FileLinesContext;
 import org.sonar.api.measures.FileLinesContextFactory;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Languages;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.scan.filesystem.InputFile;
+import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 
 public class DefaultFileLinesContextFactory implements FileLinesContextFactory {
 
   private final SonarIndex index;
+  private Languages languages;
 
-  public DefaultFileLinesContextFactory(SonarIndex index) {
+  public DefaultFileLinesContextFactory(SonarIndex index, Languages languages) {
     this.index = index;
+    this.languages = languages;
   }
 
+  @Override
   public FileLinesContext createFor(Resource resource) {
     // Reload resource in case it use deprecated key
     resource = index.getResource(resource);
     return new DefaultFileLinesContext(index, resource);
   }
 
+  @Override
+  public FileLinesContext createFor(InputFile inputFile) {
+    // FIXME remove that once DefaultFileLinesContext accept an InputFile
+    String languageKey = inputFile.attribute(InputFile.ATTRIBUTE_LANGUAGE);
+    boolean unitTest = InputFile.TYPE_TEST.equals(inputFile.attribute(InputFile.ATTRIBUTE_TYPE));
+    Resource sonarFile;
+    if (Java.KEY.equals(languageKey)) {
+      sonarFile = JavaFile.create(inputFile.path(), inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH), unitTest);
+    } else {
+      sonarFile = File.create(inputFile.path(), inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH), languages.get(languageKey), unitTest);
+    }
+    return new DefaultFileLinesContext(index, sonarFile);
+  }
+
 }
index 100c8b34273e98407f848040036854896b0db9d3..de12c23e87c454448db7b434f5c51e74ed385e10 100644 (file)
  */
 package org.sonar.batch.index;
 
-import org.sonar.api.scan.filesystem.InputFile;
-
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.scan.filesystem.InputFile;
 
 public interface ResourcePersister {
 
@@ -46,7 +45,7 @@ public interface ResourcePersister {
   Snapshot getSnapshotOrFail(InputFile resource);
 
   /**
-   * The current snapshot which is flagged as "last", different then the current analysis.
+   * The current snapshot which is flagged as "last", different than the current analysis.
    * @param onlyOlder true if the result must be anterior to the snapshot parameter
    */
   Snapshot getLastSnapshot(Snapshot snapshot, boolean onlyOlder);
index 989a4177cdaba6fcaac8d527923b30529a3b64b4..ded6dc9dcde0967d25c0959cec860ea4167d97f0 100644 (file)
@@ -28,24 +28,24 @@ import org.sonar.api.resources.Project;
  * generally used to add measure at the lowest level of the resource tree. A sensor can access and save measures on the whole tree of
  * resources.
  * </p>
- * 
+ *
  * <p>
  * For example the Cobertura Sensor parses Cobertura report and saves the first-level of measures on resources.
  * </p>
- * 
+ *
  * <p>
  * A particular attention should be given to resource exclusion. Sonar already manages exclusions at file level : if you try to save a
  * measure on a resource that is excluded in the settings, then Sonar will not save the measure. When handling a plugin or an external tool,
  * you should make sure that exclusions are passed if you are going to get back consolidated data.
  * </p>
- * 
+ *
  * @since 1.10
  */
 public interface Sensor extends BatchExtension, CheckProject {
 
   /**
    * Sensors that depend upon Squid must declare the following method :
-   * 
+   *
    * <pre>
    * &#064;DependsUpon
    * public String dependsUponSquidAnalysis() {
@@ -57,10 +57,10 @@ public interface Sensor extends BatchExtension, CheckProject {
 
   /**
    * The method that is going to be run when the sensor is called
-   * 
-   * @param project the project the sensor runs on
+   *
+   * @param module the module the sensor runs on
    * @param context the context
    */
-  void analyse(Project project, SensorContext context);
+  void analyse(Project module, SensorContext context);
 
 }
index e86bbc9fe2f997d3d6c7ae9de3a6656b80337fba..d9908d4583b46331251fce4eb132c5871854d5c0 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.api.measures;
 import com.google.common.annotations.Beta;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.scan.filesystem.InputFile;
 
 /**
  * <p>This interface is not intended to be implemented by clients.</p>
@@ -33,4 +34,6 @@ public interface FileLinesContextFactory extends BatchComponent {
 
   FileLinesContext createFor(Resource resource);
 
+  FileLinesContext createFor(InputFile inputFile);
+
 }