From ea8d694355aac5dc93de2650e11d802c6bb3e3d5 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 4 Feb 2011 15:05:56 +0100 Subject: SONAR-2155 Resource filters are ignored in RFC/LCOM4 distributions of Java projects --- .../java/org/sonar/plugins/squid/SquidPlugin.java | 7 +- .../sonar/plugins/squid/bridges/BridgeFactory.java | 2 +- .../squid/bridges/ChidamberKemererBridge.java | 5 +- .../ChidamberKemererDistributionBridge.java | 61 -------------- .../bridges/ClassComplexityDistributionBridge.java | 61 -------------- .../squid/bridges/CopyBasicMeasuresBridge.java | 2 - .../FunctionComplexityDistributionBridge.java | 62 -------------- .../ChidamberKemererDistributionBuilder.java | 98 ++++++++++++++++++++++ .../org/sonar/plugins/squid/SquidPluginTest.java | 2 +- .../squid/bridges/ChidamberKemererBridgeTest.java | 1 + 10 files changed, 107 insertions(+), 194 deletions(-) delete mode 100644 plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererDistributionBridge.java delete mode 100644 plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridge.java delete mode 100644 plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridge.java create mode 100644 plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ChidamberKemererDistributionBuilder.java (limited to 'plugins') diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java index b3236c09757..7196337aca9 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java @@ -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 diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java index bbadc0ae4e8..2ebc7a28690 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/BridgeFactory.java @@ -37,7 +37,7 @@ public final class BridgeFactory { private static List 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()); } diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java index 53cc50904bd..4b09d64d2c6 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java @@ -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 index 9a4ef5f4192..00000000000 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererDistributionBridge.java +++ /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 distrib = distribution.distributeAccordingTo(metric, limits); - StringBuilder distribString = new StringBuilder(32); - for (Map.Entry 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 index a9ae54a9cbd..00000000000 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ClassComplexityDistributionBridge.java +++ /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 distrib = distribution.distributeAccordingTo(Metric.COMPLEXITY, CLASSES_DISTRIB_BOTTOM_LIMITS); - StringBuilder distribString = new StringBuilder(32); - for (Map.Entry 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/CopyBasicMeasuresBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/CopyBasicMeasuresBridge.java index d5f11a53d47..0d596dda816 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/CopyBasicMeasuresBridge.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/CopyBasicMeasuresBridge.java @@ -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 index f51ff51cde8..00000000000 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/FunctionComplexityDistributionBridge.java +++ /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 distrib = distribution.distributeAccordingTo(Metric.COMPLEXITY, LIMITS); - StringBuilder distribString = new StringBuilder(32); - for (Map.Entry 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 index 00000000000..0dac2438e65 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ChidamberKemererDistributionBuilder.java @@ -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 diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java index 52db97389cf..0357049ac11 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java @@ -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)); } } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java index 964d51977b8..42c51b64667 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java @@ -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; -- cgit v1.2.3