diff options
author | David Gageot <david@gageot.net> | 2012-05-28 10:23:15 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-05-28 10:43:02 +0200 |
commit | 278f7980fd80c7a2288840e1332ce30344fd74e8 (patch) | |
tree | 5baca33b8375b4e344a07c0ecb2081b1b32e730d /sonar-plugin-api | |
parent | 858b34a0652ce0a819302e69d935a3590b0f0df9 (diff) | |
download | sonarqube-278f7980fd80c7a2288840e1332ce30344fd74e8.tar.gz sonarqube-278f7980fd80c7a2288840e1332ce30344fd74e8.zip |
Revert change that broke the API
Diffstat (limited to 'sonar-plugin-api')
8 files changed, 50 insertions, 33 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java index c8c58fab873..846cfa31a01 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java @@ -192,7 +192,7 @@ public class BatchExtensionDictionnary { } protected Phase.Name evaluatePhase(Object extension) { - Phase phaseAnnotation = AnnotationUtils.getClassAnnotation(extension, Phase.class); + Phase phaseAnnotation = AnnotationUtils.getAnnotation(extension, Phase.class); if (phaseAnnotation != null) { return phaseAnnotation.name(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java b/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java index b673cf8537a..dae81a92702 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/checks/AnnotationCheckFactory.java @@ -170,11 +170,11 @@ public final class AnnotationCheckFactory extends CheckFactory { private String getRuleKey(Object annotatedClassOrObject) { String key = null; - Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClassOrObject, Rule.class); + Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, Rule.class); if (ruleAnnotation != null) { key = ruleAnnotation.key(); } else { - Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClassOrObject, Check.class); + Check checkAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, Check.class); if (checkAnnotation != null) { key = checkAnnotation.key(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java index 13c9b374fa5..fb541565056 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java @@ -63,13 +63,13 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen } public PropertyDefinitions addComponent(Object component, String defaultCategory) { - Properties annotations = AnnotationUtils.getClassAnnotation(component, Properties.class); + Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class); if (annotations != null) { for (Property property : annotations.value()) { addProperty(property, defaultCategory); } } - Property annotation = AnnotationUtils.getClassAnnotation(component, Property.class); + Property annotation = AnnotationUtils.getAnnotation(component, Property.class); if (annotation != null) { addProperty(annotation, defaultCategory); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java index 7a5ba3aea8a..f4fc4485638 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java @@ -48,11 +48,11 @@ public final class AnnotationRuleParser implements ServerComponent { } private Rule create(String repositoryKey, Class annotatedClass) { - org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, org.sonar.check.Rule.class); + org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class); if (ruleAnnotation != null) { return toRule(repositoryKey, annotatedClass, ruleAnnotation); } - Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, Check.class); + Check checkAnnotation = AnnotationUtils.getAnnotation(annotatedClass, Check.class); if (checkAnnotation != null) { return toRule(repositoryKey, annotatedClass, checkAnnotation); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleAnnotationUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleAnnotationUtils.java index 2695db06b9e..517eaf44877 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleAnnotationUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleAnnotationUtils.java @@ -34,11 +34,11 @@ public final class RuleAnnotationUtils { public static String getRuleKey(Class annotatedClass) { String key = null; - org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, org.sonar.check.Rule.class); + org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class); if (ruleAnnotation != null) { key = ruleAnnotation.key(); } else { - Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, Check.class); + Check checkAnnotation = AnnotationUtils.getAnnotation(annotatedClass, Check.class); if (checkAnnotation != null) { key = checkAnnotation.key(); } 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 8ca55d28ae4..c032db3a786 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 @@ -36,25 +36,36 @@ public final class AnnotationUtils { /** * Searches for a class annotation. All inheritance tree is analysed. + * + * @since 3.1 */ - public static <A extends Annotation> A getClassAnnotation(Object object, Class<A> annotationClass) { - Class<?> initialClass = (object instanceof Class<?> ? (Class<?>) object : object.getClass()); - A result = null; - Class<?> aClass = initialClass; - while (aClass != null && result == null) { - result = aClass.getAnnotation(annotationClass); - aClass = aClass.getSuperclass(); + public static <A extends Annotation> A getAnnotation(Object objectOrClass, Class<A> annotationClass) { + Class<?> initialClass = (objectOrClass instanceof Class<?> ? (Class<?>) objectOrClass : objectOrClass.getClass()); + + for (Class<?> aClass = initialClass; aClass != null; aClass = aClass.getSuperclass()) { + A result = aClass.getAnnotation(annotationClass); + if (result != null) { + return result; + } } - if (result == null) { - List<Class<?>> interfaces = ClassUtils.getAllInterfaces(initialClass); - for (Class<?> anInterface : interfaces) { - result = anInterface.getAnnotation(annotationClass); - if (result != null) { - break; - } + for (Class<?> anInterface : (List<Class<?>>) ClassUtils.getAllInterfaces(initialClass)) { + A result = anInterface.getAnnotation(annotationClass); + if (result != null) { + return result; } } - return result; + + return null; + } + + /** + * Searches for a class annotation. All inheritance tree is analysed. + * + * @deprecated As of 3.1, replaced by {@link #getAnnotation(Object,Class)} + */ + @Deprecated + public static <A> A getClassAnnotation(Object object, Class<A> annotationClass) { + return (A) getAnnotation(object, (Class<? extends Annotation>) annotationClass); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java index 035a48c5707..793b4077356 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java @@ -36,7 +36,7 @@ public class PropertyDefinitionTest { @Test public void createFromAnnotation() { - Properties props = AnnotationUtils.getClassAnnotation(Init.class, Properties.class); + Properties props = AnnotationUtils.getAnnotation(Init.class, Properties.class); Property prop = props.value()[0]; PropertyDefinition def = PropertyDefinition.create(prop); @@ -63,7 +63,7 @@ public class PropertyDefinitionTest { @Test public void createFromAnnotation_default_values() { - Properties props = AnnotationUtils.getClassAnnotation(DefaultValues.class, Properties.class); + Properties props = AnnotationUtils.getAnnotation(DefaultValues.class, Properties.class); Property prop = props.value()[0]; PropertyDefinition def = PropertyDefinition.create(prop); @@ -159,7 +159,7 @@ public class PropertyDefinitionTest { @Test public void autoDetectPasswordType() { - Properties props = AnnotationUtils.getClassAnnotation(OldScmPlugin.class, Properties.class); + Properties props = AnnotationUtils.getAnnotation(OldScmPlugin.class, Properties.class); Property prop = props.value()[0]; PropertyDefinition def = PropertyDefinition.create(prop); @@ -176,7 +176,7 @@ public class PropertyDefinitionTest { @Test public void autoDetectLicenseType() { - Properties props = AnnotationUtils.getClassAnnotation(ViewsPlugin.class, Properties.class); + Properties props = AnnotationUtils.getAnnotation(ViewsPlugin.class, Properties.class); Property prop = props.value()[0]; PropertyDefinition def = PropertyDefinition.create(prop); 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 cd9966140ab..30207f3f4e4 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 @@ -34,34 +34,40 @@ public class AnnotationUtilsTest { @Test public void getClassAnnotation() { + FakeAnnotation annotation = AnnotationUtils.getAnnotation(new SuperClass(), FakeAnnotation.class); + assertThat(annotation.value(), is("foo")); + } + + @Test + public void getClassAnnotationWithDeprecatedMethod() { FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(new SuperClass(), FakeAnnotation.class); assertThat(annotation.value(), is("foo")); } @Test public void searchClassAnnotationInSuperClass() { - FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(new ChildClass(), FakeAnnotation.class); + FakeAnnotation annotation = AnnotationUtils.getAnnotation(new ChildClass(), FakeAnnotation.class); assertThat(annotation.value(), is("foo")); } @Test public void searchClassAnnotationInInterface() { - FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(new ImplementedClass(), FakeAnnotation.class); + FakeAnnotation annotation = AnnotationUtils.getAnnotation(new ImplementedClass(), FakeAnnotation.class); assertThat(annotation.value(), is("foo")); } @Test public void noClassAnnotation() { - FakeAnnotation annotation = AnnotationUtils.getClassAnnotation("a string", FakeAnnotation.class); + FakeAnnotation annotation = AnnotationUtils.getAnnotation("a string", FakeAnnotation.class); assertThat(annotation, nullValue()); } @Test public void shouldAcceptClasses() { - FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(SuperClass.class, FakeAnnotation.class); + FakeAnnotation annotation = AnnotationUtils.getAnnotation(SuperClass.class, FakeAnnotation.class); assertThat(annotation.value(), is("foo")); - annotation = AnnotationUtils.getClassAnnotation(ChildClass.class, FakeAnnotation.class); + annotation = AnnotationUtils.getAnnotation(ChildClass.class, FakeAnnotation.class); assertThat(annotation.value(), is("foo")); } |