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 org.opendaylight.controller.config.api.annotations.Description;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.List;
18 public class AnnotationsHelper {
21 * Look for annotation specified by annotationType on method. First observe
22 * method's class, then its super classes, then all provided interfaces.
23 * Used for finding @Description and @RequireInterface
26 * generic type of annotation
27 * @return list of found annotations
29 static <T extends Annotation> List<T> findMethodAnnotationInSuperClassesAndIfcs(
30 final Method setter, Class<T> annotationType,
31 Set<Class<?>> inspectedInterfaces) {
32 List<T> result = new ArrayList<T>();
33 Class<?> inspectedClass = setter.getDeclaringClass();
36 Method foundSetter = inspectedClass.getMethod(setter.getName(),
37 setter.getParameterTypes());
38 T annotation = foundSetter.getAnnotation(annotationType);
39 if (annotation != null) {
40 result.add(annotation);
42 // we need to go deeper
43 inspectedClass = inspectedClass.getSuperclass();
44 } catch (NoSuchMethodException e) {
45 inspectedClass = Object.class; // no need to go further
47 } while (inspectedClass.equals(Object.class) == false);
49 for (Class<?> ifc : inspectedInterfaces) {
50 if (ifc.isInterface() == false) {
51 throw new IllegalArgumentException(ifc + " is not an interface");
54 Method foundSetter = ifc.getMethod(setter.getName(),
55 setter.getParameterTypes());
56 T annotation = foundSetter.getAnnotation(annotationType);
57 if (annotation != null) {
58 result.add(annotation);
60 } catch (NoSuchMethodException e) {
68 * Look for annotation specified by annotationType on type. First observe
69 * class clazz, then its super classes, then all exported interfaces with
70 * their super types. Used for finding @Description of modules.
72 * @return list of found annotations
74 static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(
75 Class<?> clazz, Class<T> annotationType, Set<Class<?>> interfaces) {
76 List<T> result = new ArrayList<T>();
77 Class<?> declaringClass = clazz;
79 T annotation = declaringClass.getAnnotation(annotationType);
80 if (annotation != null) {
81 result.add(annotation);
83 declaringClass = declaringClass.getSuperclass();
84 } while (declaringClass.equals(Object.class) == false);
86 for (Class<?> ifc : interfaces) {
87 if (ifc.isInterface() == false) {
88 throw new IllegalArgumentException(ifc + " is not an interface");
90 T annotation = ifc.getAnnotation(annotationType);
91 if (annotation != null) {
92 result.add(annotation);
99 * @return empty string if no annotation is found, or list of descriptions
100 * separated by newline
102 static String aggregateDescriptions(List<Description> descriptions) {
103 StringBuilder builder = new StringBuilder();
104 for (Description d : descriptions) {
105 if (builder.length() != 0) {
106 builder.append("\n");
108 builder.append(d.value());
111 return builder.toString();