]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2155 Resource filters are ignored in RFC/LCOM4 distributions of Java projects
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 4 Feb 2011 14:05:56 +0000 (15:05 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 4 Feb 2011 14:06:21 +0000 (15:06 +0100)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererDistributionBridge.java [deleted file]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridge.java [deleted file]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/CopyBasicMeasuresBridge.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridge.java [deleted file]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ChidamberKemererDistributionBuilder.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java

index b3236c0975725feafa69e6419e7a2d3216aeac14..7196337aca9f35c72275e0bf07732d5b741a7b74 100644 (file)
@@ -23,10 +23,7 @@ import org.sonar.api.CoreProperties;
 import org.sonar.api.Plugin;
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
-import org.sonar.plugins.squid.decorators.ClassComplexityDistributionBuilder;
-import org.sonar.plugins.squid.decorators.ClassesDecorator;
-import org.sonar.plugins.squid.decorators.FunctionComplexityDistributionBuilder;
-import org.sonar.plugins.squid.decorators.FunctionsDecorator;
+import org.sonar.plugins.squid.decorators.*;
 
 import java.util.Arrays;
 import java.util.List;
@@ -64,7 +61,7 @@ public class SquidPlugin implements Plugin {
   public List getExtensions() {
     return Arrays.asList(SquidSearchProxy.class, SquidSensor.class, SquidRuleRepository.class, JavaSourceImporter.class,
         ClassComplexityDistributionBuilder.class, FunctionComplexityDistributionBuilder.class, ClassesDecorator.class,
-        FunctionsDecorator.class);
+        ChidamberKemererDistributionBuilder.class, FunctionsDecorator.class);
   }
 
   @Override
index bbadc0ae4e8ff18a5078310ee8a50401d3d9cc79..2ebc7a286908840d5e69c38ff7b985d2d53a65e3 100644 (file)
@@ -37,7 +37,7 @@ public final class BridgeFactory {
   private static List<Bridge> create(NoSonarFilter noSonarFilter) {
     return Arrays.asList(new CopyBasicMeasuresBridge(), new PackagesBridge(), new PublicUndocumentedApiBridge(),
         new NoSonarFilterLoader(noSonarFilter),
-        new ChidamberKemererBridge(), new RobertCMartinBridge(), new ChidamberKemererDistributionBridge(), new DesignBridge(),
+        new ChidamberKemererBridge(), new RobertCMartinBridge(), new DesignBridge(),
         new Lcom4BlocksBridge(), new ChecksBridge());
   }
 
index 53cc50904bdfad99fb8a946d109d0d67be49c978..4b09d64d2c637459379c65d75f88aadc2a342647 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.plugins.squid.bridges;
 
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.resources.Resource;
+import org.sonar.java.api.JavaClass;
+import org.sonar.squid.api.SourceClass;
 import org.sonar.squid.api.SourceFile;
 import org.sonar.squid.measures.Metric;
 
@@ -30,11 +32,12 @@ public class ChidamberKemererBridge extends Bridge {
     super(true);
   }
 
+
   @Override
   public final void onFile(SourceFile squidFile, Resource sonarFile) {
     context.saveMeasure(sonarFile, CoreMetrics.DEPTH_IN_TREE, squidFile.getDouble(Metric.DIT));
-    context.saveMeasure(sonarFile, CoreMetrics.RFC, squidFile.getDouble(Metric.RFC));
     context.saveMeasure(sonarFile, CoreMetrics.NUMBER_OF_CHILDREN, squidFile.getDouble(Metric.NOC));
+    context.saveMeasure(sonarFile, CoreMetrics.RFC, squidFile.getDouble(Metric.RFC));
     context.saveMeasure(sonarFile, CoreMetrics.LCOM4, squidFile.getDouble(Metric.LCOM4));
   }
 }
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererDistributionBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererDistributionBridge.java
deleted file mode 100644 (file)
index 9a4ef5f..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.plugins.squid.bridges;
-
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.Resource;
-import org.sonar.squid.api.SourceClass;
-import org.sonar.squid.api.SourcePackage;
-import org.sonar.squid.indexer.QueryByMeasure;
-import org.sonar.squid.indexer.QueryByParent;
-import org.sonar.squid.indexer.QueryByType;
-import org.sonar.squid.math.MeasuresDistribution;
-import org.sonar.squid.measures.Metric;
-
-import java.util.Map;
-
-public class ChidamberKemererDistributionBridge extends Bridge {
-
-  public static final int[] LCOM4_LIMITS = {2, 3, 4, 5, 10};// 1 is excluded
-  public static final int[] RFC_LIMITS = {0,5,10,20,30,50,90,150};
-
-  protected ChidamberKemererDistributionBridge() {
-    super(true);
-  }
-
-  @Override
-  public final void onPackage(SourcePackage squidPackage, Resource sonarPackage) {
-    context.saveMeasure(sonarPackage, new Measure(CoreMetrics.LCOM4_DISTRIBUTION, getDistribution(squidPackage, Metric.LCOM4, LCOM4_LIMITS)));
-    context.saveMeasure(sonarPackage, new Measure(CoreMetrics.RFC_DISTRIBUTION, getDistribution(squidPackage, Metric.RFC, RFC_LIMITS)));
-  }
-
-  private String getDistribution(SourcePackage squidPackage, Metric metric, int[] limits) {
-    MeasuresDistribution distribution = new MeasuresDistribution(squid.search(new QueryByParent(squidPackage), new QueryByType(SourceClass.class),
-        new QueryByMeasure(metric, QueryByMeasure.Operator.GREATER_THAN_EQUALS, 0)));
-    Map<Integer, Integer> distrib = distribution.distributeAccordingTo(metric, limits);
-    StringBuilder distribString = new StringBuilder(32);
-    for (Map.Entry<Integer, Integer> entry : distrib.entrySet()) {
-      distribString.append(entry.getKey()).append("=").append(entry.getValue().toString()).append(";");
-    }
-    distribString.setLength(distribString.length() - 1);
-    return distribString.toString();
-  }
-}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridge.java
deleted file mode 100644 (file)
index a9ae54a..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.plugins.squid.bridges;
-
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.Resource;
-import org.sonar.squid.Squid;
-import org.sonar.squid.api.SourceClass;
-import org.sonar.squid.api.SourceCode;
-import org.sonar.squid.api.SourcePackage;
-import org.sonar.squid.indexer.QueryByParent;
-import org.sonar.squid.indexer.QueryByType;
-import org.sonar.squid.math.MeasuresDistribution;
-import org.sonar.squid.measures.Metric;
-
-import java.util.Map;
-
-public class ClassComplexityDistributionBridge extends Bridge {
-
-  public static final int[] CLASSES_DISTRIB_BOTTOM_LIMITS = {0, 5, 10, 20, 30, 60, 90};
-
-  protected ClassComplexityDistributionBridge() {
-    super(false);
-  }
-
-  @Override
-  public final void onPackage(SourcePackage squidPackage, Resource sonarPackage) {
-    if (squidPackage.getDouble(Metric.METHODS) > 0) {
-      context.saveMeasure(sonarPackage, new Measure(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, getClassesComplexityDistribution(squidPackage, squid)));
-    }
-  }
-
-  private String getClassesComplexityDistribution(SourceCode unit, Squid squid) {
-    MeasuresDistribution distribution = new MeasuresDistribution(squid.search(new QueryByParent(unit), new QueryByType(SourceClass.class)));
-    Map<Integer, Integer> distrib = distribution.distributeAccordingTo(Metric.COMPLEXITY, CLASSES_DISTRIB_BOTTOM_LIMITS);
-    StringBuilder distribString = new StringBuilder(32);
-    for (Map.Entry<Integer, Integer> entry : distrib.entrySet()) {
-      distribString.append(entry.getKey()).append('=').append(entry.getValue().toString()).append(';');
-    }
-    distribString.setLength(distribString.length() - 1);
-    return distribString.toString();
-  }
-}
index d5f11a53d479f9927010d5466259c316937cf65c..0d596dda816bc69ac70b448e49fde739343299cb 100644 (file)
@@ -61,8 +61,6 @@ public final class CopyBasicMeasuresBridge extends Bridge {
     copy(squidCode, sonarResource, Metric.COMPLEXITY, CoreMetrics.COMPLEXITY);
     copy(squidCode, sonarResource, Metric.STATEMENTS, CoreMetrics.STATEMENTS);
     copy(squidCode, sonarResource, Metric.COMMENTED_OUT_CODE_LINES, CoreMetrics.COMMENTED_OUT_CODE_LINES);
-
-
   }
 
   private void copy(SourceCode squidResource, Resource sonarResource, Metric squidMetric, org.sonar.api.measures.Metric sonarMetric) {
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridge.java
deleted file mode 100644 (file)
index f51ff51..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.plugins.squid.bridges;
-
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.Resource;
-import org.sonar.squid.api.SourceCode;
-import org.sonar.squid.api.SourceMethod;
-import org.sonar.squid.api.SourcePackage;
-import org.sonar.squid.indexer.QueryByMeasure;
-import org.sonar.squid.indexer.QueryByParent;
-import org.sonar.squid.indexer.QueryByType;
-import org.sonar.squid.math.MeasuresDistribution;
-import org.sonar.squid.measures.Metric;
-
-import java.util.Map;
-
-public class FunctionComplexityDistributionBridge extends Bridge {
-
-  public static final int[] LIMITS = {1, 2, 4, 6, 8, 10, 12};
-
-  protected FunctionComplexityDistributionBridge() {
-    super(false);
-  }
-
-  @Override
-  public final void onPackage(SourcePackage squidPackage, Resource sonarPackage) {
-    if (squidPackage.getDouble(Metric.METHODS) > 0) {
-      context.saveMeasure(sonarPackage, new Measure(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, getFunctionComplexityDistribution(squidPackage)));
-    }
-  }
-
-  private String getFunctionComplexityDistribution(SourceCode unit) {
-    MeasuresDistribution distribution = new MeasuresDistribution(squid.search(new QueryByParent(unit), new QueryByType(SourceMethod.class),
-        new QueryByMeasure(Metric.ACCESSORS, QueryByMeasure.Operator.EQUALS, 0)));
-    Map<Integer, Integer> distrib = distribution.distributeAccordingTo(Metric.COMPLEXITY, LIMITS);
-    StringBuilder distribString = new StringBuilder(32);
-    for (Map.Entry<Integer, Integer> entry : distrib.entrySet()) {
-      distribString.append(entry.getKey()).append("=").append(entry.getValue().toString()).append(";");
-    }
-    distribString.setLength(distribString.length() - 1);
-    return distribString.toString();
-  }
-}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ChidamberKemererDistributionBuilder.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ChidamberKemererDistributionBuilder.java
new file mode 100644 (file)
index 0000000..0dac243
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.squid.decorators;
+
+import org.sonar.api.batch.Decorator;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.batch.DependedUpon;
+import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.RangeDistributionBuilder;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.Scopes;
+
+public final class ChidamberKemererDistributionBuilder implements Decorator {
+
+  public static final Integer[] LCOM4_LIMITS = {2, 3, 4, 5, 10};// 1 is excluded
+  public static final Integer[] RFC_LIMITS = {0, 5, 10, 20, 30, 50, 90, 150};
+
+  @DependedUpon
+  public Metric generatesLcom4Distribution() {
+    return CoreMetrics.LCOM4_DISTRIBUTION;
+  }
+
+  @DependsUpon
+  public Metric dependsInLcom4() {
+    return CoreMetrics.LCOM4;
+  }
+
+  @DependedUpon
+  public Metric generatesRfcDistribution() {
+    return CoreMetrics.RFC_DISTRIBUTION;
+  }
+
+  @DependsUpon
+  public Metric dependsInRfc() {
+    return CoreMetrics.RFC;
+  }
+
+  public void decorate(Resource resource, DecoratorContext context) {
+    if (shouldExecuteOn(resource)) {
+      RangeDistributionBuilder lcom4Distribution = new RangeDistributionBuilder(CoreMetrics.LCOM4_DISTRIBUTION, LCOM4_LIMITS);
+      RangeDistributionBuilder rfcDistribution = new RangeDistributionBuilder(CoreMetrics.RFC_DISTRIBUTION, RFC_LIMITS);
+
+      for (DecoratorContext childContext : context.getChildren()) {
+        if (Scopes.isFile(childContext.getResource())) {
+          addMeasureToDistribution(childContext, lcom4Distribution, CoreMetrics.LCOM4);
+          addMeasureToDistribution(childContext, rfcDistribution, CoreMetrics.RFC);
+        }
+      }
+
+      saveDistribution(context, lcom4Distribution);
+      saveDistribution(context, rfcDistribution);
+    }
+  }
+
+  private void addMeasureToDistribution(DecoratorContext childContext, RangeDistributionBuilder distribution, Metric metric) {
+    Measure measure = childContext.getMeasure(metric);
+    if (measure != null) {
+      distribution.add(measure.getIntValue());
+    }
+  }
+
+  private void saveDistribution(DecoratorContext context, RangeDistributionBuilder distribution) {
+    Measure measure = distribution.build(false);
+    if (measure != null) {
+      context.saveMeasure(measure);
+    }
+  }
+
+  boolean shouldExecuteOn(Resource resource) {
+    return Scopes.isDirectory(resource);
+  }
+
+  public boolean shouldExecuteOnProject(Project project) {
+    return Java.KEY.equals(project.getLanguageKey());
+  }
+}
\ No newline at end of file
index 52db97389cf1cd1471c43980559245fbbe59419e..0357049ac113ca16f92b6d4ea00bfa203caa941f 100644 (file)
@@ -31,6 +31,6 @@ public class SquidPluginTest {
   @Test
   public void coverageForFun() {
     assertThat(new SquidPlugin().getKey(), not(nullValue()));
-    assertThat(new SquidPlugin().getExtensions().size(), is(8));
+    assertThat(new SquidPlugin().getExtensions().size(), is(9));
   }
 }
index 964d51977b85e1bcd2cde5f293df8a45d51f51be..42c51b64667f276fd189a3b88938aade3575d279 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.squid.bridges;
 
+import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.resources.JavaFile;