From c55c3c916bd1f76f52a237e5b49eca880f8a2945 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Mon, 30 May 2011 01:35:03 +0400 Subject: [PATCH] SONAR-2164 Add an option to skip package design analysis --- .../sonar/plugins/squid/SquidExecutor.java | 13 ++++++++--- .../plugins/squid/bridges/BridgeFactory.java | 23 +++++++++++++------ .../squid/bridges/BridgeFactoryTest.java | 18 +++++++++++---- .../java/org/sonar/api/CoreProperties.java | 8 +++++++ 4 files changed, 47 insertions(+), 15 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 4be634b0546..bd295bcff53 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 @@ -21,6 +21,7 @@ package org.sonar.plugins.squid; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.SensorContext; import org.sonar.api.checks.CheckFactory; import org.sonar.api.checks.NoSonarFilter; @@ -93,7 +94,14 @@ public final class SquidExecutor { if (sourceScanned) { TimeProfiler profiler = new TimeProfiler(getClass()).start("Squid extraction"); ResourceIndex resourceIndex = new ResourceIndex().loadSquidResources(squid, context, project); - List bridges = BridgeFactory.create(bytecodeScanned, context, checkFactory, resourceIndex, squid, noSonarFilter); + + boolean skipPackageDesignAnalysis = CoreProperties.DESIGN_SKIP_PACKAGE_DESIGN_DEFAULT_VALUE; + if (project.getConfiguration() != null) { + skipPackageDesignAnalysis = project.getConfiguration() + .getBoolean(CoreProperties.DESIGN_SKIP_PACKAGE_DESIGN_PROPERTY, CoreProperties.DESIGN_SKIP_PACKAGE_DESIGN_DEFAULT_VALUE); + } + + List bridges = BridgeFactory.create(bytecodeScanned, skipPackageDesignAnalysis, context, checkFactory, resourceIndex, squid, noSonarFilter); saveProject(resourceIndex, bridges); savePackages(resourceIndex, bridges); saveFiles(resourceIndex, bridges); @@ -155,7 +163,6 @@ public final class SquidExecutor { } } - void scanSources(Collection sourceFiles) { if (sourceFiles != null && !sourceFiles.isEmpty()) { TimeProfiler profiler = new TimeProfiler(getClass()).start("Java AST scan"); @@ -188,7 +195,7 @@ public final class SquidExecutor { for (File bytecodeFilesOrDirectory : bytecodeFilesOrDirectories) { if (bytecodeFilesOrDirectory.exists() && (bytecodeFilesOrDirectory.isFile() || - !FileUtils.listFiles(bytecodeFilesOrDirectory, new String[]{"class"}, true).isEmpty())) { + !FileUtils.listFiles(bytecodeFilesOrDirectory, new String[] { "class" }, true).isEmpty())) { return true; } } 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 2ebc7a28690..578ee211cdd 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 @@ -19,13 +19,13 @@ */ package org.sonar.plugins.squid.bridges; +import com.google.common.collect.Lists; import org.sonar.api.batch.SensorContext; import org.sonar.api.checks.CheckFactory; import org.sonar.api.checks.NoSonarFilter; import org.sonar.squid.Squid; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public final class BridgeFactory { @@ -34,17 +34,26 @@ public final class BridgeFactory { // only static methods } - private static List create(NoSonarFilter noSonarFilter) { - return Arrays.asList(new CopyBasicMeasuresBridge(), new PackagesBridge(), new PublicUndocumentedApiBridge(), + private static List create(NoSonarFilter noSonarFilter, boolean skipPackageDesignAnalysis) { + ArrayList result = Lists.newArrayList( + new CopyBasicMeasuresBridge(), + new PackagesBridge(), + new PublicUndocumentedApiBridge(), new NoSonarFilterLoader(noSonarFilter), - new ChidamberKemererBridge(), new RobertCMartinBridge(), new DesignBridge(), - new Lcom4BlocksBridge(), new ChecksBridge()); + new ChidamberKemererBridge(), + new RobertCMartinBridge(), + new Lcom4BlocksBridge(), + new ChecksBridge()); + if (!skipPackageDesignAnalysis) { + result.add(new DesignBridge()); + } + return result; } - public static List create(boolean bytecodeScanned, SensorContext context, CheckFactory checkFactory, + public static List create(boolean bytecodeScanned, boolean skipPackageDesignAnalysis, SensorContext context, CheckFactory checkFactory, ResourceIndex resourceIndex, Squid squid, NoSonarFilter noSonarFilter) { List result = new ArrayList(); - for (Bridge bridge : create(noSonarFilter)) { + for (Bridge bridge : create(noSonarFilter, skipPackageDesignAnalysis)) { bridge.setCheckFactory(checkFactory); if (!bridge.needsBytecode() || bytecodeScanned) { bridge.setContext(context); diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/BridgeFactoryTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/BridgeFactoryTest.java index 783b24fa7d6..f274bd6f038 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/BridgeFactoryTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/BridgeFactoryTest.java @@ -19,28 +19,36 @@ */ package org.sonar.plugins.squid.bridges; -import java.util.List; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import java.util.List; public class BridgeFactoryTest { @Test public void createForSourceAnalysis() { - List astBridges = BridgeFactory.create(false, null, null, null, null, null); + List astBridges = BridgeFactory.create(false, true, null, null, null, null, null); assertFalse(has(astBridges, DesignBridge.class)); assertTrue(has(astBridges, CopyBasicMeasuresBridge.class)); } @Test public void createForSourceAndBytecodeAnalysis() { - List allBridges = BridgeFactory.create(true, null, null, null, null, null); + List allBridges = BridgeFactory.create(true, false, null, null, null, null, null); assertTrue(has(allBridges, DesignBridge.class)); assertTrue(has(allBridges, CopyBasicMeasuresBridge.class)); + assertTrue(has(allBridges, Lcom4BlocksBridge.class)); + } + @Test + public void createForSourceAndBytecodeWithoutDesignAnalysis() { + List allBridges = BridgeFactory.create(true, true, null, null, null, null, null); + assertFalse(has(allBridges, DesignBridge.class)); + assertTrue(has(allBridges, CopyBasicMeasuresBridge.class)); + assertTrue(has(allBridges, Lcom4BlocksBridge.class)); } private boolean has(List bridges, Class bridgeClass) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 9bdd1c3218d..d946bcad9a4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -118,6 +118,14 @@ public interface CoreProperties { String DESIGN_SKIP_DESIGN_PROPERTY = "sonar.skipDesign"; boolean DESIGN_SKIP_DESIGN_DEFAULT_VALUE = false; + /** + * Indicates whether Package Design Analysis should be skipped. + * + * @since 2.9 + */ + String DESIGN_SKIP_PACKAGE_DESIGN_PROPERTY = "sonar.skipPackageDesign"; + boolean DESIGN_SKIP_PACKAGE_DESIGN_DEFAULT_VALUE = false; + /* Findbugs */ String FINDBUGS_PLUGIN = "findbugs"; String FINDBUGS_EFFORT_PROPERTY = "sonar.findbugs.effort"; -- 2.39.5