From 05e28d46dc4437b74a1baa7a710cddd0453aa9b5 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 20 Feb 2014 11:50:41 +0100 Subject: [PATCH] SONAR-926 fix support of CPD without exclusion patterns --- .../java/org/sonar/plugins/cpd/SonarBridgeEngine.java | 7 ++++--- .../src/main/java/org/sonar/plugins/cpd/SonarEngine.java | 9 +++++---- .../main/java/org/sonar/api/batch/fs/FilePredicates.java | 6 ++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java index 14b073d6711..0ff44946650 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java @@ -22,6 +22,7 @@ package org.sonar.plugins.cpd; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; @@ -80,12 +81,12 @@ public class SonarBridgeEngine extends CpdEngine { public void analyse(Project project, String languageKey, SensorContext context) { String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS); logExclusions(cpdExclusions, LOG); - Iterable sourceFiles = fs.inputFiles(FilePredicates.and( + List sourceFiles = Lists.newArrayList(fs.inputFiles(FilePredicates.and( FilePredicates.hasType(InputFile.Type.MAIN), FilePredicates.hasLanguage(languageKey), FilePredicates.doesNotMatchPathPatterns(cpdExclusions) - )); - if (!sourceFiles.iterator().hasNext()) { + ))); + if (sourceFiles.isEmpty()) { return; } diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java index 0b340ce17c4..d463d8cf314 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java @@ -21,6 +21,7 @@ package org.sonar.plugins.cpd; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringEscapeUtils; import org.slf4j.Logger; @@ -93,12 +94,12 @@ public class SonarEngine extends CpdEngine { public void analyse(Project project, String languageKey, SensorContext context) { String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS); logExclusions(cpdExclusions, LOG); - Iterable sourceFiles = fs.inputFiles(FilePredicates.and( + List sourceFiles = Lists.newArrayList(fs.inputFiles(FilePredicates.and( FilePredicates.hasType(InputFile.Type.MAIN), FilePredicates.hasLanguage(languageKey), FilePredicates.doesNotMatchPathPatterns(cpdExclusions) - )); - if (!sourceFiles.iterator().hasNext()) { + ))); + if (sourceFiles.isEmpty()) { return; } SonarDuplicationsIndex index = createIndex(project, sourceFiles); @@ -135,7 +136,7 @@ public class SonarEngine extends CpdEngine { return index; } - private void detect(SonarDuplicationsIndex index, SensorContext context, Iterable sourceFiles) { + private void detect(SonarDuplicationsIndex index, SensorContext context, List sourceFiles) { ExecutorService executorService = Executors.newSingleThreadExecutor(); try { for (InputFile inputFile : sourceFiles) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java index 877666c733b..16c79cb962e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java @@ -65,6 +65,9 @@ public class FilePredicates { } public static FilePredicate matchesPathPatterns(String[] inclusionPatterns) { + if (inclusionPatterns.length == 0) { + return ALWAYS_TRUE; + } FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length]; for (int i = 0; i < inclusionPatterns.length; i++) { predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i])); @@ -77,6 +80,9 @@ public class FilePredicates { } public static FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) { + if (exclusionPatterns.length == 0) { + return ALWAYS_TRUE; + } return not(matchesPathPatterns(exclusionPatterns)); } -- 2.39.5