2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSet.Builder;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.opendaylight.controller.config.api.annotations.Description;
19 public class AnnotationsHelper {
21 private AnnotationsHelper() {
25 * Look for annotation specified by annotationType on method. First observe
26 * method's class, then its super classes, then all provided interfaces.
27 * Used for finding @Description and @RequireInterface
30 * generic type of annotation
31 * @return list of found annotations
33 static <T extends Annotation> List<T> findMethodAnnotationInSuperClassesAndIfcs(
34 final Method setter, final Class<T> annotationType,
35 final Set<Class<?>> inspectedInterfaces) {
36 Builder<T> result = ImmutableSet.builder();
37 Class<?> inspectedClass = setter.getDeclaringClass();
40 Method foundSetter = inspectedClass.getMethod(setter.getName(),
41 setter.getParameterTypes());
42 T annotation = foundSetter.getAnnotation(annotationType);
43 if (annotation != null) {
44 result.add(annotation);
46 // we need to go deeper
47 inspectedClass = inspectedClass.getSuperclass();
48 // no need to go further
49 } catch (final NoSuchMethodException e) {
50 inspectedClass = Object.class;
52 } while (!inspectedClass.equals(Object.class));
55 for (Class<?> ifc : inspectedInterfaces) {
56 if (!ifc.isInterface()) {
57 throw new IllegalArgumentException(ifc + " is not an interface");
60 Method foundSetter = ifc.getMethod(setter.getName(),
61 setter.getParameterTypes());
62 T annotation = foundSetter.getAnnotation(annotationType);
63 if (annotation != null) {
64 result.add(annotation);
66 } catch (final NoSuchMethodException e) {
70 return new ArrayList<>(result.build());
74 * Look for annotation specified by annotationType on type. First observe
75 * class clazz, then its super classes, then all exported interfaces with
76 * their super types. Used for finding @Description of modules.
78 * @return list of found annotations
80 static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(
81 final Class<?> clazz, final Class<T> annotationType, final Set<Class<?>> interfaces) {
82 List<T> result = new ArrayList<>();
83 Class<?> declaringClass = clazz;
85 T annotation = declaringClass.getAnnotation(annotationType);
86 if (annotation != null) {
87 result.add(annotation);
89 declaringClass = declaringClass.getSuperclass();
90 } while (!declaringClass.equals(Object.class));
92 for (Class<?> ifc : interfaces) {
93 if (!ifc.isInterface()) {
94 throw new IllegalArgumentException(ifc + " is not an interface");
96 T annotation = ifc.getAnnotation(annotationType);
97 if (annotation != null) {
98 result.add(annotation);
105 * @return empty string if no annotation is found, or list of descriptions
106 * separated by newline
108 static String aggregateDescriptions(final List<Description> descriptions) {
109 StringBuilder builder = new StringBuilder();
110 for (Description d : descriptions) {
111 if (builder.length() != 0) {
112 builder.append("\n");
114 builder.append(d.value());
117 return builder.toString();