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 {
20 private AnnotationsHelper() {
24 * Look for annotation specified by annotationType on method. First observe
25 * method's class, then its super classes, then all provided interfaces.
26 * Used for finding @Description and @RequireInterface
29 * generic type of annotation
30 * @return list of found annotations
32 static <T extends Annotation> List<T> findMethodAnnotationInSuperClassesAndIfcs(
33 final Method setter, Class<T> annotationType,
34 Set<Class<?>> inspectedInterfaces) {
35 List<T> result = new ArrayList<T>();
36 Class<?> inspectedClass = setter.getDeclaringClass();
39 Method foundSetter = inspectedClass.getMethod(setter.getName(),
40 setter.getParameterTypes());
41 T annotation = foundSetter.getAnnotation(annotationType);
42 if (annotation != null) {
43 result.add(annotation);
45 // we need to go deeper
46 inspectedClass = inspectedClass.getSuperclass();
47 } catch (NoSuchMethodException e) {
48 inspectedClass = Object.class; // no need to go further
50 } while (inspectedClass.equals(Object.class) == false);
52 for (Class<?> ifc : inspectedInterfaces) {
53 if (ifc.isInterface() == false) {
54 throw new IllegalArgumentException(ifc + " is not an interface");
57 Method foundSetter = ifc.getMethod(setter.getName(),
58 setter.getParameterTypes());
59 T annotation = foundSetter.getAnnotation(annotationType);
60 if (annotation != null) {
61 result.add(annotation);
63 } catch (NoSuchMethodException e) {
71 * Look for annotation specified by annotationType on type. First observe
72 * class clazz, then its super classes, then all exported interfaces with
73 * their super types. Used for finding @Description of modules.
75 * @return list of found annotations
77 static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(
78 Class<?> clazz, Class<T> annotationType, Set<Class<?>> interfaces) {
79 List<T> result = new ArrayList<T>();
80 Class<?> declaringClass = clazz;
82 T annotation = declaringClass.getAnnotation(annotationType);
83 if (annotation != null) {
84 result.add(annotation);
86 declaringClass = declaringClass.getSuperclass();
87 } while (declaringClass.equals(Object.class) == false);
89 for (Class<?> ifc : interfaces) {
90 if (ifc.isInterface() == false) {
91 throw new IllegalArgumentException(ifc + " is not an interface");
93 T annotation = ifc.getAnnotation(annotationType);
94 if (annotation != null) {
95 result.add(annotation);
102 * @return empty string if no annotation is found, or list of descriptions
103 * separated by newline
105 static String aggregateDescriptions(List<Description> descriptions) {
106 StringBuilder builder = new StringBuilder();
107 for (Description d : descriptions) {
108 if (builder.length() != 0) {
109 builder.append("\n");
111 builder.append(d.value());
114 return builder.toString();