From 278f7980fd80c7a2288840e1332ce30344fd74e8 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 28 May 2012 10:23:15 +0200 Subject: [PATCH] Revert change that broke the API --- .../sonar/batch/bootstrap/ExtensionUtils.java | 8 ++-- .../api/batch/BatchExtensionDictionnary.java | 2 +- .../api/checks/AnnotationCheckFactory.java | 4 +- .../sonar/api/config/PropertyDefinitions.java | 4 +- .../sonar/api/rules/AnnotationRuleParser.java | 4 +- .../sonar/api/rules/RuleAnnotationUtils.java | 4 +- .../org/sonar/api/utils/AnnotationUtils.java | 41 ++++++++++++------- .../api/config/PropertyDefinitionTest.java | 8 ++-- .../sonar/api/utils/AnnotationUtilsTest.java | 16 +++++--- .../java/org/sonar/server/ui/ViewProxy.java | 24 +++++------ 10 files changed, 66 insertions(+), 49 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java index f02ef103c10..da9c7ffc214 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java @@ -36,7 +36,7 @@ public final class ExtensionUtils { static boolean isInstantiationStrategy(Object extension, String strategy) { Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); - InstantiationStrategy extStrategy = AnnotationUtils.getClassAnnotation(clazz, InstantiationStrategy.class); + InstantiationStrategy extStrategy = AnnotationUtils.getAnnotation(clazz, InstantiationStrategy.class); if (extStrategy != null) { return strategy.equals(extStrategy.value()); } @@ -49,7 +49,7 @@ public final class ExtensionUtils { static boolean isSupportedEnvironment(Object extension, EnvironmentInformation environment) { Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); - SupportedEnvironment env = AnnotationUtils.getClassAnnotation(clazz, SupportedEnvironment.class); + SupportedEnvironment env = AnnotationUtils.getAnnotation(clazz, SupportedEnvironment.class); if (env == null) { return true; } @@ -62,12 +62,12 @@ public final class ExtensionUtils { } static boolean checkDryRun(Object extension, boolean dryRun) { - return !dryRun || AnnotationUtils.getClassAnnotation(extension, NotDryRun.class)==null; + return !dryRun || AnnotationUtils.getAnnotation(extension, NotDryRun.class) == null; } static boolean isMavenExtensionOnly(Object extension) { Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); - SupportedEnvironment env = AnnotationUtils.getClassAnnotation(clazz, SupportedEnvironment.class); + SupportedEnvironment env = AnnotationUtils.getAnnotation(clazz, SupportedEnvironment.class); return env!=null && env.value().length==1 && StringUtils.equalsIgnoreCase("maven", env.value()[0]); } 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 getClassAnnotation(Object object, Class 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 getAnnotation(Object objectOrClass, Class 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> interfaces = ClassUtils.getAllInterfaces(initialClass); - for (Class anInterface : interfaces) { - result = anInterface.getAnnotation(annotationClass); - if (result != null) { - break; - } + for (Class anInterface : (List>) 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 getClassAnnotation(Object object, Class annotationClass) { + return (A) getAnnotation(object, (Class) 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")); } diff --git a/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java b/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java index 29d869e050a..ae3c1b29798 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java @@ -88,7 +88,7 @@ public class ViewProxy implements Comparable { } private void initRequiredMeasures(V view) { - RequiredMeasures requiredMeasuresAnnotation = AnnotationUtils.getClassAnnotation(view, RequiredMeasures.class); + RequiredMeasures requiredMeasuresAnnotation = AnnotationUtils.getAnnotation(view, RequiredMeasures.class); if (requiredMeasuresAnnotation != null) { mandatoryMeasures = requiredMeasuresAnnotation.allOf(); needOneOfMeasures = requiredMeasuresAnnotation.anyOf(); @@ -96,21 +96,21 @@ public class ViewProxy implements Comparable { } private void initWidgetLayout(final V view) { - WidgetLayout layoutAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetLayout.class); + WidgetLayout layoutAnnotation = AnnotationUtils.getAnnotation(view, WidgetLayout.class); if (layoutAnnotation != null) { widgetLayout = layoutAnnotation.value(); } } private void initWidgetCategory(final V view) { - WidgetCategory categAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetCategory.class); + WidgetCategory categAnnotation = AnnotationUtils.getAnnotation(view, WidgetCategory.class); if (categAnnotation != null) { widgetCategories = categAnnotation.value(); } } private void initWidgetGlobal(V view) { - WidgetScope scopeAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetScope.class); + WidgetScope scopeAnnotation = AnnotationUtils.getAnnotation(view, WidgetScope.class); if (scopeAnnotation != null) { checkValidScope(view, scopeAnnotation); isGlobal = ImmutableSet.copyOf(scopeAnnotation.value()).contains(WidgetScope.GLOBAL); @@ -126,7 +126,7 @@ public class ViewProxy implements Comparable { } private void initWidgetProperties(final V view) { - WidgetProperties propAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetProperties.class); + WidgetProperties propAnnotation = AnnotationUtils.getAnnotation(view, WidgetProperties.class); if (propAnnotation != null) { for (WidgetProperty property : propAnnotation.value()) { widgetPropertiesByKey.put(property.key(), property); @@ -135,14 +135,14 @@ public class ViewProxy implements Comparable { } private void initDescription(final V view) { - Description descriptionAnnotation = AnnotationUtils.getClassAnnotation(view, Description.class); + Description descriptionAnnotation = AnnotationUtils.getAnnotation(view, Description.class); if (descriptionAnnotation != null) { description = descriptionAnnotation.value(); } } private void initDefaultTabInfo(final V view) { - DefaultTab defaultTabAnnotation = AnnotationUtils.getClassAnnotation(view, DefaultTab.class); + DefaultTab defaultTabAnnotation = AnnotationUtils.getAnnotation(view, DefaultTab.class); if (defaultTabAnnotation != null) { if (defaultTabAnnotation.metrics().length == 0) { isDefaultTab = true; @@ -156,35 +156,35 @@ public class ViewProxy implements Comparable { } private void initResourceLanguage(final V view) { - ResourceLanguage languageAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceLanguage.class); + ResourceLanguage languageAnnotation = AnnotationUtils.getAnnotation(view, ResourceLanguage.class); if (languageAnnotation != null) { resourceLanguages = languageAnnotation.value(); } } private void initResourceQualifier(final V view) { - ResourceQualifier qualifierAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceQualifier.class); + ResourceQualifier qualifierAnnotation = AnnotationUtils.getAnnotation(view, ResourceQualifier.class); if (qualifierAnnotation != null) { resourceQualifiers = qualifierAnnotation.value(); } } private void initResourceScope(final V view) { - ResourceScope scopeAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceScope.class); + ResourceScope scopeAnnotation = AnnotationUtils.getAnnotation(view, ResourceScope.class); if (scopeAnnotation != null) { resourceScopes = scopeAnnotation.value(); } } private void initSections(final V view) { - NavigationSection sectionAnnotation = AnnotationUtils.getClassAnnotation(view, NavigationSection.class); + NavigationSection sectionAnnotation = AnnotationUtils.getAnnotation(view, NavigationSection.class); if (sectionAnnotation != null) { sections = sectionAnnotation.value(); } } private void initUserRoles(final V view) { - UserRole userRoleAnnotation = AnnotationUtils.getClassAnnotation(view, UserRole.class); + UserRole userRoleAnnotation = AnnotationUtils.getAnnotation(view, UserRole.class); if (userRoleAnnotation != null) { userRoles = userRoleAnnotation.value(); } -- 2.39.5