diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-02 08:08:24 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-02 08:09:02 +0100 |
commit | 4c4af803b71cabb8fabd7ae142bb6a6efd9bdbd9 (patch) | |
tree | da8cff4a03b8ff08587bdbaf10cde7218f4e908b /sonar-plugin-api | |
parent | c449685c7fc56d15daab8eec3df557ed6a8ec938 (diff) | |
download | sonarqube-4c4af803b71cabb8fabd7ae142bb6a6efd9bdbd9.tar.gz sonarqube-4c4af803b71cabb8fabd7ae142bb6a6efd9bdbd9.zip |
Fix org.sonar.api.utils.AnnotationUtils: search for annotations in interfaces too
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/AnnotationUtils.java | 25 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/utils/AnnotationUtilsTest.java | 21 |
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 |