diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-06-04 15:43:44 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-07-12 20:21:13 +0200 |
commit | e020f1425d815ae48e9b5c39bc0eaf92382c3c06 (patch) | |
tree | 8de8c513fe61a8c78a5c7d99d71cefa82e1d9bdc /sonar-scanner-engine/src/main/java/org/sonar/scanner/fs | |
parent | 65c61f5006221de77fff2c314d07df8fb73ddc45 (diff) | |
download | sonarqube-e020f1425d815ae48e9b5c39bc0eaf92382c3c06.tar.gz sonarqube-e020f1425d815ae48e9b5c39bc0eaf92382c3c06.zip |
Extract implementation from plugin API - fix tests
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/fs')
10 files changed, 22 insertions, 941 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/AbstractProjectOrModule.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/AbstractProjectOrModule.java index 9fc2d130f4a..d458edd0ca9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/AbstractProjectOrModule.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/AbstractProjectOrModule.java @@ -34,6 +34,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.fs.internal.DefaultInputComponent; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultFileSystem.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultFileSystem.java index 1c1729634e1..de882ad0c54 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultFileSystem.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultFileSystem.java @@ -37,6 +37,7 @@ import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.PathUtils; import org.sonar.scanner.fs.predicates.DefaultFilePredicates; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultIndexedFile.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultIndexedFile.java deleted file mode 100644 index a3d060217e0..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultIndexedFile.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.fs; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Path; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; -import org.sonar.api.batch.fs.IndexedFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.utils.PathUtils; - -/** - * @since 6.3 - */ -@Immutable -public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile { - private final String projectRelativePath; - private final String moduleRelativePath; - private final String projectKey; - private final String language; - private final Type type; - private final Path absolutePath; - private final SensorStrategy sensorStrategy; - - /** - * Testing purposes only! - */ - public DefaultIndexedFile(String projectKey, Path baseDir, String relativePath, @Nullable String language) { - this(baseDir.resolve(relativePath), projectKey, relativePath, relativePath, Type.MAIN, language, TestInputFileBuilder.nextBatchId(), - new SensorStrategy()); - } - - public DefaultIndexedFile(Path absolutePath, String projectKey, String projectRelativePath, String moduleRelativePath, Type type, @Nullable String language, int batchId, - SensorStrategy sensorStrategy) { - super(batchId); - this.projectKey = projectKey; - this.projectRelativePath = PathUtils.sanitize(projectRelativePath); - this.moduleRelativePath = PathUtils.sanitize(moduleRelativePath); - this.type = type; - this.language = language; - this.sensorStrategy = sensorStrategy; - this.absolutePath = absolutePath; - } - - @Override - public String relativePath() { - return sensorStrategy.isGlobal() ? projectRelativePath : moduleRelativePath; - } - - public String getModuleRelativePath() { - return moduleRelativePath; - } - - public String getProjectRelativePath() { - return projectRelativePath; - } - - @Override - public String absolutePath() { - return PathUtils.sanitize(path().toString()); - } - - @Override - public File file() { - return path().toFile(); - } - - @Override - public Path path() { - return absolutePath; - } - - @Override - public InputStream inputStream() throws IOException { - return Files.newInputStream(path()); - } - - @CheckForNull - @Override - public String language() { - return language; - } - - @Override - public Type type() { - return type; - } - - /** - * Component key (without branch). - */ - @Override - public String key() { - return new StringBuilder().append(projectKey).append(":").append(projectRelativePath).toString(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (!(o instanceof DefaultIndexedFile)) { - return false; - } - - DefaultIndexedFile that = (DefaultIndexedFile) o; - return projectRelativePath.equals(that.projectRelativePath); - } - - @Override - public int hashCode() { - return projectRelativePath.hashCode(); - } - - @Override - public String toString() { - return projectRelativePath; - } - - @Override - public boolean isFile() { - return true; - } - - @Override - public String filename() { - return path().getFileName().toString(); - } - - @Override - public URI uri() { - return path().toUri(); - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputComponent.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputComponent.java deleted file mode 100644 index 11601e0265f..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputComponent.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.fs; - -import java.util.HashSet; -import java.util.Set; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.measure.Metric; - -/** - * @since 5.2 - */ -public abstract class DefaultInputComponent implements InputComponent { - private int id; - private Set<String> storedMetricKeys = new HashSet<>(); - - public DefaultInputComponent(int scannerId) { - this.id = scannerId; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || this.getClass() != o.getClass()) { - return false; - } - - DefaultInputComponent that = (DefaultInputComponent) o; - return key().equals(that.key()); - } - - public int scannerId() { - return id; - } - - @Override - public int hashCode() { - return key().hashCode(); - } - - @Override - public String toString() { - return "[key=" + key() + "]"; - } - - public void setHasMeasureFor(Metric metric) { - storedMetricKeys.add(metric.key()); - } - - public boolean hasMeasureFor(Metric metric) { - return storedMetricKeys.contains(metric.key()); - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputDir.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputDir.java deleted file mode 100644 index 13eb77be1c4..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputDir.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.fs; - -import java.io.File; -import java.net.URI; -import java.nio.file.Path; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.utils.PathUtils; - -/** - * @since 4.5 - */ -public class DefaultInputDir extends DefaultInputComponent implements InputDir { - - private final String relativePath; - private final String moduleKey; - private Path moduleBaseDir; - - public DefaultInputDir(String moduleKey, String relativePath) { - super(-1); - this.moduleKey = moduleKey; - this.relativePath = PathUtils.sanitize(relativePath); - } - - @Override - public String relativePath() { - return relativePath; - } - - @Override - public String absolutePath() { - return PathUtils.sanitize(path().toString()); - } - - @Override - public File file() { - return path().toFile(); - } - - @Override - public Path path() { - if (moduleBaseDir == null) { - throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))"); - } - return moduleBaseDir.resolve(relativePath); - } - - public String moduleKey() { - return moduleKey; - } - - @Override - public String key() { - StringBuilder sb = new StringBuilder().append(moduleKey).append(":"); - if (StringUtils.isEmpty(relativePath)) { - sb.append("/"); - } else { - sb.append(relativePath); - } - return sb.toString(); - } - - /** - * For testing purpose. Will be automatically set when dir is added to {@link DefaultFileSystem} - */ - public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) { - this.moduleBaseDir = moduleBaseDir.normalize(); - return this; - } - - @Override - public boolean isFile() { - return false; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || this.getClass() != o.getClass()) { - return false; - } - - DefaultInputDir that = (DefaultInputDir) o; - return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath); - } - - @Override - public int hashCode() { - return moduleKey.hashCode() + relativePath.hashCode() * 13; - } - - @Override - public String toString() { - return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; - } - - @Override - public URI uri() { - return path().toUri(); - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputFile.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputFile.java deleted file mode 100644 index f29f11db64a..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/DefaultInputFile.java +++ /dev/null @@ -1,440 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.fs; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.BitSet; -import java.util.Collection; -import java.util.Optional; -import java.util.Set; -import java.util.function.Consumer; -import java.util.stream.Collectors; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.io.ByteOrderMark; -import org.apache.commons.io.input.BOMInputStream; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.Metadata; - -import static org.sonar.api.utils.Preconditions.checkArgument; -import static org.sonar.api.utils.Preconditions.checkState; - -/** - * @since 4.2 - * To create {@link InputFile} in tests, use {@link TestInputFileBuilder}. - */ -public class DefaultInputFile extends DefaultInputComponent implements InputFile { - - private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; - - private final DefaultIndexedFile indexedFile; - private final String contents; - private final Consumer<DefaultInputFile> metadataGenerator; - - private boolean published; - private boolean excludedForCoverage; - private boolean excludedForDuplication; - private boolean ignoreAllIssues; - // Lazy init to save memory - private BitSet noSonarLines; - private Status status; - private Charset charset; - private Metadata metadata; - private Collection<int[]> ignoreIssuesOnlineRanges; - private BitSet executableLines; - - public DefaultInputFile(DefaultIndexedFile indexedFile, Consumer<DefaultInputFile> metadataGenerator) { - this(indexedFile, metadataGenerator, null); - } - - // For testing - public DefaultInputFile(DefaultIndexedFile indexedFile, Consumer<DefaultInputFile> metadataGenerator, @Nullable String contents) { - super(indexedFile.scannerId()); - this.indexedFile = indexedFile; - this.metadataGenerator = metadataGenerator; - this.metadata = null; - this.published = false; - this.excludedForCoverage = false; - this.contents = contents; - } - - public void checkMetadata() { - if (metadata == null) { - metadataGenerator.accept(this); - } - } - - @Override - public InputStream inputStream() throws IOException { - return contents != null ? new ByteArrayInputStream(contents.getBytes(charset())) - : new BOMInputStream(Files.newInputStream(path()), - ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); - } - - @Override - public String contents() throws IOException { - if (contents != null) { - return contents; - } else { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - try (InputStream inputStream = inputStream()) { - byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; - int length; - while ((length = inputStream.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - } - return result.toString(charset().name()); - } - } - - public DefaultInputFile setPublished(boolean published) { - this.published = published; - return this; - } - - public boolean isPublished() { - return published; - } - - public DefaultInputFile setExcludedForCoverage(boolean excludedForCoverage) { - this.excludedForCoverage = excludedForCoverage; - return this; - } - - public boolean isExcludedForCoverage() { - return excludedForCoverage; - } - - public DefaultInputFile setExcludedForDuplication(boolean excludedForDuplication) { - this.excludedForDuplication = excludedForDuplication; - return this; - } - - public boolean isExcludedForDuplication() { - return excludedForDuplication; - } - - /** - * @deprecated since 6.6 - */ - @Deprecated - @Override - public String relativePath() { - return indexedFile.relativePath(); - } - - public String getModuleRelativePath() { - return indexedFile.getModuleRelativePath(); - } - - public String getProjectRelativePath() { - return indexedFile.getProjectRelativePath(); - } - - @Override - public String absolutePath() { - return indexedFile.absolutePath(); - } - - @Override - public File file() { - return indexedFile.file(); - } - - @Override - public Path path() { - return indexedFile.path(); - } - - @CheckForNull - @Override - public String language() { - return indexedFile.language(); - } - - @Override - public Type type() { - return indexedFile.type(); - } - - /** - * Component key (without branch). - */ - @Override - public String key() { - return indexedFile.key(); - } - - @Override - public int hashCode() { - return indexedFile.hashCode(); - } - - @Override - public String toString() { - return indexedFile.toString(); - } - - /** - * {@link #setStatus(Status)} - */ - @Override - public Status status() { - checkMetadata(); - return status; - } - - @Override - public int lines() { - checkMetadata(); - return metadata.lines(); - } - - @Override - public boolean isEmpty() { - checkMetadata(); - return metadata.isEmpty(); - } - - @Override - public Charset charset() { - checkMetadata(); - return charset; - } - - public int lastValidOffset() { - checkMetadata(); - return metadata.lastValidOffset(); - } - - /** - * Digest hash of the file. - */ - public String hash() { - checkMetadata(); - return metadata.hash(); - } - - public int nonBlankLines() { - checkMetadata(); - return metadata.nonBlankLines(); - } - - public int[] originalLineStartOffsets() { - checkMetadata(); - checkState(metadata.originalLineStartOffsets() != null, "InputFile is not properly initialized."); - checkState(metadata.originalLineStartOffsets().length == metadata.lines(), - "InputFile is not properly initialized. 'originalLineStartOffsets' property length should be equal to 'lines'"); - return metadata.originalLineStartOffsets(); - } - - public int[] originalLineEndOffsets() { - checkMetadata(); - checkState(metadata.originalLineEndOffsets() != null, "InputFile is not properly initialized."); - checkState(metadata.originalLineEndOffsets().length == metadata.lines(), - "InputFile is not properly initialized. 'originalLineEndOffsets' property length should be equal to 'lines'"); - return metadata.originalLineEndOffsets(); - } - - @Override - public TextPointer newPointer(int line, int lineOffset) { - checkMetadata(); - DefaultTextPointer textPointer = new DefaultTextPointer(line, lineOffset); - checkValid(textPointer, "pointer"); - return textPointer; - } - - @Override - public TextRange newRange(TextPointer start, TextPointer end) { - checkMetadata(); - checkValid(start, "start pointer"); - checkValid(end, "end pointer"); - return newRangeValidPointers(start, end, false); - } - - @Override - public TextRange newRange(int startLine, int startLineOffset, int endLine, int endLineOffset) { - checkMetadata(); - TextPointer start = newPointer(startLine, startLineOffset); - TextPointer end = newPointer(endLine, endLineOffset); - return newRangeValidPointers(start, end, false); - } - - @Override - public TextRange selectLine(int line) { - checkMetadata(); - TextPointer startPointer = newPointer(line, 0); - TextPointer endPointer = newPointer(line, lineLength(line)); - return newRangeValidPointers(startPointer, endPointer, true); - } - - public void validate(TextRange range) { - checkMetadata(); - checkValid(range.start(), "start pointer"); - checkValid(range.end(), "end pointer"); - } - - /** - * Create Range from global offsets. Used for backward compatibility with older API. - */ - public TextRange newRange(int startOffset, int endOffset) { - checkMetadata(); - return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset), false); - } - - public TextPointer newPointer(int globalOffset) { - checkMetadata(); - checkArgument(globalOffset >= 0, "%s is not a valid offset for a file", globalOffset); - checkArgument(globalOffset <= lastValidOffset(), "%s is not a valid offset for file %s. Max offset is %s", globalOffset, this, lastValidOffset()); - int line = findLine(globalOffset); - int startLineOffset = originalLineStartOffsets()[line - 1]; - // In case the global offset is between \r and \n, move the pointer to a valid location - return new DefaultTextPointer(line, Math.min(globalOffset, originalLineEndOffsets()[line - 1]) - startLineOffset); - } - - public DefaultInputFile setStatus(Status status) { - this.status = status; - return this; - } - - public DefaultInputFile setCharset(Charset charset) { - this.charset = charset; - return this; - } - - private void checkValid(TextPointer pointer, String owner) { - checkArgument(pointer.line() >= 1, "%s is not a valid line for a file", pointer.line()); - checkArgument(pointer.line() <= this.metadata.lines(), "%s is not a valid line for %s. File %s has %s line(s)", pointer.line(), owner, this, metadata.lines()); - checkArgument(pointer.lineOffset() >= 0, "%s is not a valid line offset for a file", pointer.lineOffset()); - int lineLength = lineLength(pointer.line()); - checkArgument(pointer.lineOffset() <= lineLength, - "%s is not a valid line offset for %s. File %s has %s character(s) at line %s", pointer.lineOffset(), owner, this, lineLength, pointer.line()); - } - - private int lineLength(int line) { - return originalLineEndOffsets()[line - 1] - originalLineStartOffsets()[line - 1]; - } - - private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) { - checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0), - "Start pointer %s should be before end pointer %s", start, end); - return new DefaultTextRange(start, end); - } - - private int findLine(int globalOffset) { - return Math.abs(Arrays.binarySearch(originalLineStartOffsets(), globalOffset) + 1); - } - - public DefaultInputFile setMetadata(Metadata metadata) { - this.metadata = metadata; - return this; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - if (this.getClass() != obj.getClass()) { - return false; - } - - DefaultInputFile that = (DefaultInputFile) obj; - return this.getProjectRelativePath().equals(that.getProjectRelativePath()); - } - - @Override - public boolean isFile() { - return true; - } - - @Override - public String filename() { - return indexedFile.filename(); - } - - @Override - public URI uri() { - return indexedFile.uri(); - } - - public void noSonarAt(Set<Integer> noSonarLines) { - if (this.noSonarLines == null) { - this.noSonarLines = new BitSet(lines()); - } - noSonarLines.forEach(l -> this.noSonarLines.set(l - 1)); - } - - public boolean hasNoSonarAt(int line) { - if (this.noSonarLines == null) { - return false; - } - return this.noSonarLines.get(line - 1); - } - - public boolean isIgnoreAllIssues() { - return ignoreAllIssues; - } - - public void setIgnoreAllIssues(boolean ignoreAllIssues) { - this.ignoreAllIssues = ignoreAllIssues; - } - - public void addIgnoreIssuesOnLineRanges(Collection<int[]> lineRanges) { - if (this.ignoreIssuesOnlineRanges == null) { - this.ignoreIssuesOnlineRanges = new ArrayList<>(); - } - this.ignoreIssuesOnlineRanges.addAll(lineRanges); - } - - public boolean isIgnoreAllIssuesOnLine(@Nullable Integer line) { - if (line == null || ignoreIssuesOnlineRanges == null) { - return false; - } - return ignoreIssuesOnlineRanges.stream().anyMatch(r -> r[0] <= line && line <= r[1]); - } - - public void setExecutableLines(Set<Integer> executableLines) { - checkState(this.executableLines == null, "Executable lines have already been saved for file: {}", this.toString()); - this.executableLines = new BitSet(lines()); - executableLines.forEach(l -> this.executableLines.set(l - 1)); - } - - public Optional<Set<Integer>> getExecutableLines() { - if (this.executableLines == null) { - return Optional.empty(); - } - return Optional.of(this.executableLines.stream().map(i -> i + 1).boxed().collect(Collectors.toSet())); - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/PathPattern.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/PathPattern.java deleted file mode 100644 index 5d5947d9927..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/PathPattern.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.fs; - -import java.nio.file.Path; -import javax.annotation.concurrent.ThreadSafe; -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.utils.PathUtils; -import org.sonar.api.utils.WildcardPattern; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -@ThreadSafe -public abstract class PathPattern { - - private static final Logger LOG = Loggers.get(PathPattern.class); - - /** - * @deprecated since 6.6 - */ - @Deprecated - private static final String ABSOLUTE_PATH_PATTERN_PREFIX = "file:"; - final WildcardPattern pattern; - - PathPattern(String pattern) { - this.pattern = WildcardPattern.create(pattern); - } - - public abstract boolean match(Path absolutePath, Path relativePath); - - public abstract boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension); - - public static PathPattern create(String s) { - String trimmed = StringUtils.trim(s); - if (StringUtils.startsWithIgnoreCase(trimmed, ABSOLUTE_PATH_PATTERN_PREFIX)) { - LOG.warn("Using absolute path pattern is deprecated. Please use relative path instead of '" + trimmed + "'"); - return new AbsolutePathPattern(StringUtils.substring(trimmed, ABSOLUTE_PATH_PATTERN_PREFIX.length())); - } - return new RelativePathPattern(trimmed); - } - - public static PathPattern[] create(String[] s) { - PathPattern[] result = new PathPattern[s.length]; - for (int i = 0; i < s.length; i++) { - result[i] = create(s[i]); - } - return result; - } - - /** - * @deprecated since 6.6 - */ - @Deprecated - private static class AbsolutePathPattern extends PathPattern { - private AbsolutePathPattern(String pattern) { - super(pattern); - } - - @Override - public boolean match(Path absolutePath, Path relativePath) { - return match(absolutePath, relativePath, true); - } - - @Override - public boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension) { - String path = PathUtils.sanitize(absolutePath.toString()); - if (!caseSensitiveFileExtension) { - String extension = sanitizeExtension(FilenameUtils.getExtension(path)); - if (StringUtils.isNotBlank(extension)) { - path = StringUtils.removeEndIgnoreCase(path, extension); - path = path + extension; - } - } - return pattern.match(path); - } - - @Override - public String toString() { - return ABSOLUTE_PATH_PATTERN_PREFIX + pattern.toString(); - } - } - - /** - * Path relative to module basedir - */ - private static class RelativePathPattern extends PathPattern { - private RelativePathPattern(String pattern) { - super(pattern); - } - - @Override - public boolean match(Path absolutePath, Path relativePath) { - return match(absolutePath, relativePath, true); - } - - @Override - public boolean match(Path absolutePath, Path relativePath, boolean caseSensitiveFileExtension) { - String path = PathUtils.sanitize(relativePath.toString()); - if (!caseSensitiveFileExtension) { - String extension = sanitizeExtension(FilenameUtils.getExtension(path)); - if (StringUtils.isNotBlank(extension)) { - path = StringUtils.removeEndIgnoreCase(path, extension); - path = path + extension; - } - } - return path != null && pattern.match(path); - } - - @Override - public String toString() { - return pattern.toString(); - } - } - - static String sanitizeExtension(String suffix) { - return StringUtils.lowerCase(StringUtils.removeStart(suffix, ".")); - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/TestInputFileBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/TestInputFileBuilder.java index 5d6e8b9bc9c..eee66064b29 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/TestInputFileBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/TestInputFileBuilder.java @@ -31,7 +31,14 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; +import org.sonar.api.batch.fs.internal.DefaultIndexedFile; +import org.sonar.api.batch.fs.internal.DefaultInputDir; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.Metadata; +import org.sonar.api.batch.fs.internal.SensorStrategy; import org.sonar.api.utils.PathUtils; /** @@ -211,14 +218,14 @@ public class TestInputFileBuilder { return setMetadata(new FileMetadata().readMetadata(new StringReader(content))); } - public DefaultInputFile build() { + public org.sonar.api.batch.fs.internal.DefaultInputFile build() { Path absolutePath = moduleBaseDir.resolve(relativePath); if (projectBaseDir == null) { projectBaseDir = moduleBaseDir; } String projectRelativePath = projectBaseDir.relativize(absolutePath).toString(); - DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, projectKey, projectRelativePath, relativePath, type, language, id, new SensorStrategy()); - DefaultInputFile inputFile = new DefaultInputFile(indexedFile, + org.sonar.api.batch.fs.internal.DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, projectKey, projectRelativePath, relativePath, type, language, id, new SensorStrategy()); + org.sonar.api.batch.fs.internal.DefaultInputFile inputFile = new org.sonar.api.batch.fs.internal.DefaultInputFile(indexedFile, f -> f.setMetadata(new Metadata(lines, nonBlankLines, hash, originalLineStartOffsets, originalLineEndOffsets, lastValidOffset)), contents); inputFile.setStatus(status); @@ -227,7 +234,7 @@ public class TestInputFileBuilder { return inputFile; } - public static DefaultInputModule newDefaultInputModule(String moduleKey, File baseDir) { + public static org.sonar.api.batch.fs.internal.DefaultInputModule newDefaultInputModule(String moduleKey, File baseDir) { ProjectDefinition definition = ProjectDefinition.create() .setKey(moduleKey) .setBaseDir(baseDir) @@ -235,17 +242,17 @@ public class TestInputFileBuilder { return newDefaultInputModule(definition); } - public static DefaultInputModule newDefaultInputModule(ProjectDefinition projectDefinition) { - return new DefaultInputModule(projectDefinition, TestInputFileBuilder.nextBatchId()); + public static org.sonar.api.batch.fs.internal.DefaultInputModule newDefaultInputModule(ProjectDefinition projectDefinition) { + return new org.sonar.api.batch.fs.internal.DefaultInputModule(projectDefinition, TestInputFileBuilder.nextBatchId()); } - public static DefaultInputModule newDefaultInputModule(AbstractProjectOrModule parent, String key) throws IOException { + public static DefaultInputModule newDefaultInputModule(org.sonar.api.batch.fs.internal.AbstractProjectOrModule parent, String key) throws IOException { Path basedir = parent.getBaseDir().resolve(key); Files.createDirectory(basedir); return newDefaultInputModule(key, basedir.toFile()); } - public static DefaultInputProject newDefaultInputProject(String projectKey, File baseDir) { + public static org.sonar.api.batch.fs.internal.DefaultInputProject newDefaultInputProject(String projectKey, File baseDir) { ProjectDefinition definition = ProjectDefinition.create() .setKey(projectKey) .setBaseDir(baseDir) @@ -253,8 +260,8 @@ public class TestInputFileBuilder { return newDefaultInputProject(definition); } - public static DefaultInputProject newDefaultInputProject(ProjectDefinition projectDefinition) { - return new DefaultInputProject(projectDefinition, TestInputFileBuilder.nextBatchId()); + public static org.sonar.api.batch.fs.internal.DefaultInputProject newDefaultInputProject(ProjectDefinition projectDefinition) { + return new org.sonar.api.batch.fs.internal.DefaultInputProject(projectDefinition, TestInputFileBuilder.nextBatchId()); } public static DefaultInputProject newDefaultInputProject(String key, Path baseDir) throws IOException { @@ -262,7 +269,7 @@ public class TestInputFileBuilder { return newDefaultInputProject(key, baseDir.toFile()); } - public static DefaultInputDir newDefaultInputDir(AbstractProjectOrModule module, String relativePath) throws IOException { + public static org.sonar.api.batch.fs.internal.DefaultInputDir newDefaultInputDir(org.sonar.api.batch.fs.internal.AbstractProjectOrModule module, String relativePath) throws IOException { Path basedir = module.getBaseDir().resolve(relativePath); Files.createDirectory(basedir); return new DefaultInputDir(module.key(), relativePath) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/DefaultFilePredicates.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/DefaultFilePredicates.java index 60bb37e9896..bcda1ac7351 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/DefaultFilePredicates.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/DefaultFilePredicates.java @@ -30,7 +30,7 @@ import org.sonar.api.batch.fs.FilePredicate; import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; -import org.sonar.scanner.fs.PathPattern; +import org.sonar.api.batch.fs.internal.PathPattern; /** * Factory of {@link FilePredicate} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/PathPatternPredicate.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/PathPatternPredicate.java index 0fdabcbcd31..032d820919b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/PathPatternPredicate.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/fs/predicates/PathPatternPredicate.java @@ -21,7 +21,7 @@ package org.sonar.scanner.fs.predicates; import java.nio.file.Paths; import org.sonar.api.batch.fs.InputFile; -import org.sonar.scanner.fs.PathPattern; +import org.sonar.api.batch.fs.internal.PathPattern; /** * @since 4.2 |