Merge "(Bug 2035) - Increasing default Akka config for auto-down of unreachable nodes...
[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 com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSet.Builder;
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 import org.opendaylight.controller.config.api.annotations.Description;
18
19 public class AnnotationsHelper {
20
21     private AnnotationsHelper() {
22     }
23
24     /**
25      * Look for annotation specified by annotationType on method. First observe
26      * method's class, then its super classes, then all provided interfaces.
27      * Used for finding @Description and @RequireInterface
28      *
29      * @param <T>
30      *            generic type of annotation
31      * @return list of found annotations
32      */
33     static <T extends Annotation> List<T> findMethodAnnotationInSuperClassesAndIfcs(
34             final Method setter, final Class<T> annotationType,
35             final Set<Class<?>> inspectedInterfaces) {
36         Builder<T> result = ImmutableSet.builder();
37         Class<?> inspectedClass = setter.getDeclaringClass();
38         do {
39             try {
40                 Method foundSetter = inspectedClass.getMethod(setter.getName(),
41                         setter.getParameterTypes());
42                 T annotation = foundSetter.getAnnotation(annotationType);
43                 if (annotation != null) {
44                     result.add(annotation);
45                 }
46                 // we need to go deeper
47                 inspectedClass = inspectedClass.getSuperclass();
48             } catch (NoSuchMethodException e) {
49                 inspectedClass = Object.class; // no need to go further
50             }
51         } while (!inspectedClass.equals(Object.class));
52
53         // inspect interfaces
54         for (Class<?> ifc : inspectedInterfaces) {
55             if (ifc.isInterface() == false) {
56                 throw new IllegalArgumentException(ifc + " is not an interface");
57             }
58             try {
59                 Method foundSetter = ifc.getMethod(setter.getName(),
60                         setter.getParameterTypes());
61                 T annotation = foundSetter.getAnnotation(annotationType);
62                 if (annotation != null) {
63                     result.add(annotation);
64                 }
65             } catch (NoSuchMethodException e) {
66
67             }
68         }
69         return new ArrayList<>(result.build());
70     }
71
72     /**
73      * Look for annotation specified by annotationType on type. First observe
74      * class clazz, then its super classes, then all exported interfaces with
75      * their super types. Used for finding @Description of modules.
76      *
77      * @return list of found annotations
78      */
79     static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(
80             final Class<?> clazz, final Class<T> annotationType, final Set<Class<?>> interfaces) {
81         List<T> result = new ArrayList<T>();
82         Class<?> declaringClass = clazz;
83         do {
84             T annotation = declaringClass.getAnnotation(annotationType);
85             if (annotation != null) {
86                 result.add(annotation);
87             }
88             declaringClass = declaringClass.getSuperclass();
89         } while (declaringClass.equals(Object.class) == false);
90         // inspect interfaces
91         for (Class<?> ifc : interfaces) {
92             if (ifc.isInterface() == false) {
93                 throw new IllegalArgumentException(ifc + " is not an interface");
94             }
95             T annotation = ifc.getAnnotation(annotationType);
96             if (annotation != null) {
97                 result.add(annotation);
98             }
99         }
100         return result;
101     }
102
103     /**
104      * @return empty string if no annotation is found, or list of descriptions
105      *         separated by newline
106      */
107     static String aggregateDescriptions(final List<Description> descriptions) {
108         StringBuilder builder = new StringBuilder();
109         for (Description d : descriptions) {
110             if (builder.length() != 0) {
111                 builder.append("\n");
112             }
113             builder.append(d.value());
114
115         }
116         return builder.toString();
117     }
118 }