diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-31 15:26:06 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-31 15:26:53 +0100 |
commit | 3d36db1e80ab9f2c05610b5d2d6d3c7dd7ad4025 (patch) | |
tree | 56004569511edd88ac0c1714b49a68fbee2bdcde /sonar-plugin-api/src/main/java/org/sonar/api/resources | |
parent | 6c56f137f465db11cffcbb02aa57eb3e2518e9d9 (diff) | |
download | sonarqube-3d36db1e80ab9f2c05610b5d2d6d3c7dd7ad4025.tar.gz sonarqube-3d36db1e80ab9f2c05610b5d2d6d3c7dd7ad4025.zip |
SONAR-926 Add a fromIOFile method on Directory to be consistent with File
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar/api/resources')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java | 28 | ||||
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java | 7 |
2 files changed, 27 insertions, 8 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java index d3ea1c9e17b..d916dcec556 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java @@ -22,8 +22,11 @@ package org.sonar.api.resources; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; import org.sonar.api.batch.SensorContext; +import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.WildcardPattern; +import javax.annotation.CheckForNull; + /** * @since 1.10 * Extends JavaPackage to allow smooth migration from JavaPackage to Directory @@ -38,7 +41,7 @@ public class Directory extends JavaPackage { } /** - * @deprecated since 4.2 use {@link #create(String, String)} + * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} */ @Deprecated public Directory(String relativePathFromSourceDir) { @@ -46,7 +49,7 @@ public class Directory extends JavaPackage { } /** - * @deprecated since 4.2 use {@link #create(String, String)} + * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} */ @Deprecated public Directory(String relativePathFromSourceDir, Language language) { @@ -107,12 +110,29 @@ public class Directory extends JavaPackage { } /** + * Creates a {@link Directory} from an absolute {@link java.io.File} and a module. + * The returned {@link Directory} can be then passed for example to + * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}. + * @param dir absolute path to a directory + * @param module + * @return null if the directory is not under module basedir. + * @since 4.2 + */ + @CheckForNull + public static Directory fromIOFile(java.io.File dir, Project module) { + String relativePathFromBasedir = new PathResolver().relativePath(module.getFileSystem().getBasedir(), dir); + if (relativePathFromBasedir != null) { + return Directory.create(relativePathFromBasedir); + } + return null; + } + + /** * Create a Directory that is partially initialized. But that's enough to call for example * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed. * Internal use only. - * @since 4.2 */ - public static Directory create(String relativePathFromBaseDir) { + static Directory create(String relativePathFromBaseDir) { Directory d = new Directory(); String normalizedPath = normalize(relativePathFromBaseDir); d.setKey(normalizedPath); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java index 013b1236adb..57c4f585c2c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java @@ -156,8 +156,8 @@ public class File extends Resource { } /** - * Creates a SonarQube File from an absolute Java IO File and a module. - * The returned SonarQube File can be then passed for example to + * Creates a {@link File} from an absolute {@link java.io.File} and a module. + * The returned {@link File} can be then passed for example to * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}. * @param file absolute path to a file * @param module @@ -243,9 +243,8 @@ public class File extends Resource { * Create a File that is partially initialized. But that's enough to call for example * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed. * Internal use only. - * @since 4.2 */ - public static File create(String relativePathFromBasedir) { + private static File create(String relativePathFromBasedir) { File file = new File(); String normalizedPath = normalize(relativePathFromBasedir); file.setKey(normalizedPath); |