diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-22 09:27:34 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-22 12:35:56 +0200 |
commit | a1defc3dd106409930d9323d7bc055f816c2bcd3 (patch) | |
tree | f07e884153f38592a4359d9a2be3cf6270c2ab1c /sonar-plugin-api | |
parent | 6d5f68a9de9c3c0365921ad95304ea7051640c7b (diff) | |
download | sonarqube-a1defc3dd106409930d9323d7bc055f816c2bcd3.tar.gz sonarqube-a1defc3dd106409930d9323d7bc055f816c2bcd3.zip |
SONAR-5389 Add InputDir concept in batch API
Diffstat (limited to 'sonar-plugin-api')
6 files changed, 255 insertions, 2 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 56fdc0320f5..0c285f686e2 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 @@ -96,6 +96,16 @@ public interface FileSystem extends BatchComponent { InputFile inputFile(FilePredicate predicate); /** + * Returns {@link InputDir} matching the current {@link File}. + * @return null if directory is not indexed. + * @throw {@link IllegalArgumentException} is File is null or not a directory. + * + * @since 4.5 + */ + @CheckForNull + InputDir inputDir(File dir); + + /** * Input files matching the given attributes. Return all the files if the parameter * <code>attributes</code> is empty. * <p/> diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputDir.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputDir.java new file mode 100644 index 00000000000..d120c609e4e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputDir.java @@ -0,0 +1,61 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.batch.fs; + +import java.io.File; + +/** + * Layer over {@link java.io.File} for directories. + * + * @since 4.5 + */ +public interface InputDir extends InputPath { + + /** + * Path relative to module base directory. Path is unique and identifies directory + * within given <code>{@link FileSystem}</code>. File separator is the forward + * slash ('/'), even on Microsoft Windows. + * <p/> + * Returns <code>src/main/java/com</code> if module base dir is + * <code>/path/to/module</code> and if directory is + * <code>/path/to/module/src/main/java/com</code>. + * <p/> + * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo'). + */ + @Override + String relativePath(); + + /** + * Normalized absolute path. File separator is forward slash ('/'), even on Microsoft Windows. + * <p/> + * This is not canonical path. Symbolic links are not resolved. For example if /project/src links + * to /tmp/src and basedir is /project, then this method returns /project/src. Use + * {@code file().getCanonicalPath()} to resolve symbolic link. + */ + @Override + String absolutePath(); + + /** + * The underlying absolute {@link java.io.File} + */ + @Override + File file(); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java index 83e9697f15f..32ea0e3cf2e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java @@ -20,14 +20,13 @@ package org.sonar.api.batch.fs; import java.io.File; -import java.io.Serializable; /** * This layer over {@link java.io.File} adds information for code analyzers. * * @since 4.2 */ -public interface InputFile extends Serializable { +public interface InputFile extends InputPath { enum Type { MAIN, TEST @@ -51,6 +50,7 @@ public interface InputFile extends Serializable { * <p/> * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo'). */ + @Override String relativePath(); /** @@ -60,11 +60,13 @@ public interface InputFile extends Serializable { * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use * {@code file().getCanonicalPath()} to resolve symbolic link. */ + @Override String absolutePath(); /** * The underlying absolute {@link java.io.File} */ + @Override File file(); /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java new file mode 100644 index 00000000000..6a3ac3a0bf2 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.batch.fs; + +import java.io.File; +import java.io.Serializable; + +/** + * Layer over {@link java.io.File} for files or directories. + * + * @since 4.5 + */ +public interface InputPath extends Serializable { + + /** + * @see InputFile#relativePath() + * @see InputDir#relativePath() + */ + String relativePath(); + + /** + * @see InputFile#absolutePath() + * @see InputDir#absolutePath() + */ + String absolutePath(); + + /** + * @see InputFile#file() + * @see InputDir#file() + */ + File file(); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java index 2002c6feb1f..7b79d948d10 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java @@ -19,10 +19,13 @@ */ package org.sonar.api.batch.fs.internal; +import org.sonar.api.utils.PathUtils; + import com.google.common.base.Preconditions; import org.sonar.api.batch.fs.FilePredicate; 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 javax.annotation.CheckForNull; @@ -155,6 +158,12 @@ public class DefaultFileSystem implements FileSystem { return result; } + @Override + public InputDir inputDir(File dir) { + doPreloadFiles(); + return cache.inputDir(PathUtils.sanitize(new RelativeP)); + } + public static Collection<InputFile> filter(Iterable<InputFile> target, FilePredicate predicate) { Collection<InputFile> result = new ArrayList<InputFile>(); for (InputFile element : target) { @@ -210,6 +219,9 @@ public class DefaultFileSystem implements FileSystem { @CheckForNull protected abstract InputFile inputFile(RelativePathPredicate predicate); + @CheckForNull + protected abstract InputDir inputDir(String relativePath); + protected abstract void doAdd(InputFile inputFile); final void add(InputFile inputFile) { @@ -222,6 +234,7 @@ public class DefaultFileSystem implements FileSystem { */ private static class MapCache extends Cache { private final Map<String, InputFile> fileMap = new HashMap<String, InputFile>(); + private final Map<String, InputDir> dirMap = new HashMap<String, InputDir>(); @Override public Iterable<InputFile> inputFiles() { @@ -234,6 +247,11 @@ public class DefaultFileSystem implements FileSystem { } @Override + protected InputDir inputDir(String relativePath) { + return dirMap.get(relativePath); + } + + @Override protected void doAdd(InputFile inputFile) { fileMap.put(inputFile.relativePath(), inputFile); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java new file mode 100644 index 00000000000..26fc37a0030 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java @@ -0,0 +1,112 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.batch.fs.internal; + +import org.sonar.api.batch.fs.InputDir; +import org.sonar.api.utils.PathUtils; + +import javax.annotation.CheckForNull; + +import java.io.File; +import java.io.Serializable; + +/** + * @since 4.5 + */ +public class DefaultInputDir implements InputDir, Serializable { + + private final String relativePath; + private String absolutePath; + private String key; + + public DefaultInputDir(String relativePath) { + this.relativePath = PathUtils.sanitize(relativePath); + } + + @Override + public String relativePath() { + return relativePath; + } + + /** + * Marked as nullable just for the unit tests that do not call {@link #setFile(java.io.File)} + * previously. + */ + @Override + @CheckForNull + public String absolutePath() { + return absolutePath; + } + + @Override + public File file() { + if (absolutePath == null) { + throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))"); + } + return new File(absolutePath); + } + + /** + * Component key. It's marked as nullable just for the unit tests that + * do not previously call {@link #setKey(String)}. + */ + @CheckForNull + public String key() { + return key; + } + + public DefaultInputDir setAbsolutePath(String s) { + this.absolutePath = PathUtils.sanitize(s); + return this; + } + + public DefaultInputDir setFile(File file) { + setAbsolutePath(file.getAbsolutePath()); + return this; + } + + public DefaultInputDir setKey(String s) { + this.key = s; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof DefaultInputDir)) { + return false; + } + + DefaultInputDir that = (DefaultInputDir) o; + return relativePath.equals(that.relativePath); + } + + @Override + public int hashCode() { + return relativePath.hashCode(); + } + + @Override + public String toString() { + return "[relative=" + relativePath + ", abs=" + absolutePath + "]"; + } +} |