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 java.lang.annotation.Annotation;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.opendaylight.controller.config.api.annotations.Description;
17 public class AnnotationsHelper {
19 private AnnotationsHelper() {
23 * Look for annotation specified by annotationType on method. First observe
24 * method's class, then its super classes, then all provided interfaces.
25 * Used for finding @Description and @RequireInterface
28 * generic type of annotation
29 * @return list of found annotations
31 static <T extends Annotation> List<T> findMethodAnnotationInSuperClassesAndIfcs(
32 final Method setter, Class<T> annotationType,
33 Set<Class<?>> inspectedInterfaces) {
34 List<T> result = new ArrayList<T>();
35 Class<?> inspectedClass = setter.getDeclaringClass();
38 Method foundSetter = inspectedClass.getMethod(setter.getName(),
39 setter.getParameterTypes());
40 T annotation = foundSetter.getAnnotation(annotationType);
41 if (annotation != null) {
42 result.add(annotation);
44 // we need to go deeper
45 inspectedClass = inspectedClass.getSuperclass();
46 } catch (NoSuchMethodException e) {
47 inspectedClass = Object.class; // no need to go further
49 } while (inspectedClass.equals(Object.class) == false);
51 for (Class<?> ifc : inspectedInterfaces) {
52 if (ifc.isInterface() == false) {
53 throw new IllegalArgumentException(ifc + " is not an interface");
56 Method foundSetter = ifc.getMethod(setter.getName(),
57 setter.getParameterTypes());
58 T annotation = foundSetter.getAnnotation(annotationType);
59 if (annotation != null) {
60 result.add(annotation);
62 } catch (NoSuchMethodException e) {
70 * Look for annotation specified by annotationType on type. First observe
71 * class clazz, then its super classes, then all exported interfaces with
72 * their super types. Used for finding @Description of modules.
74 * @return list of found annotations
76 static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(
77 Class<?> clazz, Class<T> annotationType, Set<Class<?>> interfaces) {
78 List<T> result = new ArrayList<T>();
79 Class<?> declaringClass = clazz;
81 T annotation = declaringClass.getAnnotation(annotationType);
82 if (annotation != null) {
83 result.add(annotation);
85 declaringClass = declaringClass.getSuperclass();
86 } while (declaringClass.equals(Object.class) == false);
88 for (Class<?> ifc : interfaces) {
89 if (ifc.isInterface() == false) {
90 throw new IllegalArgumentException(ifc + " is not an interface");
92 T annotation = ifc.getAnnotation(annotationType);
93 if (annotation != null) {
94 result.add(annotation);
101 * @return empty string if no annotation is found, or list of descriptions
102 * separated by newline
104 static String aggregateDescriptions(List<Description> descriptions) {
105 StringBuilder builder = new StringBuilder();
106 for (Description d : descriptions) {
107 if (builder.length() != 0) {
108 builder.append("\n");
110 builder.append(d.value());
113 return builder.toString();