|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.jmx.export.annotation;
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
| 20 | +import java.lang.reflect.AnnotatedElement; |
20 | 21 | import java.lang.reflect.Array;
|
21 | 22 | import java.lang.reflect.Method;
|
22 | 23 | import java.lang.reflect.Modifier;
|
23 |
| -import java.util.Collection; |
24 |
| -import java.util.Set; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.stream.Collectors; |
25 | 28 |
|
26 | 29 | import org.springframework.beans.BeanUtils;
|
27 |
| -import org.springframework.beans.annotation.AnnotationBeanUtils; |
| 30 | +import org.springframework.beans.BeanWrapper; |
| 31 | +import org.springframework.beans.MutablePropertyValues; |
| 32 | +import org.springframework.beans.PropertyAccessorFactory; |
| 33 | +import org.springframework.beans.PropertyValue; |
28 | 34 | import org.springframework.beans.factory.BeanFactory;
|
29 | 35 | import org.springframework.beans.factory.BeanFactoryAware;
|
30 | 36 | import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
31 | 37 | import org.springframework.beans.factory.config.EmbeddedValueResolver;
|
32 |
| -import org.springframework.core.annotation.AnnotationUtils; |
| 38 | +import org.springframework.core.annotation.AnnotationFilter; |
| 39 | +import org.springframework.core.annotation.MergedAnnotation; |
| 40 | +import org.springframework.core.annotation.MergedAnnotationPredicates; |
| 41 | +import org.springframework.core.annotation.MergedAnnotations; |
| 42 | +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; |
| 43 | +import org.springframework.core.annotation.RepeatableContainers; |
33 | 44 | import org.springframework.jmx.export.metadata.InvalidMetadataException;
|
34 | 45 | import org.springframework.jmx.export.metadata.JmxAttributeSource;
|
35 | 46 | import org.springframework.lang.Nullable;
|
@@ -65,85 +76,125 @@ public void setBeanFactory(BeanFactory beanFactory) {
|
65 | 76 | @Override
|
66 | 77 | @Nullable
|
67 | 78 | public org.springframework.jmx.export.metadata.ManagedResource getManagedResource(Class<?> beanClass) throws InvalidMetadataException {
|
68 |
| - ManagedResource ann = AnnotationUtils.findAnnotation(beanClass, ManagedResource.class); |
69 |
| - if (ann == null) { |
| 79 | + MergedAnnotation<ManagedResource> ann = MergedAnnotations.from(beanClass, SearchStrategy.EXHAUSTIVE) |
| 80 | + .get(ManagedResource.class).withNonMergedAttributes(); |
| 81 | + if (!ann.isPresent()) { |
70 | 82 | return null;
|
71 | 83 | }
|
72 |
| - Class<?> declaringClass = AnnotationUtils.findAnnotationDeclaringClass(ManagedResource.class, beanClass); |
| 84 | + Class<?> declaringClass = (Class<?>) ann.getSource(); |
73 | 85 | Class<?> target = (declaringClass != null && !declaringClass.isInterface() ? declaringClass : beanClass);
|
74 | 86 | if (!Modifier.isPublic(target.getModifiers())) {
|
75 | 87 | throw new InvalidMetadataException("@ManagedResource class '" + target.getName() + "' must be public");
|
76 | 88 | }
|
77 |
| - org.springframework.jmx.export.metadata.ManagedResource managedResource = new org.springframework.jmx.export.metadata.ManagedResource(); |
78 |
| - AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource, this.embeddedValueResolver); |
79 |
| - return managedResource; |
| 89 | + |
| 90 | + org.springframework.jmx.export.metadata.ManagedResource bean = new org.springframework.jmx.export.metadata.ManagedResource(); |
| 91 | + Map<String, Object> map = ann.asMap(); |
| 92 | + List<PropertyValue> list = new ArrayList<>(map.size()); |
| 93 | + map.forEach((attrName, attrValue) -> { |
| 94 | + if (!"value".equals(attrName)) { |
| 95 | + Object value = attrValue; |
| 96 | + if (this.embeddedValueResolver != null && value instanceof String) { |
| 97 | + value = this.embeddedValueResolver.resolveStringValue((String) value); |
| 98 | + } |
| 99 | + list.add(new PropertyValue(attrName, value)); |
| 100 | + } |
| 101 | + }); |
| 102 | + PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(new MutablePropertyValues(list)); |
| 103 | + return bean; |
80 | 104 | }
|
81 | 105 |
|
82 | 106 | @Override
|
83 | 107 | @Nullable
|
84 | 108 | public org.springframework.jmx.export.metadata.ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException {
|
85 |
| - ManagedAttribute ann = AnnotationUtils.findAnnotation(method, ManagedAttribute.class); |
86 |
| - if (ann == null) { |
| 109 | + MergedAnnotation<ManagedAttribute> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE) |
| 110 | + .get(ManagedAttribute.class).withNonMergedAttributes(); |
| 111 | + if (!ann.isPresent()) { |
87 | 112 | return null;
|
88 | 113 | }
|
89 |
| - org.springframework.jmx.export.metadata.ManagedAttribute managedAttribute = new org.springframework.jmx.export.metadata.ManagedAttribute(); |
90 |
| - AnnotationBeanUtils.copyPropertiesToBean(ann, managedAttribute, "defaultValue"); |
91 |
| - if (ann.defaultValue().length() > 0) { |
92 |
| - managedAttribute.setDefaultValue(ann.defaultValue()); |
| 114 | + |
| 115 | + org.springframework.jmx.export.metadata.ManagedAttribute bean = new org.springframework.jmx.export.metadata.ManagedAttribute(); |
| 116 | + Map<String, Object> map = ann.asMap(); |
| 117 | + MutablePropertyValues pvs = new MutablePropertyValues(map); |
| 118 | + pvs.removePropertyValue("defaultValue"); |
| 119 | + PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(pvs); |
| 120 | + String defaultValue = (String) map.get("defaultValue"); |
| 121 | + if (defaultValue.length() > 0) { |
| 122 | + bean.setDefaultValue(defaultValue); |
93 | 123 | }
|
94 |
| - return managedAttribute; |
| 124 | + return bean; |
95 | 125 | }
|
96 | 126 |
|
97 | 127 | @Override
|
98 | 128 | @Nullable
|
99 | 129 | public org.springframework.jmx.export.metadata.ManagedMetric getManagedMetric(Method method) throws InvalidMetadataException {
|
100 |
| - ManagedMetric ann = AnnotationUtils.findAnnotation(method, ManagedMetric.class); |
| 130 | + MergedAnnotation<ManagedMetric> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE) |
| 131 | + .get(ManagedMetric.class).withNonMergedAttributes(); |
| 132 | + |
101 | 133 | return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedMetric.class);
|
102 | 134 | }
|
103 | 135 |
|
104 | 136 | @Override
|
105 | 137 | @Nullable
|
106 | 138 | public org.springframework.jmx.export.metadata.ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException {
|
107 |
| - ManagedOperation ann = AnnotationUtils.findAnnotation(method, ManagedOperation.class); |
| 139 | + MergedAnnotation<ManagedOperation> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE) |
| 140 | + .get(ManagedOperation.class).withNonMergedAttributes(); |
| 141 | + |
108 | 142 | return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedOperation.class);
|
109 | 143 | }
|
110 | 144 |
|
111 | 145 | @Override
|
112 | 146 | public org.springframework.jmx.export.metadata.ManagedOperationParameter[] getManagedOperationParameters(Method method)
|
113 | 147 | throws InvalidMetadataException {
|
114 | 148 |
|
115 |
| - Set<ManagedOperationParameter> anns = AnnotationUtils.getRepeatableAnnotations( |
| 149 | + List<MergedAnnotation<? extends Annotation>> anns = getRepeatableAnnotations( |
116 | 150 | method, ManagedOperationParameter.class, ManagedOperationParameters.class);
|
| 151 | + |
117 | 152 | return copyPropertiesToBeanArray(anns, org.springframework.jmx.export.metadata.ManagedOperationParameter.class);
|
118 | 153 | }
|
119 | 154 |
|
120 | 155 | @Override
|
121 | 156 | public org.springframework.jmx.export.metadata.ManagedNotification[] getManagedNotifications(Class<?> clazz)
|
122 | 157 | throws InvalidMetadataException {
|
123 | 158 |
|
124 |
| - Set<ManagedNotification> anns = AnnotationUtils.getRepeatableAnnotations( |
| 159 | + List<MergedAnnotation<? extends Annotation>> anns = getRepeatableAnnotations( |
125 | 160 | clazz, ManagedNotification.class, ManagedNotifications.class);
|
| 161 | + |
126 | 162 | return copyPropertiesToBeanArray(anns, org.springframework.jmx.export.metadata.ManagedNotification.class);
|
127 | 163 | }
|
128 | 164 |
|
129 | 165 |
|
| 166 | + private static List<MergedAnnotation<? extends Annotation>> getRepeatableAnnotations( |
| 167 | + AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType, |
| 168 | + Class<? extends Annotation> containerAnnotationType) { |
| 169 | + |
| 170 | + return MergedAnnotations.from(annotatedElement, SearchStrategy.EXHAUSTIVE, |
| 171 | + RepeatableContainers.of(annotationType, containerAnnotationType), AnnotationFilter.PLAIN) |
| 172 | + .stream(annotationType) |
| 173 | + .filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex)) |
| 174 | + .map(MergedAnnotation::withNonMergedAttributes) |
| 175 | + .collect(Collectors.toList()); |
| 176 | + } |
| 177 | + |
130 | 178 | @SuppressWarnings("unchecked")
|
131 |
| - private static <T> T[] copyPropertiesToBeanArray(Collection<? extends Annotation> anns, Class<T> beanClass) { |
| 179 | + private static <T> T[] copyPropertiesToBeanArray( |
| 180 | + List<MergedAnnotation<? extends Annotation>> anns, Class<T> beanClass) { |
| 181 | + |
132 | 182 | T[] beans = (T[]) Array.newInstance(beanClass, anns.size());
|
133 | 183 | int i = 0;
|
134 |
| - for (Annotation ann : anns) { |
| 184 | + for (MergedAnnotation<? extends Annotation> ann : anns) { |
135 | 185 | beans[i++] = copyPropertiesToBean(ann, beanClass);
|
136 | 186 | }
|
137 | 187 | return beans;
|
138 | 188 | }
|
139 | 189 |
|
140 | 190 | @Nullable
|
141 |
| - private static <T> T copyPropertiesToBean(@Nullable Annotation ann, Class<T> beanClass) { |
142 |
| - if (ann == null) { |
| 191 | + private static <T> T copyPropertiesToBean(MergedAnnotation<? extends Annotation> ann, Class<T> beanClass) { |
| 192 | + if (!ann.isPresent()) { |
143 | 193 | return null;
|
144 | 194 | }
|
145 | 195 | T bean = BeanUtils.instantiateClass(beanClass);
|
146 |
| - AnnotationBeanUtils.copyPropertiesToBean(ann, bean); |
| 196 | + BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); |
| 197 | + bw.setPropertyValues(new MutablePropertyValues(ann.asMap())); |
147 | 198 | return bean;
|
148 | 199 | }
|
149 | 200 |
|
|
0 commit comments