2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
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;
17 import org.opendaylight.controller.config.api.annotations.Description;
19 public final class AnnotationsHelper {
21 private AnnotationsHelper() {
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
30 * generic type of annotation
31 * @return list of found annotations
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();
39 Method foundSetter = inspectedClass.getMethod(setter.getName(), setter.getParameterTypes());
40 T annotation = foundSetter.getAnnotation(annotationType);
41 if (annotation != null) {
42 result.add(annotation);
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;
50 } while (!inspectedClass.equals(Object.class));
53 for (Class<?> ifc : inspectedInterfaces) {
54 if (!ifc.isInterface()) {
55 throw new IllegalArgumentException(ifc + " is not an interface");
58 Method foundSetter = ifc.getMethod(setter.getName(), setter.getParameterTypes());
59 T annotation = foundSetter.getAnnotation(annotationType);
60 if (annotation != null) {
61 result.add(annotation);
63 } catch (final NoSuchMethodException e) {
64 break; // FIXME: is this ok?
67 return new ArrayList<>(result.build());
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.
75 * @return list of found annotations
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;
82 T annotation = declaringClass.getAnnotation(annotationType);
83 if (annotation != null) {
84 result.add(annotation);
86 declaringClass = declaringClass.getSuperclass();
87 } while (!declaringClass.equals(Object.class));
89 for (Class<?> ifc : interfaces) {
90 if (!ifc.isInterface()) {
91 throw new IllegalArgumentException(ifc + " is not an interface");
93 T annotation = ifc.getAnnotation(annotationType);
94 if (annotation != null) {
95 result.add(annotation);
102 * Lists aggregate descriptions.
104 * @return empty string if no annotation is found, or list of descriptions
105 * separated by newline
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");
113 builder.append(d.value());
116 return builder.toString();