summaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-01-25 19:10:20 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-01-25 19:26:43 +0100
commite7de7db7a3a3de0bf7961f0b5210156182622f10 (patch)
treea638d3b82ebf27f68184c527df61fff9923502ab /sonar-plugin-api
parentf3b88bb8e5a57a215e59dcc5c38f550433c9a1eb (diff)
downloadsonarqube-e7de7db7a3a3de0bf7961f0b5210156182622f10.tar.gz
sonarqube-e7de7db7a3a3de0bf7961f0b5210156182622f10.zip
Improve backward-compatibility when adding data to a non-indexed resource
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java9
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java5
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java8
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java6
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java60
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java2
7 files changed, 70 insertions, 30 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java
index 07b58dc1a13..8ac007a1e6a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SensorContext.java
@@ -58,15 +58,16 @@ public interface SensorContext {
boolean index(Resource resource, Resource parentReference);
/**
- * Returns true if the referenced resource is excluded. An excluded resource is not indexed.
+ * Returns true if the referenced resource is indexed and excluded.
* @since 2.6
*/
boolean isExcluded(Resource reference);
/**
+ * Returns true if the referenced resource is indexed.
* @since 2.6
*/
- boolean isIndexed(Resource reference);
+ boolean isIndexed(Resource reference, boolean acceptExcluded);
/**
* Search for an indexed resource.
@@ -88,7 +89,6 @@ public interface SensorContext {
Collection<Resource> getChildren(Resource reference);
-
// ----------- MEASURES ON PROJECT --------------
/**
@@ -188,14 +188,13 @@ public interface SensorContext {
/**
* Save the source code of a file. The file must be have been indexed before.
- * Note: the source stream is not closed.
*
* @return false if the resource is excluded or not indexed
* @throws org.sonar.api.resources.DuplicatedSourceException
* if the source has already been set on this resource
* @since 1.10. Returns a boolean since 2.6.
*/
- boolean saveSource(Resource reference, String source) throws DuplicatedSourceException;
+ void saveSource(Resource reference, String source) throws DuplicatedSourceException;
// ----------- LINKS --------------
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java
index 06ae5ae80fd..cf441562c1b 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java
@@ -39,6 +39,7 @@ public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Depe
/**
* Indexes a resource as a direct child of project. This method does nothing and returns true if the resource already indexed.
+ * If the method resource.getParent() does not return null, then this parent will be indexed too.
*
* @return false if the resource is excluded
* @since 2.6
@@ -65,7 +66,7 @@ public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Depe
/**
* @since 2.6
*/
- public abstract boolean isIndexed(Resource reference);
+ public abstract boolean isIndexed(Resource reference, boolean acceptExcluded);
/**
* Search for an indexed resource.
@@ -95,7 +96,7 @@ public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Depe
* @throws org.sonar.api.resources.DuplicatedSourceException
* if the source has already been set on this resource
*/
- public abstract boolean setSource(Resource reference, String source) throws DuplicatedSourceException;
+ public abstract void setSource(Resource reference, String source) throws DuplicatedSourceException;
public abstract Project getProject();
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java
index 4b7837779e3..85a47eb8945 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java
@@ -353,11 +353,11 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
return getFiles(getTestDirs(), false /* FIXME should be true? */, langs);
}
- private class DefaultInputFile implements InputFile {
+ private static final class DefaultInputFile implements InputFile {
private File basedir;
private File file;
- public DefaultInputFile(File basedir, File file) {
+ DefaultInputFile(File basedir, File file) {
this.basedir = basedir;
this.file = file;
}
@@ -369,6 +369,10 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
public File getFile() {
return file;
}
+
+ public String getRelativePath() {
+ return DefaultProjectFileSystem.getRelativePath(file, basedir);
+ }
}
/**
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 24b7d32b6b8..40872867c7e 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
@@ -38,7 +38,7 @@ public class File extends Resource<Directory> {
private String filename;
private Language language;
private Directory parent;
- private String qualifier = Resource.QUALIFIER_FILE;
+ private String qualifier = Qualifiers.FILE;
/**
* 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
@@ -187,8 +187,8 @@ public class File extends Resource<Directory> {
/**
* @return SCOPE_ENTITY
*/
- public String getScope() {
- return Resource.SCOPE_ENTITY;
+ public final String getScope() {
+ return SCOPE;
}
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java
index 6e87e3445ad..4c926f897e1 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java
@@ -30,4 +30,14 @@ public interface InputFile {
File getFile();
+ /**
+ * Path relative to basedir. Directory separator is slash '/', whatever the platform.
+ *
+ * Example on windows: if basedir is c:\project\src\ and file is c:\project\src\org\foo\Bar.java, then relative path
+ * is org/foo/Bar.java
+ *
+ * Example on unix: if basedir is /project/src and file is /project/src/org/foo/Bar.java, then relative path
+ * is org/foo/Bar.java as well.
+ */
+ String getRelativePath();
}
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 ef940898ced..009a04e670a 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
@@ -37,7 +37,7 @@ public class JavaFile extends Resource<JavaPackage> {
private String longName;
private String packageKey;
private boolean unitTest;
- private JavaPackage parent = null;
+ private JavaPackage parent;
/**
* Creates a JavaFile that is not a class of test based on package and file names
@@ -52,7 +52,7 @@ public class JavaFile extends Resource<JavaPackage> {
* @param unitTest whether it is a unit test file or a source file
*/
public JavaFile(String packageKey, String className, boolean unitTest) {
- if (className==null) {
+ if (className == null) {
throw new IllegalArgumentException("Java filename can not be null");
}
if (className.indexOf('$') >= 0) {
@@ -86,10 +86,10 @@ public class JavaFile extends Resource<JavaPackage> {
* @param unitTest whether it is a unit test file or a source file
*/
public JavaFile(String key, boolean unitTest) {
- if (key==null) {
+ if (key == null) {
throw new IllegalArgumentException("Java filename can not be null");
}
- if (key != null && key.indexOf('$') >= 0) {
+ if (key.indexOf('$') >= 0) {
throw new IllegalArgumentException("Java inner classes are not supported : " + key);
}
String realKey = StringUtils.trim(key);
@@ -117,6 +117,7 @@ public class JavaFile extends Resource<JavaPackage> {
parent = new JavaPackage(packageKey);
}
return parent;
+
}
/**
@@ -151,14 +152,14 @@ public class JavaFile extends Resource<JavaPackage> {
* @return SCOPE_ENTITY
*/
public String getScope() {
- return Resource.SCOPE_ENTITY;
+ return Scopes.FILE;
}
/**
* @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_CLASS depending whether it is a unit test class
*/
public String getQualifier() {
- return unitTest ? Qualifiers.UNIT_TEST_FILE : Resource.QUALIFIER_CLASS;
+ return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.CLASS;
}
/**
@@ -179,23 +180,14 @@ public class JavaFile extends Resource<JavaPackage> {
if (!fileKey.endsWith(".java")) {
fileKey += ".java";
}
- if (StringUtils.substringAfterLast(antPattern, "/").indexOf(".")<0) {
+ if (StringUtils.substringAfterLast(antPattern, "/").indexOf(".") < 0) {
antPattern += ".*";
}
WildcardPattern matcher = WildcardPattern.create(antPattern, ".");
return matcher.match(fileKey);
}
- /**
- * Creates a JavaFile from a file in the source directories
- *
- * @return the JavaFile created if exists, null otherwise
- */
- public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) {
- if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) {
- return null;
- }
- String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs);
+ public static JavaFile fromRelativePath(String relativePath, boolean unitTest) {
if (relativePath != null) {
String pacname = null;
String classname = relativePath;
@@ -212,6 +204,19 @@ public class JavaFile extends Resource<JavaPackage> {
}
/**
+ * Creates a JavaFile from a file in the source directories
+ *
+ * @return the JavaFile created if exists, null otherwise
+ */
+ public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) {
+ if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) {
+ return null;
+ }
+ String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs);
+ return fromRelativePath(relativePath, unitTest);
+ }
+
+ /**
* Shortcut to fromIOFile with an abolute path
*/
public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) {
@@ -230,4 +235,25 @@ public class JavaFile extends Resource<JavaPackage> {
.append("unitTest", unitTest)
.toString();
}
+
+// @Override
+// public boolean equals(Object o) {
+// if (this == o) return true;
+// if (o == null || getClass() != o.getClass()) return false;
+// if (!super.equals(o)) return false;
+//
+// JavaFile javaFile = (JavaFile) o;
+// if (unitTest != javaFile.unitTest) return false;
+// if (!getKey().equals(javaFile.getKey())) return false;
+//
+// return true;
+// }
+//
+// @Override
+// public int hashCode() {
+// int result = super.hashCode();
+// result = 31 * result + getKey().hashCode();
+// result = 31 * result + (unitTest ? 1 : 0);
+// return result;
+// }
}
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 d61088c1b45..0ec5528ca9f 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
@@ -80,7 +80,7 @@ public class JavaPackage extends Resource {
* @return QUALIFIER_PACKAGE
*/
public String getQualifier() {
- return Resource.QUALIFIER_PACKAGE;
+ return Qualifiers.PACKAGE;
}
/**