summaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/AnnotationUtils.java25
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/AnnotationUtilsTest.java21
2 files changed, 34 insertions, 12 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/AnnotationUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/AnnotationUtils.java
index 10d1b15f64e..bcf9e1a512e 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/AnnotationUtils.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/AnnotationUtils.java
@@ -19,8 +19,6 @@
*/
package org.sonar.api.utils;
-import java.lang.annotation.Annotation;
-
/**
* A utility class for annotations
*
@@ -35,14 +33,23 @@ public final class AnnotationUtils {
* Searches for a class annotation. All inheritance tree is analysed.
*/
public static <A> A getClassAnnotation(final Object object, final Class<A> annotationClass) {
- Class aClass = (object instanceof Class ? (Class)object : object.getClass());
- while (aClass != null) {
- Annotation annotation = aClass.getAnnotation(annotationClass);
- if (annotation != null) {
- return (A) annotation;
- }
+ Class initialClass = (object instanceof Class ? (Class) object : object.getClass());
+ A result = null;
+ Class aClass=initialClass;
+ while (aClass != null && result == null) {
+ result = (A)aClass.getAnnotation(annotationClass);
aClass = aClass.getSuperclass();
}
- return null;
+
+ if (result==null) {
+ Class[] interfaces = initialClass.getInterfaces();
+ for (Class anInterface : interfaces) {
+ result = (A)anInterface.getAnnotation(annotationClass);
+ if (result!=null) {
+ break;
+ }
+ }
+ }
+ return result;
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/AnnotationUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/AnnotationUtilsTest.java
index 395c60396a0..b29a86787c9 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/AnnotationUtilsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/AnnotationUtilsTest.java
@@ -19,9 +19,6 @@
*/
package org.sonar.api.utils;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.core.IsNull.nullValue;
-import static org.junit.Assert.assertThat;
import org.junit.Test;
import java.lang.annotation.ElementType;
@@ -29,6 +26,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+
public class AnnotationUtilsTest {
@Test
@@ -44,6 +45,12 @@ public class AnnotationUtilsTest {
}
@Test
+ public void searchClassAnnotationInInterface() {
+ FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(new ImplementedClass(), FakeAnnotation.class);
+ assertThat(annotation.value(), is("foo"));
+ }
+
+ @Test
public void noClassAnnotation() {
FakeAnnotation annotation = AnnotationUtils.getClassAnnotation("a string", FakeAnnotation.class);
assertThat(annotation, nullValue());
@@ -72,4 +79,12 @@ class SuperClass {
class ChildClass extends SuperClass {
+}
+
+@FakeAnnotation("foo")
+interface AnnotatedInterface {
+}
+
+class ImplementedClass implements AnnotatedInterface {
+
} \ No newline at end of file