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());
}
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;
}
}
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]);
}
}
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();
}
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();
}
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);
}
}
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);
}
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();
}
/**
* 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);
}
}
@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);
@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);
@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);
@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);
@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"));
}
}
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();
}
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);
}
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);
}
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;
}
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();
}