diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-02 11:06:20 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-04 14:58:16 +0200 |
commit | bcae3c079bef995afef0ffc4dbe774fbdf0bf9ed (patch) | |
tree | dcdb45969ce79cdc88827152796dba4fb7ae7355 | |
parent | 69d96f792448e9809bed04ecc1ea0dd520e8f9e6 (diff) | |
download | sonarqube-bcae3c079bef995afef0ffc4dbe774fbdf0bf9ed.tar.gz sonarqube-bcae3c079bef995afef0ffc4dbe774fbdf0bf9ed.zip |
Fix quality flaws
16 files changed, 49 insertions, 39 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java index 5e3840a8510..52318045214 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java @@ -55,7 +55,7 @@ import org.sonar.api.batch.ScannerSide; public interface FileSystem { /** - * Absolute base directory of module + * Absolute base directory of module. */ File baseDir(); @@ -136,7 +136,9 @@ public interface FileSystem { /** * Files matching the given predicate. * @see #predicates() + * @deprecated since 6.6 Plugins should avoid working with {@link File} and prefer working with {@link InputFile} */ + @Deprecated Iterable<File> files(FilePredicate predicate); /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java index effb5663f31..3562a8d9010 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java @@ -120,6 +120,10 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile return excludedForCoverage; } + /** + * @deprecated since 6.6 + */ + @Deprecated @Override public String relativePath() { return indexedFile.relativePath(); @@ -133,6 +137,10 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile return indexedFile.getProjectRelativePath(); } + /** + * @deprecated since 6.6 + */ + @Deprecated @Override public String absolutePath() { return indexedFile.absolutePath(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/TaskResult.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/TaskResult.java index d9f1e193a78..41786c272da 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/TaskResult.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/TaskResult.java @@ -104,7 +104,7 @@ public class TaskResult implements org.sonar.scanner.mediumtest.ScanTaskObserver private void storeFs(ProjectScanContainer container) { InputComponentStore inputFileCache = container.getComponentByType(InputComponentStore.class); for (InputFile inputPath : inputFileCache.allFiles()) { - inputFiles.put(inputPath.relativePath(), inputPath); + inputFiles.put(((DefaultInputFile) inputPath).getProjectRelativePath(), inputPath); } for (InputDir inputPath : inputFileCache.allDirs()) { inputDirs.put(inputPath.relativePath(), inputPath); @@ -189,7 +189,7 @@ public class TaskResult implements org.sonar.scanner.mediumtest.ScanTaskObserver } } } catch (Exception e) { - throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath(), e); + throw new IllegalStateException("Can't read syntax highlighting for " + file, e); } return result; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java index 94e992c64d8..02232592b42 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java @@ -103,10 +103,10 @@ public abstract class AbstractPhaseExecutor { coverageExclusions.log(); for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) { - boolean excluded = coverageExclusions.isExcluded(inputFile); + boolean excluded = coverageExclusions.isExcluded((DefaultInputFile) inputFile); if (excluded) { ((DefaultInputFile) inputFile).setExcludedForCoverage(true); - LOG.debug("File {} excluded for coverage", inputFile.relativePath()); + LOG.debug("File {} excluded for coverage", inputFile); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java index 97675a88960..f0f3ba39895 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java @@ -27,7 +27,7 @@ import javax.annotation.concurrent.Immutable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.config.Configuration; import org.sonar.api.utils.WildcardPattern; @@ -49,11 +49,11 @@ public class CoverageExclusions { log("Excluded sources for coverage: ", exclusionPatterns); } - boolean isExcluded(InputFile file) { + boolean isExcluded(DefaultInputFile file) { boolean found = false; Iterator<WildcardPattern> iterator = exclusionPatterns.iterator(); while (!found && iterator.hasNext()) { - found = iterator.next().match(file.relativePath()); + found = iterator.next().match(file.getModuleRelativePath()); } return found; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java index f12ce7f7186..be8847063f3 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java @@ -22,7 +22,6 @@ package org.sonar.scanner.report; import java.util.Collection; import java.util.stream.Collectors; import javax.annotation.CheckForNull; - import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; @@ -30,7 +29,6 @@ import org.sonar.api.batch.fs.InputComponent; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputModule; -import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultInputComponent; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; @@ -155,8 +153,11 @@ public class ComponentsPublisher implements ReportPublisherStep { @CheckForNull private String getPath(InputComponent component) { - if (component instanceof InputPath) { - InputPath inputPath = (InputPath) component; + if (component instanceof InputFile) { + DefaultInputFile inputPath = (DefaultInputFile) component; + return inputPath.getModuleRelativePath(); + } else if (component instanceof InputDir) { + InputDir inputPath = (InputDir) component; if (StringUtils.isEmpty(inputPath.relativePath())) { return "/"; } else { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java index 46ae0a0e28c..915504ba16c 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java @@ -34,7 +34,6 @@ import org.sonar.api.scan.filesystem.PathResolver; @Immutable public class DefaultInputModuleHierarchy implements InputModuleHierarchy { - private final PathResolver pathResolver = new PathResolver(); private final DefaultInputModule root; private final Map<DefaultInputModule, DefaultInputModule> parents; private final ImmutableMultimap<DefaultInputModule, DefaultInputModule> children; @@ -111,6 +110,6 @@ public class DefaultInputModuleHierarchy implements InputModuleHierarchy { Path parentBaseDir = parent.getBaseDir(); Path moduleBaseDir = inputModule.getBaseDir(); - return pathResolver.relativePath(parentBaseDir, moduleBaseDir); + return PathResolver.relativePath(parentBaseDir, moduleBaseDir); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java index 33cd3749c1b..ea2b20fc4bc 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java @@ -170,8 +170,8 @@ public class FileIndexer { return null; } String language = langDetection.language(realAbsoluteFile, relativePath); - if (language == null && langDetection.forcedLanguage() != null) { - LOG.warn("File '{}' is ignored because it doesn't belong to the forced language '{}'", realAbsoluteFile.toAbsolutePath(), langDetection.forcedLanguage()); + if (language == null && langDetection.getForcedLanguage() != null) { + LOG.warn("File '{}' is ignored because it doesn't belong to the forced language '{}'", realAbsoluteFile.toAbsolutePath(), langDetection.getForcedLanguage()); return null; } DefaultInputFile inputFile = inputFileBuilder.create(type, realAbsoluteFile, language); @@ -179,7 +179,7 @@ public class FileIndexer { progress.increaseExcludedByPatternsCount(); return null; } - String parentRelativePath = getParentRelativePath(inputFile); + String parentRelativePath = getParentRelativePath(realAbsoluteFile); synchronized (this) { progress.markAsIndexed(inputFile); indexFileAndParentDir(inputFile, parentRelativePath); @@ -189,11 +189,11 @@ public class FileIndexer { return null; } - private String getParentRelativePath(InputFile inputFile) { - Path parentDir = inputFile.path().getParent(); + private String getParentRelativePath(Path filePath) { + Path parentDir = filePath.getParent(); String relativePath = PathResolver.relativePath(module.getBaseDir(), parentDir); if (relativePath == null) { - throw new IllegalStateException("Failed to compute relative path of file: " + inputFile); + throw new IllegalStateException("Failed to compute relative path of file: " + parentDir); } return relativePath; } @@ -214,7 +214,7 @@ public class FileIndexer { // InputFileFilter extensions. Might trigger generation of metadata for (InputFileFilter filter : filters) { if (!filter.accept(indexedFile)) { - LOG.debug("'{}' excluded by {}", indexedFile.toString(), filter.getClass().getName()); + LOG.debug("'{}' excluded by {}", indexedFile, filter.getClass().getName()); return false; } } @@ -277,7 +277,7 @@ public class FileIndexer { + "disjoint sets for main and test files"); } int count = indexedCount.incrementAndGet(); - progressReport.message(count + " " + pluralizeFiles(count) + " indexed... (last one was " + inputFile.relativePath() + ")"); + progressReport.message(count + " " + pluralizeFiles(count) + " indexed... (last one was " + inputFile.getProjectRelativePath() + ")"); } void increaseExcludedByPatternsCount() { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java index 7b885a0b428..cace1de40e5 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java @@ -92,7 +92,7 @@ public class LanguageDetection { patternsByLanguage = Collections.unmodifiableMap(patternsByLanguageBuilder); } - public String forcedLanguage() { + public String getForcedLanguage() { return forcedLanguage; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java index edfb8ce56a6..27c185fe545 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java @@ -102,7 +102,7 @@ public final class ScmPublisher { addIfNotEmpty(filesToBlame, f); } else { // File status is SAME so that mean fileData exists - FileData fileData = projectRepositories.fileData(inputModule.definition().getKeyWithBranch(), f.relativePath()); + FileData fileData = projectRepositories.fileData(inputModule.definition().getKeyWithBranch(), inputFile.getModuleRelativePath()); if (StringUtils.isEmpty(fileData.revision())) { addIfNotEmpty(filesToBlame, f); } else { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java index fe00b8e2913..c12c55cc820 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java @@ -324,7 +324,7 @@ public class DefaultSensorStorage implements SensorStorage { public static void validateCoverageMeasure(String value, InputFile inputFile) { Map<Integer, Integer> m = KeyValueFormat.parseIntInt(value); - validatePositiveLine(m, inputFile.absolutePath()); + validatePositiveLine(m, inputFile.toString()); validateMaxLine(m, inputFile); } @@ -333,7 +333,7 @@ public class DefaultSensorStorage implements SensorStorage { for (int line : m.keySet()) { if (line > maxLine) { - throw new IllegalStateException(String.format("Can't create measure for line %d for file '%s' with %d lines", line, inputFile.absolutePath(), maxLine)); + throw new IllegalStateException(String.format("Can't create measure for line %d for file '%s' with %d lines", line, inputFile, maxLine)); } } } @@ -364,7 +364,7 @@ public class DefaultSensorStorage implements SensorStorage { inputFile.setPublished(true); int componentRef = inputFile.batchId(); if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) { - throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile.absolutePath()); + throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile); } final ScannerReport.SyntaxHighlightingRule.Builder builder = ScannerReport.SyntaxHighlightingRule.newBuilder(); final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java index 5c0c0351fc2..8761acc1e5c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java @@ -125,10 +125,10 @@ public class BranchMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); + assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); // no branch in the report - DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("src/sample.xoo"); + DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("moduleA/src/sample.xoo"); assertThat(result.getReportReader().readComponent(inputfile.batchId()).getPath()).isEqualTo("src/sample.xoo"); // no branch in InputModule's key or in report @@ -145,7 +145,7 @@ public class BranchMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); + assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java index c42972232d4..e780fe99ad6 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java @@ -232,7 +232,7 @@ public class CoverageMediumTest { .build()) .execute(); - InputFile file1 = result.inputFile("src/sample1.xoo"); + InputFile file1 = result.inputFile("module1/src/sample1.xoo"); assertThat(result.coverageFor(file1, 1)).isNull(); assertThat(result.coverageFor(file1, 2).getHits()).isFalse(); @@ -242,7 +242,7 @@ public class CoverageMediumTest { assertThat(result.coverageFor(file1, 3).getHits()).isFalse(); assertThat(result.coverageFor(file1, 4)).isNull(); - InputFile file2 = result.inputFile("src/sample2.xoo"); + InputFile file2 = result.inputFile("module1/src/sample2.xoo"); assertThat(result.coverageFor(file2, 1)).isNull(); assertThat(result.coverageFor(file2, 2)).isNull(); assertThat(result.coverageFor(file2, 3)).isNull(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java index 00c1ca1042a..ed41c72973d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java @@ -142,8 +142,8 @@ public class CpdMediumTest { assertThat(result.inputFiles()).hasSize(2); - InputFile inputFile1 = result.inputFile("sample1.xoo"); - InputFile inputFile2 = result.inputFile("sample2.xoo"); + InputFile inputFile1 = result.inputFile("module1/sample1.xoo"); + InputFile inputFile2 = result.inputFile("module2/sample2.xoo"); // One clone group on each file List<org.sonar.scanner.protocol.output.ScannerReport.Duplication> duplicationGroupsFile1 = result.duplicationsFor(inputFile1); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java index bf86ea126c1..61fb4627b6d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java @@ -132,7 +132,7 @@ public class ProjectBuilderMediumTest { .put("sonar.xoo.enableProjectBuilder", "true") .build()) .execute(); - List<Issue> issues = result.issuesFor(result.inputFile("src/sample.xoo")); + List<Issue> issues = result.issuesFor(result.inputFile("module1/src/sample.xoo")); assertThat(issues).hasSize(10); assertThat(issues) @@ -181,7 +181,7 @@ public class ProjectBuilderMediumTest { .build()) .execute(); - List<Issue> issues = result.issuesFor(result.inputFile("src/sample.xoo")); + List<Issue> issues = result.issuesFor(result.inputFile("module1/src/sample.xoo")); assertThat(issues).hasSize(10); assertThat(issues) diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java index a6d208b35bb..ec448a90233 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java @@ -21,7 +21,7 @@ package org.sonar.scanner.phases; import org.junit.Before; import org.junit.Test; -import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.internal.MapSettings; @@ -41,7 +41,7 @@ public class CoverageExclusionsTest { @Test public void shouldExcludeFileBasedOnPattern() { - InputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build(); + DefaultInputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build(); settings.setProperty("sonar.coverage.exclusions", "src/org/polop/*"); coverageExclusions = new CoverageExclusions(settings.asConfig()); assertThat(coverageExclusions.isExcluded(file)).isTrue(); @@ -49,7 +49,7 @@ public class CoverageExclusionsTest { @Test public void shouldNotExcludeFileBasedOnPattern() { - InputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build(); + DefaultInputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build(); settings.setProperty("sonar.coverage.exclusions", "src/org/other/*"); coverageExclusions = new CoverageExclusions(settings.asConfig()); assertThat(coverageExclusions.isExcluded(file)).isFalse(); |