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;
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
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());
}
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;
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));
}
}
+++ /dev/null
-/*
- * 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();
- }
-}
+++ /dev/null
-/*
- * 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();
- }
-}
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) {
+++ /dev/null
-/*
- * 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();
- }
-}
--- /dev/null
+/*
+ * 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
@Test
public void coverageForFun() {
assertThat(new SquidPlugin().getKey(), not(nullValue()));
- assertThat(new SquidPlugin().getExtensions().size(), is(8));
+ assertThat(new SquidPlugin().getExtensions().size(), is(9));
}
}
*/
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;