diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-01 10:57:21 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-01 10:57:28 +0100 |
commit | 936f7f5f75e4acd3a58e12f67e69d9e24290c91c (patch) | |
tree | 6abd75dd50384cd602994d04d48fc48aec87569c /plugins/sonar-squid-java-plugin/src | |
parent | ee6efa4035d4f956bec561b9680f10d35068ea46 (diff) | |
download | sonarqube-936f7f5f75e4acd3a58e12f67e69d9e24290c91c.tar.gz sonarqube-936f7f5f75e4acd3a58e12f67e69d9e24290c91c.zip |
SONAR-2159 The total classes used in complexity distribution is different than the number of classes
Diffstat (limited to 'plugins/sonar-squid-java-plugin/src')
7 files changed, 136 insertions, 28 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidExecutor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidExecutor.java index a2736b7c755..46ffd241297 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidExecutor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidExecutor.java @@ -53,7 +53,7 @@ public final class SquidExecutor { private CheckFactory checkFactory; public SquidExecutor(boolean analyzePropertyAccessors, String fieldNamesToExcludeFromLcom4Computation, CheckFactory checkFactory, - Charset sourcesCharset) { + Charset sourcesCharset) { JavaSquidConfiguration conf = createJavaSquidConfiguration(analyzePropertyAccessors, fieldNamesToExcludeFromLcom4Computation, sourcesCharset); squid = new Squid(conf); @@ -61,13 +61,13 @@ public final class SquidExecutor { } private JavaSquidConfiguration createJavaSquidConfiguration(boolean analyzePropertyAccessors, - String fieldNamesToExcludeFromLcom4Computation, - Charset sourcesCharset) { + String fieldNamesToExcludeFromLcom4Computation, + Charset sourcesCharset) { JavaSquidConfiguration conf = new JavaSquidConfiguration(analyzePropertyAccessors, sourcesCharset); - if ( !StringUtils.isBlank(fieldNamesToExcludeFromLcom4Computation)) { + if (!StringUtils.isBlank(fieldNamesToExcludeFromLcom4Computation)) { for (String fieldName : fieldNamesToExcludeFromLcom4Computation.split(",")) { - if ( !StringUtils.isBlank(fieldName)) { + if (!StringUtils.isBlank(fieldName)) { conf.addFieldToExcludeFromLcom4Calculation(fieldName); } } @@ -136,8 +136,11 @@ public final class SquidExecutor { Collection<SourceCode> squidClasses = squid.search(new QueryByType(SourceClass.class)); for (SourceCode squidClass : squidClasses) { Resource sonarClass = resourceIndex.get(squidClass); - for (Bridge bridge : bridges) { - bridge.onClass((SourceClass) squidClass, (JavaClass)sonarClass); + // can be null with anonymous classes + if (sonarClass != null) { + for (Bridge bridge : bridges) { + bridge.onClass((SourceClass) squidClass, (JavaClass) sonarClass); + } } } } @@ -145,9 +148,11 @@ public final class SquidExecutor { private void saveMethods(ResourceIndex resourceIndex, List<Bridge> bridges) { Collection<SourceCode> squidMethods = squid.search(new QueryByType(SourceMethod.class)); for (SourceCode squidMethod : squidMethods) { - JavaMethod sonarMethod = (JavaMethod)resourceIndex.get(squidMethod); - for (Bridge bridge : bridges) { - bridge.onMethod((SourceMethod) squidMethod, sonarMethod); + JavaMethod sonarMethod = (JavaMethod) resourceIndex.get(squidMethod); + if (sonarMethod != null) { + for (Bridge bridge : bridges) { + bridge.onMethod((SourceMethod) squidMethod, sonarMethod); + } } } } 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 ea18236faef..6fed2b2c84c 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 @@ -24,7 +24,9 @@ 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 java.util.Arrays; import java.util.List; @@ -61,7 +63,8 @@ public class SquidPlugin implements Plugin { public List getExtensions() { return Arrays.asList(SquidSearchProxy.class, SquidSensor.class, SquidRuleRepository.class, JavaSourceImporter.class, - ClassComplexityDistributionBuilder.class, FunctionComplexityDistributionBuilder.class); + ClassComplexityDistributionBuilder.class, FunctionComplexityDistributionBuilder.class, ClassesDecorator.class, + FunctionsDecorator.class); } @Override 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 94f7b3bdd65..a783141cc75 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 @@ -39,7 +39,6 @@ public final class CopyBasicMeasuresBridge extends Bridge { @Override public void onFile(SourceFile squidFile, Resource sonarResource) { copyStandard(squidFile, sonarResource); - copy(squidFile, sonarResource, Metric.CLASSES, CoreMetrics.CLASSES); copy(squidFile, sonarResource, Metric.FILES, CoreMetrics.FILES); context.saveMeasure(sonarResource, CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY, ParsingUtils.scaleValue(squidFile.getDouble(Metric.PUBLIC_DOCUMENTED_API_DENSITY) * 100, 2)); } @@ -58,12 +57,13 @@ public final class CopyBasicMeasuresBridge extends Bridge { copy(squidCode, sonarResource, Metric.LINES_OF_CODE, CoreMetrics.NCLOC); copy(squidCode, sonarResource, Metric.LINES, CoreMetrics.LINES); copy(squidCode, sonarResource, Metric.COMMENT_LINES_WITHOUT_HEADER, CoreMetrics.COMMENT_LINES); - copy(squidCode, sonarResource, Metric.METHODS, CoreMetrics.FUNCTIONS); copy(squidCode, sonarResource, Metric.ACCESSORS, CoreMetrics.ACCESSORS); copy(squidCode, sonarResource, Metric.PUBLIC_API, CoreMetrics.PUBLIC_API); 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/ResourceIndex.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java index 105a33e6b2d..b9c5819b994 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java @@ -30,6 +30,7 @@ import org.sonar.java.api.JavaClass; import org.sonar.java.api.JavaMethod; import org.sonar.squid.Squid; import org.sonar.squid.api.*; +import org.sonar.squid.indexer.QueryByMeasure; import org.sonar.squid.indexer.QueryByType; import org.sonar.squid.measures.Metric; @@ -38,7 +39,7 @@ import java.util.HashMap; public final class ResourceIndex extends HashMap<SourceCode, Resource> { - public ResourceIndex loadSquidResources(Squid squid, SensorContext context, Project project) { + public ResourceIndex loadSquidResources(Squid squid, SensorContext context, Project project) { loadSquidProject(squid, project); loadSquidPackages(squid, context); loadSquidFiles(squid, context); @@ -64,16 +65,16 @@ public final class ResourceIndex extends HashMap<SourceCode, Resource> { Collection<SourceCode> files = squid.search(new QueryByType(SourceFile.class)); for (SourceCode squidFile : files) { JavaFile sonarFile = SquidUtils.convertJavaFileKeyFromSquidFormat(squidFile.getKey()); - JavaPackage sonarPackage = (JavaPackage)get(squidFile.getParent(SourcePackage.class)); + JavaPackage sonarPackage = (JavaPackage) get(squidFile.getParent(SourcePackage.class)); context.index(sonarFile, sonarPackage); put(squidFile, context.getResource(sonarFile)); // resource is reloaded to get the id } } private void loadSquidClasses(Squid squid, SensorContext context) { - Collection<SourceCode> classes = squid.search(new QueryByType(SourceClass.class)); + Collection<SourceCode> classes = squid.search(new QueryByType(SourceClass.class), new QueryByMeasure(Metric.CLASSES, QueryByMeasure.Operator.GREATER_THAN_EQUALS, 1)); for (SourceCode squidClass : classes) { - JavaFile sonarFile = (JavaFile)get(squidClass.getParent(SourceFile.class)); + JavaFile sonarFile = (JavaFile) get(squidClass.getParent(SourceFile.class)); JavaClass sonarClass = new JavaClass.Builder() .setName(convertClassKey(squidClass.getKey())) .setFromLine(squidClass.getStartAtLine()) @@ -88,17 +89,19 @@ public final class ResourceIndex extends HashMap<SourceCode, Resource> { Collection<SourceCode> methods = squid.search(new QueryByType(SourceMethod.class)); for (SourceCode squidMethod : methods) { SourceClass squidClass = squidMethod.getParent(SourceClass.class); - JavaClass sonarClass = (JavaClass)get(squidClass); - JavaMethod sonarMethod = new JavaMethod.Builder() - .setClass(sonarClass) - .setSignature(squidMethod.getName()) - .setFromLine(squidMethod.getStartAtLine()) - .setToLine(squidMethod.getEndAtLine()) - .setAccessor(squidMethod.getInt(Metric.ACCESSORS)>0) - .create(); + JavaClass sonarClass = (JavaClass) get(squidClass); + if (sonarClass != null) { + JavaMethod sonarMethod = new JavaMethod.Builder() + .setClass(sonarClass) + .setSignature(squidMethod.getName()) + .setFromLine(squidMethod.getStartAtLine()) + .setToLine(squidMethod.getEndAtLine()) + .setAccessor(squidMethod.getInt(Metric.ACCESSORS) > 0) + .create(); - context.index(sonarMethod, sonarClass); - put(squidMethod, sonarMethod); + context.index(sonarMethod, sonarClass); + put(squidMethod, sonarMethod); + } } } diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassesDecorator.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassesDecorator.java new file mode 100644 index 00000000000..aa605bdefd2 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassesDecorator.java @@ -0,0 +1,48 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +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 ClassesDecorator implements Decorator { + + public void decorate(Resource resource, DecoratorContext context) { + if (Scopes.isFile(resource)) { + int classes = 0; + for (DecoratorContext child : context.getChildren()) { + if (Scopes.isType(child.getResource())) { + classes++; + } + } + context.saveMeasure(new Measure(CoreMetrics.CLASSES, (double) classes)); + } + } + + public boolean shouldExecuteOnProject(Project project) { + return Java.KEY.equals(project.getLanguageKey()); + } +} diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FunctionsDecorator.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FunctionsDecorator.java new file mode 100644 index 00000000000..3bd9e65f2e3 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/FunctionsDecorator.java @@ -0,0 +1,49 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.api.resources.Scopes; +import org.sonar.java.api.JavaMethod; + +public final class FunctionsDecorator implements Decorator { + + public void decorate(Resource resource, DecoratorContext context) { + if (Scopes.isType(resource)) { + int methods=0; + for (DecoratorContext child : context.getChildren()) { + if (child.getResource() instanceof JavaMethod && !((JavaMethod)child.getResource()).isAccessor()) { + methods++; + } + } + context.saveMeasure(new Measure(CoreMetrics.FUNCTIONS, (double)methods)); + } + } + + public boolean shouldExecuteOnProject(Project project) { + return Java.KEY.equals(project.getLanguageKey()); + } +} 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 0387b501095..4bf33ac016f 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(6)); + assertThat(new SquidPlugin().getExtensions().size(), is(8)); } } |