6859e4caf3f37dd086a512651146290e8306b53a
[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                 // no need to go further
49             } catch (NoSuchMethodException e) {
50                 inspectedClass = Object.class;
51             }
52         } while (!inspectedClass.equals(Object.class));
53
54         // inspect interfaces
55         for (Class<?> ifc : inspectedInterfaces) {
56             if (!ifc.isInterface()) {
57                 throw new IllegalArgumentException(ifc + " is not an interface");
58             }
59             try {
60                 Method foundSetter = ifc.getMethod(setter.getName(),
61                         setter.getParameterTypes());
62                 T annotation = foundSetter.getAnnotation(annotationType);
63                 if (annotation != null) {
64                     result.add(annotation);
65                 }
66             } catch (NoSuchMethodException e) {
67
68             }
69         }
70         return new ArrayList<>(result.build());
71     }
72
73     /**
74      * Look for annotation specified by annotationType on type. First observe
75      * class clazz, then its super classes, then all exported interfaces with
76      * their super types. Used for finding @Description of modules.
77      *
78      * @return list of found annotations
79      */
80     static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(
81             final Class<?> clazz, final Class<T> annotationType, final Set<Class<?>> interfaces) {
82         List<T> result = new ArrayList<>();
83         Class<?> declaringClass = clazz;
84         do {
85             T annotation = declaringClass.getAnnotation(annotationType);
86             if (annotation != null) {
87                 result.add(annotation);
88             }
89             declaringClass = declaringClass.getSuperclass();
90         } while (!declaringClass.equals(Object.class));
91         // inspect interfaces
92         for (Class<?> ifc : interfaces) {
93             if (!ifc.isInterface()) {
94                 throw new IllegalArgumentException(ifc + " is not an interface");
95             }
96             T annotation = ifc.getAnnotation(annotationType);
97             if (annotation != null) {
98                 result.add(annotation);
99             }
100         }
101         return result;
102     }
103
104     /**
105      * @return empty string if no annotation is found, or list of descriptions
106      *         separated by newline
107      */
108     static String aggregateDescriptions(final List<Description> descriptions) {
109         StringBuilder builder = new StringBuilder();
110         for (Description d : descriptions) {
111             if (builder.length() != 0) {
112                 builder.append("\n");
113             }
114             builder.append(d.value());
115
116         }
117         return builder.toString();
118     }
119 }