diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-13 20:49:01 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-13 20:49:54 +0100 |
commit | 808e4f107fdd1659a41bb616660090fc04a9d7c0 (patch) | |
tree | e3b1c3fa12d35d90909dbe77f1c6722796b15885 /sonar-plugin-api/src/main/java/org/sonar/api/resources | |
parent | 256c992dd006ded2b7e2a84e0a5a7dfb8be16fe7 (diff) | |
download | sonarqube-808e4f107fdd1659a41bb616660090fc04a9d7c0.tar.gz sonarqube-808e4f107fdd1659a41bb616660090fc04a9d7c0.zip |
SONAR-3024 Introduce deprecated key on resources to ease transition
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar/api/resources')
5 files changed, 243 insertions, 86 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 98f9c386bf8..1bb04d84886 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 @@ -33,18 +33,30 @@ public class Directory extends Resource { private Language language; - public Directory(String key) { - this(key, null); + private Directory() { + // USed by factory } - public Directory(String key, Language language) { - setKey(parseKey(key)); + /** + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} + */ + @Deprecated + public Directory(String deprecatedKey) { + this(deprecatedKey, null); + } + + /** + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} + */ + @Deprecated + public Directory(String deprecatedKey, Language language) { + setDeprecatedKey(parseKey(deprecatedKey)); this.language = language; } @Override public String getName() { - return getKey(); + return getDeprecatedKey(); } @Override @@ -95,6 +107,15 @@ public class Directory extends Resource { return key; } + public static Directory create(String path, String directoryDeprecatedKey) { + Directory d = new Directory(); + String normalizedPath = normalize(path); + d.setKey(normalizedPath); + d.setDeprecatedKey(parseKey(directoryDeprecatedKey)); + d.setPath(normalizedPath); + return d; + } + @Override public String toString() { return new ToStringBuilder(this) @@ -103,4 +124,5 @@ public class Directory extends Resource { .append("language", language) .toString(); } + } 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 84d2d34ce77..75adc4cb1b3 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 @@ -35,58 +35,70 @@ public class File extends Resource { public static final String SCOPE = Scopes.FILE; - private String directoryKey; + private String directoryDeprecatedKey; private String filename; private Language language; private Directory parent; private String qualifier = Qualifiers.FILE; + private File() { + // Used by factory method + } + /** * File in project. Key is the path relative to project source directories. It is not the absolute path and it does not include the path * to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The absolute path may be * c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir is src/main/sql. + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ - public File(String key) { - if (key == null) { + @Deprecated + public File(String deprecatedKey) { + if (deprecatedKey == null) { throw new IllegalArgumentException("File key is null"); } - String realKey = parseKey(key); + String realKey = parseKey(deprecatedKey); if (realKey.indexOf(Directory.SEPARATOR) >= 0) { - this.directoryKey = Directory.parseKey(StringUtils.substringBeforeLast(key, Directory.SEPARATOR)); + this.directoryDeprecatedKey = Directory.parseKey(StringUtils.substringBeforeLast(deprecatedKey, Directory.SEPARATOR)); this.filename = StringUtils.substringAfterLast(realKey, Directory.SEPARATOR); - realKey = new StringBuilder().append(this.directoryKey).append(Directory.SEPARATOR).append(filename).toString(); + realKey = new StringBuilder().append(this.directoryDeprecatedKey).append(Directory.SEPARATOR).append(filename).toString(); } else { - this.filename = key; + this.filename = deprecatedKey; } - setKey(realKey); + setDeprecatedKey(realKey); } /** * Creates a file from its containing directory and name + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ - public File(String directory, String filename) { + @Deprecated + public File(String deprecatedDirectoryKey, String filename) { this.filename = StringUtils.trim(filename); - if (StringUtils.isBlank(directory)) { - setKey(filename); + if (StringUtils.isBlank(deprecatedDirectoryKey)) { + setDeprecatedKey(filename); } else { - this.directoryKey = Directory.parseKey(directory); - setKey(new StringBuilder().append(directoryKey).append(Directory.SEPARATOR).append(this.filename).toString()); + this.directoryDeprecatedKey = Directory.parseKey(deprecatedDirectoryKey); + setDeprecatedKey(new StringBuilder().append(directoryDeprecatedKey).append(Directory.SEPARATOR).append(this.filename).toString()); } } /** * Creates a File from its language and its key + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ - public File(Language language, String key) { - this(key); + @Deprecated + public File(Language language, String deprecatedKey) { + this(deprecatedKey); this.language = language; } /** * Creates a File from language, directory and filename + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ + @Deprecated public File(Language language, String directory, String filename) { this(directory, filename); this.language = language; @@ -100,11 +112,7 @@ public class File extends Resource { @Override public Directory getParent() { if (parent == null) { - parent = new Directory(directoryKey); - String filePath = getPath(); - if (StringUtils.isNotBlank(filePath)) { - parent.setPath(StringUtils.substringBeforeLast(filePath, Directory.SEPARATOR)); - } + parent = new Directory(directoryDeprecatedKey); } return parent; } @@ -126,13 +134,15 @@ public class File extends Resource { */ @Override public boolean matchFilePattern(String antPattern) { - WildcardPattern matcher = WildcardPattern.create(antPattern, "/"); + WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR); return matcher.match(getKey()); } /** * Creates a File from an io.file and a list of sources directories + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ + @Deprecated public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) { PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file); if (relativePath != null) { @@ -143,7 +153,9 @@ public class File extends Resource { /** * Creates a File from its name and a project + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ + @Deprecated public static File fromIOFile(java.io.File file, Project project) { return fromIOFile(file, project.getFileSystem().getSourceDirs()); } @@ -217,12 +229,35 @@ public class File extends Resource { this.qualifier = qualifier; } + public static File create(String relativePathFromBasedir, String relativePathFromSourceDir, Language language, boolean unitTest) { + File file = new File(); + String normalizedPath = normalize(relativePathFromBasedir); + file.setKey(normalizedPath); + file.setPath(normalizedPath); + String directoryKey = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR); + file.setLanguage(language); + if (relativePathFromSourceDir.contains(Directory.SEPARATOR)) { + file.filename = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR); + file.directoryDeprecatedKey = Directory.parseKey(StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR)); + file.setDeprecatedKey(file.directoryDeprecatedKey + Directory.SEPARATOR + file.filename); + } else { + file.filename = relativePathFromSourceDir; + file.directoryDeprecatedKey = Directory.ROOT; + file.setDeprecatedKey(file.filename); + } + if (unitTest) { + file.setQualifier(Qualifiers.UNIT_TEST_FILE); + } + file.parent = Directory.create(directoryKey, file.directoryDeprecatedKey); + return file; + } + @Override public String toString() { return new ToStringBuilder(this) .append("key", getKey()) .append("path", getPath()) - .append("dir", directoryKey) + .append("dir", directoryDeprecatedKey) .append("filename", filename) .append("language", language) .toString(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java index d3cbf2f88ad..74fce50de45 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java @@ -35,15 +35,21 @@ import java.util.List; public class JavaFile extends Resource { private static final String JAVA_SUFFIX = ".java"; - private String filename; - private String longName; - private String packageKey; + private String className; + private String fullyQualifiedName; + private String packageFullyQualifiedName; private boolean unitTest; private JavaPackage parent; + private JavaFile() { + // Default constructor + } + /** * Creates a JavaFile that is not a class of test based on package and file names + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String packageName, String className) { this(packageName, className, false); } @@ -52,29 +58,33 @@ public class JavaFile extends Resource { * Creates a JavaFile that can be of any type based on package and file names * * @param unitTest whether it is a unit test file or a source file + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String packageKey, String className, boolean unitTest) { if (className == null) { throw new IllegalArgumentException("Java filename can not be null"); } - this.filename = StringUtils.trim(className); - String key; + this.className = StringUtils.trim(className); + String deprecatedKey; if (StringUtils.isBlank(packageKey)) { - this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; - this.longName = this.filename; - key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); + this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME; + this.fullyQualifiedName = this.className; + deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString(); } else { - this.packageKey = packageKey.trim(); - key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); - this.longName = key; + this.packageFullyQualifiedName = packageKey.trim(); + deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString(); + this.fullyQualifiedName = deprecatedKey; } - setKey(key); + setDeprecatedKey(deprecatedKey); this.unitTest = unitTest; } /** * Creates a source file from its key + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String key) { this(key, false); } @@ -83,7 +93,9 @@ public class JavaFile extends Resource { * Creates any JavaFile from its key * * @param unitTest whether it is a unit test file or a source file + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String key, boolean unitTest) { if (key == null) { throw new IllegalArgumentException("Java filename can not be null"); @@ -92,17 +104,17 @@ public class JavaFile extends Resource { this.unitTest = unitTest; if (realKey.contains(".")) { - this.filename = StringUtils.substringAfterLast(realKey, "."); - this.packageKey = StringUtils.substringBeforeLast(realKey, "."); - this.longName = realKey; + this.className = StringUtils.substringAfterLast(realKey, "."); + this.packageFullyQualifiedName = StringUtils.substringBeforeLast(realKey, "."); + this.fullyQualifiedName = realKey; } else { - this.filename = realKey; - this.longName = realKey; - this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; + this.className = realKey; + this.fullyQualifiedName = realKey; + this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME; realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString(); } - setKey(realKey); + setDeprecatedKey(realKey); } /** @@ -111,14 +123,9 @@ public class JavaFile extends Resource { @Override public JavaPackage getParent() { if (parent == null) { - parent = new JavaPackage(packageKey); - String filePath = getPath(); - if (StringUtils.isNotBlank(filePath)) { - parent.setPath(StringUtils.substringBeforeLast(filePath, Directory.SEPARATOR)); - } + parent = new JavaPackage(packageFullyQualifiedName); } return parent; - } /** @@ -142,7 +149,7 @@ public class JavaFile extends Resource { */ @Override public String getName() { - return filename; + return className; } /** @@ -150,7 +157,7 @@ public class JavaFile extends Resource { */ @Override public String getLongName() { - return longName; + return fullyQualifiedName; } /** @@ -181,22 +188,14 @@ public class JavaFile extends Resource { */ @Override public boolean matchFilePattern(String antPattern) { - String fileKey = getKey(); - if (!fileKey.endsWith(JAVA_SUFFIX)) { - fileKey += JAVA_SUFFIX; - } - // Add wildcard extension if not provided - if ((antPattern.contains("/") && StringUtils.substringAfterLast(antPattern, "/").indexOf('.') < 0) || antPattern.indexOf('.') < 0) { - antPattern += ".*"; - } - String noPackagePrefix = JavaPackage.DEFAULT_PACKAGE_NAME + "."; - if (fileKey.startsWith(noPackagePrefix)) { - fileKey = fileKey.substring(noPackagePrefix.length()); - } - WildcardPattern matcher = WildcardPattern.create(antPattern, "."); - return matcher.match(fileKey); + WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR); + return matcher.match(getKey()); } + /** + * @deprecated since 4.2 use {@link #create(String, String, boolean)} + */ + @Deprecated public static JavaFile fromIOFile(File file, Project module, boolean unitTest) { if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) { return null; @@ -212,6 +211,43 @@ public class JavaFile extends Resource { return null; } + /** + * For internal use only. + */ + public static JavaFile create(String relativePathFromBasedir) { + JavaFile javaFile = new JavaFile(); + String normalizedPath = normalize(relativePathFromBasedir); + javaFile.setKey(normalizedPath); + javaFile.setPath(normalizedPath); + String directoryKey = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR); + javaFile.parent = JavaPackage.create(directoryKey); + return javaFile; + } + + public static JavaFile create(String relativePathFromBasedir, String relativePathFromSourceDir, boolean unitTest) { + JavaFile javaFile = JavaFile.create(relativePathFromBasedir); + if (relativePathFromSourceDir.contains(Directory.SEPARATOR)) { + javaFile.packageFullyQualifiedName = StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR); + javaFile.packageFullyQualifiedName = StringUtils.replace(javaFile.packageFullyQualifiedName, Directory.SEPARATOR, "."); + javaFile.className = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR); + javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.className, JAVA_SUFFIX); + javaFile.fullyQualifiedName = javaFile.packageFullyQualifiedName + "." + javaFile.className; + javaFile.setDeprecatedKey(javaFile.fullyQualifiedName); + } else { + javaFile.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME; + javaFile.className = StringUtils.removeEndIgnoreCase(relativePathFromSourceDir, JAVA_SUFFIX); + javaFile.fullyQualifiedName = javaFile.className; + javaFile.setDeprecatedKey(JavaPackage.DEFAULT_PACKAGE_NAME + "." + javaFile.className); + } + javaFile.unitTest = unitTest; + javaFile.parent.setDeprecatedKey(javaFile.packageFullyQualifiedName); + return javaFile; + } + + /** + * @deprecated since 4.2 use {@link #create(String, String, boolean)} + */ + @Deprecated public static JavaFile fromRelativePath(String relativePath, boolean unitTest) { if (relativePath != null) { String pacname = null; @@ -232,7 +268,9 @@ public class JavaFile extends Resource { * Creates a JavaFile from a file in the source directories * * @return the JavaFile created if exists, null otherwise + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) { if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) { return null; @@ -246,7 +284,9 @@ public class JavaFile extends Resource { /** * Shortcut to fromIOFile with an abolute path + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) { if (path == null) { return null; @@ -258,8 +298,9 @@ public class JavaFile extends Resource { public String toString() { return new ToStringBuilder(this) .append("key", getKey()) + .append("deprecatedKey", getDeprecatedKey()) .append("path", getPath()) - .append("filename", filename) + .append("filename", className) .toString(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java index 71bed21e468..499d2706a58 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java @@ -22,9 +22,11 @@ package org.sonar.api.resources; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; +import javax.annotation.Nullable; + /** * A class that represents a Java package in Sonar - * + * * @since 1.10 */ public class JavaPackage extends Resource { @@ -36,23 +38,27 @@ public class JavaPackage extends Resource { /** * Default constructor + * @deprecated since 4.2 use {@link #create(String, String)} */ + @Deprecated public JavaPackage() { this(null); } /** - * Creates a JavaPackage from its key. Will use DEFAULT_PACKAGE_NAME if key is null + * Creates a JavaPackage from its key. + * @deprecated since 4.2 use {@link #create(String, String)} */ - public JavaPackage(String key) { - setKey(StringUtils.defaultIfEmpty(StringUtils.trim(key), DEFAULT_PACKAGE_NAME)); + @Deprecated + public JavaPackage(String deprecatedKey) { + setDeprecatedKey(StringUtils.defaultIfEmpty(StringUtils.trim(deprecatedKey), DEFAULT_PACKAGE_NAME)); } /** * @return whether the JavaPackage key is the default key */ public boolean isDefault() { - return StringUtils.equals(getKey(), DEFAULT_PACKAGE_NAME); + return StringUtils.equals(getDeprecatedKey(), DEFAULT_PACKAGE_NAME); } /** @@ -92,7 +98,7 @@ public class JavaPackage extends Resource { */ @Override public String getName() { - return getKey(); + return getDeprecatedKey(); } /** @@ -119,11 +125,29 @@ public class JavaPackage extends Resource { return Java.INSTANCE; } + /** + * For internal use only. + */ + public static JavaPackage create(String path) { + JavaPackage pac = new JavaPackage(); + String normalizedPath = normalize(path); + pac.setKey(normalizedPath); + pac.setPath(normalizedPath); + return pac; + } + + public static JavaPackage create(String relativePathFromBasedir, @Nullable String packageQualifiedName) { + JavaPackage pac = JavaPackage.create(relativePathFromBasedir); + pac.setDeprecatedKey(StringUtils.defaultIfEmpty(StringUtils.trim(packageQualifiedName), DEFAULT_PACKAGE_NAME)); + return pac; + } + @Override public String toString() { return new ToStringBuilder(this) - .append("id", getId()) - .append("key", getKey()) - .toString(); + .append("id", getId()) + .append("key", getKey()) + .append("deprecatedKey", getDeprecatedKey()) + .toString(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java index 6588de10201..0bd6309a7e7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java @@ -19,8 +19,6 @@ */ package org.sonar.api.resources; -import org.apache.commons.lang.StringUtils; - import javax.annotation.Nullable; import java.io.Serializable; @@ -126,10 +124,14 @@ public abstract class Resource implements Serializable { private String key = null; + private String deprecatedKey = null; + private String path = null; private String effectiveKey = null; + private String deprecatedEffectiveKey = null; + private boolean isExcluded = false; /** @@ -139,11 +141,29 @@ public abstract class Resource implements Serializable { return key; } - protected void setKey(String s) { + /** + * Internal use only + */ + public void setKey(String s) { this.key = s; } /** + * @return the resource deprecated key. Should not be used except to deal with backward compatibility. + * @since 4.2 + */ + public final String getDeprecatedKey() { + return deprecatedKey; + } + + /** + * For internal use only + */ + public void setDeprecatedKey(String s) { + this.deprecatedKey = s; + } + + /** * @return the resource name */ public abstract String getName(); @@ -215,7 +235,7 @@ public abstract class Resource implements Serializable { return this; } - private String normalize(@Nullable String path) { + protected static String normalize(@Nullable String path) { if (path == null) { return null; } @@ -242,6 +262,21 @@ public abstract class Resource implements Serializable { } /** + * Internal use only + */ + public String getDeprecatedEffectiveKey() { + return deprecatedEffectiveKey; + } + + /** + * Internal use only + */ + public final Resource setDeprecatedEffectiveKey(String deprecatedEffectiveKey) { + this.deprecatedEffectiveKey = deprecatedEffectiveKey; + return this; + } + + /** * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier. */ @Deprecated @@ -269,15 +304,15 @@ public abstract class Resource implements Serializable { } Resource resource = (Resource) o; - if (StringUtils.isBlank(path)) { + if (key != null) { return key.equals(resource.key); + } else { + return resource.key == null && deprecatedKey.equals(resource.deprecatedKey); } - return path.equals(resource.path); - } @Override public int hashCode() { - return key.hashCode(); + return key != null ? key.hashCode() : deprecatedKey.hashCode(); } } |