aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Bridge.java8
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java32
-rw-r--r--sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java83
-rw-r--r--sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java30
4 files changed, 130 insertions, 23 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Bridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Bridge.java
index 278e9a6cb29..51ab4d0e1f7 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Bridge.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Bridge.java
@@ -23,13 +23,15 @@ import org.sonar.api.batch.SensorContext;
import org.sonar.api.checks.CheckFactory;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
+import org.sonar.java.api.JavaClass;
import org.sonar.squid.Squid;
+import org.sonar.squid.api.SourceClass;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.api.SourcePackage;
import org.sonar.squid.api.SourceProject;
/**
- * Pattern visitor : project -> packages -> files
+ * Pattern visitor : project -> packages -> files -> classes
*/
public abstract class Bridge {
@@ -74,4 +76,8 @@ public abstract class Bridge {
public void onFile(SourceFile squidFile, Resource sonarFile) {
}
+
+ public void onClass(SourceClass squidClass, JavaClass sonarClass) {
+
+ }
}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java
index 57134b8ff69..c5d5fa3c55a 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ResourceIndex.java
@@ -25,10 +25,9 @@ import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.JavaPackage;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
+import org.sonar.java.api.JavaClass;
import org.sonar.squid.Squid;
-import org.sonar.squid.api.SourceCode;
-import org.sonar.squid.api.SourceFile;
-import org.sonar.squid.api.SourcePackage;
+import org.sonar.squid.api.*;
import org.sonar.squid.indexer.QueryByType;
import java.util.Collection;
@@ -40,6 +39,8 @@ public final class ResourceIndex extends HashMap<SourceCode, Resource> {
loadSquidProject(squid, project);
loadSquidPackages(squid, context);
loadSquidFiles(squid, context);
+ loadSquidClasses(squid, context);
+// loadSquidMethods(squid, context);
return this;
}
@@ -51,7 +52,7 @@ public final class ResourceIndex extends HashMap<SourceCode, Resource> {
Collection<SourceCode> packages = squid.search(new QueryByType(SourcePackage.class));
for (SourceCode squidPackage : packages) {
JavaPackage sonarPackage = SquidUtils.convertJavaPackageKeyFromSquidFormat(squidPackage.getKey());
- context.saveResource(sonarPackage);
+ context.index(sonarPackage);
put(squidPackage, context.getResource(sonarPackage)); // resource is reloaded to get the id
}
}
@@ -65,4 +66,27 @@ public final class ResourceIndex extends HashMap<SourceCode, Resource> {
}
}
+ private void loadSquidClasses(Squid squid, SensorContext context) {
+ Collection<SourceCode> classes = squid.search(new QueryByType(SourceClass.class));
+ for (SourceCode squidClass : classes) {
+ JavaFile sonarFile = (JavaFile)get(squidClass.getParent(SourceFile.class));
+ JavaClass sonarClass = new JavaClass.Builder()
+ .setName(squidClass.getKey())
+ .setFromLine(squidClass.getStartAtLine())
+ .setToLine(squidClass.getEndAtLine())
+ .create();
+ context.index(sonarClass, sonarFile);
+ put(squidClass, context.getResource(sonarClass)); // resource is reloaded to get the id
+ }
+ }
+
+ private void loadSquidMethods(Squid squid, SensorContext context) {
+ Collection<SourceCode> methods = squid.search(new QueryByType(SourceMethod.class));
+ for (SourceCode squidMethod : methods) {
+ JavaClass sonarClass = (JavaClass)get(squidMethod.getParent(SourceClass.class));
+ //context.index(new JavaMethod(squidMethod.getKey()), sonarClass);
+ //put(squidMethod, context.getResource(sonarClass)); // resource is reloaded to get the id
+ }
+ }
+
}
diff --git a/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java b/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java
index 537fba56985..8db9f241414 100644
--- a/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java
+++ b/sonar-java-api/src/main/java/org/sonar/java/api/JavaClass.java
@@ -20,36 +20,59 @@
package org.sonar.java.api;
import org.apache.commons.lang.StringUtils;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Resource;
/**
* @since 2.6
*/
public final class JavaClass extends Resource {
- private String name;
+ public static final int UNKNOWN_LINE = -1;
+
+ private int fromLine = UNKNOWN_LINE;
+ private int toLine = UNKNOWN_LINE;
private JavaClass(String name) {
- this.name = name;
setKey(name);
}
+ private JavaClass(String name, int fromLine, int toLine) {
+ setKey(name);
+ this.fromLine = fromLine;
+ this.toLine = toLine;
+ }
+
public String getPackageName() {
- return StringUtils.substringBeforeLast(name, JavaUtils.PACKAGE_SEPARATOR);
+ if (StringUtils.contains(getKey(), JavaUtils.PACKAGE_SEPARATOR)) {
+ return StringUtils.substringBeforeLast(getKey(), JavaUtils.PACKAGE_SEPARATOR);
+ }
+ return "";
}
public String getClassName() {
- return StringUtils.substringAfterLast(name, JavaUtils.PACKAGE_SEPARATOR);
+ String className = StringUtils.substringAfterLast(getKey(), JavaUtils.PACKAGE_SEPARATOR);
+ return StringUtils.defaultIfEmpty(className, getKey());
+ }
+
+ public int getFromLine() {
+ return fromLine;
+ }
+
+ public int getToLine() {
+ return toLine;
}
@Override
public String getName() {
- return getClassName();
+ return getKey();
}
@Override
public String getLongName() {
- return name;
+ return getKey();
}
@Override
@@ -64,7 +87,7 @@ public final class JavaClass extends Resource {
@Override
public String getScope() {
- return Scopes.TYPE;
+ return null;
}
@Override
@@ -82,15 +105,51 @@ public final class JavaClass extends Resource {
return false;
}
- public static JavaClass create(String name) {
+ public static JavaClass createRef(String name) {
return new JavaClass(name);
}
- public static JavaClass create(String packageName, String className) {
+ public static JavaClass createRef(String packageName, String className) {
if (StringUtils.isBlank(packageName)) {
return new JavaClass(className);
}
- String name = new StringBuilder().append(packageName).append(JavaUtils.PACKAGE_SEPARATOR).append(className).toString();
- return new JavaClass(name);
+ return new JavaClass(toName(packageName, className));
+ }
+
+ private static String toName(String packageName, String className) {
+ if (StringUtils.isBlank(packageName)) {
+ return className;
+ }
+ return new StringBuilder().append(packageName).append(JavaUtils.PACKAGE_SEPARATOR).append(className).toString();
+ }
+
+ public static class Builder {
+ private String name;
+ private int fromLine = UNKNOWN_LINE;
+ private int toLine = UNKNOWN_LINE;
+
+ public Builder setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder setName(String packageName, String className) {
+ this.name = toName(packageName, className);
+ return this;
+ }
+
+ public Builder setFromLine(int fromLine) {
+ this.fromLine = Math.max(UNKNOWN_LINE, fromLine);
+ return this;
+ }
+
+ public Builder setToLine(int toLine) {
+ this.toLine = Math.max(UNKNOWN_LINE, toLine);
+ return this;
+ }
+
+ public JavaClass create() {
+ return new JavaClass(name, fromLine, toLine);
+ }
}
}
diff --git a/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java b/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java
index 576d7d28c41..a2972ac2bc3 100644
--- a/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java
+++ b/sonar-java-api/src/test/java/org/sonar/java/api/JavaClassTest.java
@@ -30,22 +30,40 @@ import static org.junit.Assert.assertThat;
public class JavaClassTest {
@Test
- public void shouldCreateJavaClassFromName() {
- JavaClass javaClass = JavaClass.create("org.foo.Bar");
+ public void shouldCreateReferenceFromName() {
+ JavaClass javaClass = JavaClass.createRef("org.foo.Bar");
assertThat(javaClass.getClassName(), is("Bar"));
assertThat(javaClass.getKey(), is("org.foo.Bar"));
assertThat(javaClass.getLanguage(), is((Language)Java.INSTANCE));
- assertThat(javaClass.getName(), is("Bar"));
+ assertThat(javaClass.getName(), is("org.foo.Bar"));
assertThat(javaClass.getLongName(), is("org.foo.Bar"));
}
@Test
- public void shouldCreateJavaClassFromPackageAndClassname() {
- JavaClass javaClass = JavaClass.create("org.foo", "Bar");
+ public void shouldCreateReferenceFromPackageAndClassname() {
+ JavaClass javaClass = JavaClass.createRef("org.foo", "Bar");
assertThat(javaClass.getClassName(), is("Bar"));
assertThat(javaClass.getKey(), is("org.foo.Bar"));
assertThat(javaClass.getLanguage(), is((Language)Java.INSTANCE));
- assertThat(javaClass.getName(), is("Bar"));
+ assertThat(javaClass.getName(), is("org.foo.Bar"));
assertThat(javaClass.getLongName(), is("org.foo.Bar"));
}
+
+ @Test
+ public void shouldGetPackageName() {
+ JavaClass javaClass = JavaClass.createRef("org.foo.Bar");
+ assertThat(javaClass.getPackageName(), is("org.foo"));
+
+ javaClass = JavaClass.createRef("Bar");
+ assertThat(javaClass.getPackageName(), is(""));
+ }
+
+ @Test
+ public void shouldGetClassName() {
+ JavaClass javaClass = JavaClass.createRef("org.foo.Bar");
+ assertThat(javaClass.getClassName(), is("Bar"));
+
+ javaClass = JavaClass.createRef("Bar");
+ assertThat(javaClass.getClassName(), is("Bar"));
+ }
}