Merge "Add get-config commit edit-config to testtool"
[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 org.opendaylight.controller.config.api.annotations.Description;
11
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17
18 public class AnnotationsHelper {
19
20     private AnnotationsHelper() {
21     }
22
23     /**
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
27      *
28      * @param <T>
29      *            generic type of annotation
30      * @return list of found annotations
31      */
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();
37         do {
38             try {
39                 Method foundSetter = inspectedClass.getMethod(setter.getName(),
40                         setter.getParameterTypes());
41                 T annotation = foundSetter.getAnnotation(annotationType);
42                 if (annotation != null) {
43                     result.add(annotation);
44                 }
45                 // we need to go deeper
46                 inspectedClass = inspectedClass.getSuperclass();
47             } catch (NoSuchMethodException e) {
48                 inspectedClass = Object.class; // no need to go further
49             }
50         } while (inspectedClass.equals(Object.class) == false);
51         // inspect interfaces
52         for (Class<?> ifc : inspectedInterfaces) {
53             if (ifc.isInterface() == false) {
54                 throw new IllegalArgumentException(ifc + " is not an interface");
55             }
56             try {
57                 Method foundSetter = ifc.getMethod(setter.getName(),
58                         setter.getParameterTypes());
59                 T annotation = foundSetter.getAnnotation(annotationType);
60                 if (annotation != null) {
61                     result.add(annotation);
62                 }
63             } catch (NoSuchMethodException e) {
64
65             }
66         }
67         return result;
68     }
69
70     /**
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.
74      *
75      * @return list of found annotations
76      */
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;
81         do {
82             T annotation = declaringClass.getAnnotation(annotationType);
83             if (annotation != null) {
84                 result.add(annotation);
85             }
86             declaringClass = declaringClass.getSuperclass();
87         } while (declaringClass.equals(Object.class) == false);
88         // inspect interfaces
89         for (Class<?> ifc : interfaces) {
90             if (ifc.isInterface() == false) {
91                 throw new IllegalArgumentException(ifc + " is not an interface");
92             }
93             T annotation = ifc.getAnnotation(annotationType);
94             if (annotation != null) {
95                 result.add(annotation);
96             }
97         }
98         return result;
99     }
100
101     /**
102      * @return empty string if no annotation is found, or list of descriptions
103      *         separated by newline
104      */
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");
110             }
111             builder.append(d.value());
112
113         }
114         return builder.toString();
115     }
116 }