Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / AnnotationsHelper.java
1 /*
2  * Copyright (c) 2013, 2017 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 final 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. Used
27      * 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(final Method setter,
34             final Class<T> annotationType, final Set<Class<?>> inspectedInterfaces) {
35         Builder<T> result = ImmutableSet.builder();
36         Class<?> inspectedClass = setter.getDeclaringClass();
37         do {
38             try {
39                 Method foundSetter = inspectedClass.getMethod(setter.getName(), 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                 // no need to go further
47             } catch (final NoSuchMethodException e) {
48                 inspectedClass = Object.class;
49             }
50         } while (!inspectedClass.equals(Object.class));
51
52         // inspect interfaces
53         for (Class<?> ifc : inspectedInterfaces) {
54             if (!ifc.isInterface()) {
55                 throw new IllegalArgumentException(ifc + " is not an interface");
56             }
57             try {
58                 Method foundSetter = ifc.getMethod(setter.getName(), setter.getParameterTypes());
59                 T annotation = foundSetter.getAnnotation(annotationType);
60                 if (annotation != null) {
61                     result.add(annotation);
62                 }
63             } catch (final NoSuchMethodException e) {
64                 break; // FIXME: is this ok?
65             }
66         }
67         return new ArrayList<>(result.build());
68     }
69
70     /**
71      * Look for annotation specified by annotationType on type. First observe class
72      * clazz, then its super classes, then all exported interfaces with their super
73      * types. Used for finding @Description of modules.
74      *
75      * @return list of found annotations
76      */
77     static <T extends Annotation> List<T> findClassAnnotationInSuperClassesAndIfcs(final Class<?> clazz,
78             final Class<T> annotationType, final Set<Class<?>> interfaces) {
79         List<T> result = new ArrayList<>();
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));
88         // inspect interfaces
89         for (Class<?> ifc : interfaces) {
90             if (!ifc.isInterface()) {
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      * Lists aggregate descriptions.
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 }