Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / AnnotationsHelper.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
9
10 import java.lang.annotation.Annotation;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.controller.config.api.annotations.Description;
17
18 class AnnotationsHelper {
19
20     /**
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
24      *
25      * @param <T>
26      *            generic type of annotation
27      * @return list of found annotations
28      */
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();
34         do {
35             try {
36                 Method foundSetter = inspectedClass.getMethod(setter.getName(),
37                         setter.getParameterTypes());
38                 T annotation = foundSetter.getAnnotation(annotationType);
39                 if (annotation != null) {
40                     result.add(annotation);
41                 }
42                 // we need to go deeper
43                 inspectedClass = inspectedClass.getSuperclass();
44             } catch (NoSuchMethodException e) {
45                 inspectedClass = Object.class; // no need to go further
46             }
47         } while (inspectedClass.equals(Object.class) == false);
48         // inspect interfaces
49         for (Class<?> ifc : inspectedInterfaces) {
50             if (ifc.isInterface() == false) {
51                 throw new IllegalArgumentException(ifc + " is not an interface");
52             }
53             try {
54                 Method foundSetter = ifc.getMethod(setter.getName(),
55                         setter.getParameterTypes());
56                 T annotation = foundSetter.getAnnotation(annotationType);
57                 if (annotation != null) {
58                     result.add(annotation);
59                 }
60             } catch (NoSuchMethodException e) {
61
62             }
63         }
64         return result;
65     }
66
67     /**
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.
71      *
72      * @return list of found annotations
73      */
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;
78         do {
79             T annotation = declaringClass.getAnnotation(annotationType);
80             if (annotation != null) {
81                 result.add(annotation);
82             }
83             declaringClass = declaringClass.getSuperclass();
84         } while (declaringClass.equals(Object.class) == false);
85         // inspect interfaces
86         for (Class<?> ifc : interfaces) {
87             if (ifc.isInterface() == false) {
88                 throw new IllegalArgumentException(ifc + " is not an interface");
89             }
90             T annotation = ifc.getAnnotation(annotationType);
91             if (annotation != null) {
92                 result.add(annotation);
93             }
94         }
95         return result;
96     }
97
98     /**
99      * @return empty string if no annotation is found, or list of descriptions
100      *         separated by newline
101      */
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");
107             }
108             builder.append(d.value());
109
110         }
111         return builder.toString();
112     }
113 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.