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 java.lang.reflect.Method;
11 import java.util.HashSet;
12 import java.util.List;
14 import javax.annotation.Nullable;
15 import javax.annotation.concurrent.Immutable;
16 import javax.management.MBeanAttributeInfo;
17 import javax.management.ObjectName;
18 import org.opendaylight.controller.config.api.annotations.Description;
19 import org.opendaylight.controller.config.api.annotations.RequireInterface;
22 class AttributeHolder {
24 private final String name;
25 private final String description;
26 private final Object object;
27 private final boolean writable;
30 private final RequireInterface requireInterfaceAnnotation;
31 private final String attributeType;
33 protected static final Set<Class<?>> PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER = new HashSet<>();
36 PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.add(ObjectName.class);
37 PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.add(ObjectName[].class);
38 PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.add(List.class);
41 public AttributeHolder(final String name, final Object object, final String returnType,
42 final boolean writable,
43 @Nullable final RequireInterface requireInterfaceAnnotation,
44 final String description) {
46 throw new NullPointerException();
50 throw new NullPointerException();
53 this.writable = writable;
54 this.requireInterfaceAnnotation = requireInterfaceAnnotation;
55 this.attributeType = returnType;
56 this.description = description;
59 public MBeanAttributeInfo toMBeanAttributeInfo() {
60 return new MBeanAttributeInfo(name, attributeType,
61 description, true, true, false);
65 * @return annotation if setter sets ObjectName or ObjectName[], and is
66 * annotated. Return null otherwise.
68 RequireInterface getRequireInterfaceOrNull() {
69 return requireInterfaceAnnotation;
72 public String getName() {
76 public Object getObject() {
80 public String getAttributeType() {
84 public boolean isWritable() {
88 public String getDescription() {
93 * Find @Description annotations in method class and all its exported
97 * @param jmxInterfaces
98 * @return empty string if no annotation is found, or list of descriptions
99 * separated by newline
101 static String findDescription(final Method setter, final Set<Class<?>> jmxInterfaces) {
102 List<Description> descriptions = AnnotationsHelper
103 .findMethodAnnotationInSuperClassesAndIfcs(setter, Description.class, jmxInterfaces);
104 return AnnotationsHelper.aggregateDescriptions(descriptions);
108 * Find @RequireInterface annotation by searching method class and all
109 * exported interfaces.
112 * @param inspectedInterfaces
113 * @return null if no annotation is found, otherwise return the annotation
114 * @throws IllegalStateException if more than one value is specified by found annotations
115 * @throws IllegalArgumentException if set of exported interfaces contains non interface type
117 static RequireInterface findRequireInterfaceAnnotation(final Method setter,
118 final Set<Class<?>> inspectedInterfaces) {
120 // only allow setX(ObjectName y) or setX(ObjectName[] y) or setX(List<ObjectName> y) to continue
122 if (setter.getParameterTypes().length > 1 ||
123 !PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.contains(setter.getParameterTypes()[0])) {
127 List<RequireInterface> foundRequireInterfaces = AnnotationsHelper
128 .findMethodAnnotationInSuperClassesAndIfcs(setter, RequireInterface.class, inspectedInterfaces);
129 // make sure the list if not empty contains always annotation with same
131 Set<Class<?>> foundValues = new HashSet<>();
132 for (RequireInterface ri : foundRequireInterfaces) {
133 foundValues.add(ri.value());
135 if (foundValues.isEmpty()) {
137 } else if (foundValues.size() > 1) {
138 throw new IllegalStateException("Error finding @RequireInterface. "
139 + "More than one value specified as required interface "
140 + foundValues + " of " + setter + " of "
141 + setter.getDeclaringClass());
143 return foundRequireInterfaces.get(0);