2 * Copyright (c) 2013 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 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.
27 * Used for finding @Description and @RequireInterface
30 * generic type of annotation
31 * @return list of found annotations
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();
40 Method foundSetter = inspectedClass.getMethod(setter.getName(),
41 setter.getParameterTypes());
42 T annotation = foundSetter.getAnnotation(annotationType);
43 if (annotation != null) {
44 result.add(annotation);
46 // we need to go deeper
47 inspectedClass = inspectedClass.getSuperclass();
48 } catch (NoSuchMethodException e) {
49 inspectedClass = Object.class; // no need to go further
51 } while (!inspectedClass.equals(Object.class));
54 for (Class<?> ifc : inspectedInterfaces) {
55 if (ifc.isInterface() == false) {
56 throw new IllegalArgumentException(ifc + " is not an interface");
59 Method foundSetter = ifc.getMethod(setter.getName(),
60 setter.getParameterTypes());
61 T annotation = foundSetter.getAnnotation(annotationType);
62 if (annotation != null) {
63 result.add(annotation);
65 } catch (NoSuchMethodException e) {
69 return new ArrayList<>(result.build());
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.
77 * @return list of found annotations
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;
84 T annotation = declaringClass.getAnnotation(annotationType);
85 if (annotation != null) {
86 result.add(annotation);
88 declaringClass = declaringClass.getSuperclass();
89 } while (declaringClass.equals(Object.class) == false);
91 for (Class<?> ifc : interfaces) {
92 if (ifc.isInterface() == false) {
93 throw new IllegalArgumentException(ifc + " is not an interface");
95 T annotation = ifc.getAnnotation(annotationType);
96 if (annotation != null) {
97 result.add(annotation);
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();