aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-java-api
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-java-api')
-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
2 files changed, 95 insertions, 18 deletions
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"));
+ }
}