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