diff --git a/spring-beans/src/main/java/org/springframework/beans/annotation/AnnotationBeanUtils.java b/spring-beans/src/main/java/org/springframework/beans/annotation/AnnotationBeanUtils.java index 5dd0fb33aa98..c0b22ad47d37 100644 --- a/spring-beans/src/main/java/org/springframework/beans/annotation/AnnotationBeanUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/annotation/AnnotationBeanUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -34,7 +35,9 @@ * @author Rob Harrop * @author Juergen Hoeller * @since 2.0 + * @deprecated as of 5.2, in favor of custom annotation attribute processing */ +@Deprecated public abstract class AnnotationBeanUtils { /** @@ -62,7 +65,8 @@ public static void copyPropertiesToBean(Annotation ann, Object bean, String... e public static void copyPropertiesToBean(Annotation ann, Object bean, @Nullable StringValueResolver valueResolver, String... excludedProperties) { - Set excluded = new HashSet<>(Arrays.asList(excludedProperties)); + Set excluded = (excludedProperties.length == 0 ? Collections.emptySet() : + new HashSet<>(Arrays.asList(excludedProperties))); Method[] annotationProperties = ann.annotationType().getDeclaredMethods(); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); for (Method annotationProperty : annotationProperties) { diff --git a/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java b/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java index 55fcff5e8db8..30f6446bf156 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/annotation/AnnotationJmxAttributeSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,19 +17,30 @@ package org.springframework.jmx.export.annotation; import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.Collection; -import java.util.Set; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import org.springframework.beans.BeanUtils; -import org.springframework.beans.annotation.AnnotationBeanUtils; +import org.springframework.beans.BeanWrapper; +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.PropertyAccessorFactory; +import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.EmbeddedValueResolver; -import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.AnnotationFilter; +import org.springframework.core.annotation.MergedAnnotation; +import org.springframework.core.annotation.MergedAnnotationPredicates; +import org.springframework.core.annotation.MergedAnnotations; +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; +import org.springframework.core.annotation.RepeatableContainers; import org.springframework.jmx.export.metadata.InvalidMetadataException; import org.springframework.jmx.export.metadata.JmxAttributeSource; import org.springframework.lang.Nullable; @@ -65,46 +76,69 @@ public void setBeanFactory(BeanFactory beanFactory) { @Override @Nullable public org.springframework.jmx.export.metadata.ManagedResource getManagedResource(Class beanClass) throws InvalidMetadataException { - ManagedResource ann = AnnotationUtils.findAnnotation(beanClass, ManagedResource.class); - if (ann == null) { + MergedAnnotation ann = MergedAnnotations.from(beanClass, SearchStrategy.EXHAUSTIVE) + .get(ManagedResource.class).withNonMergedAttributes(); + if (!ann.isPresent()) { return null; } - Class declaringClass = AnnotationUtils.findAnnotationDeclaringClass(ManagedResource.class, beanClass); + Class declaringClass = (Class) ann.getSource(); Class target = (declaringClass != null && !declaringClass.isInterface() ? declaringClass : beanClass); if (!Modifier.isPublic(target.getModifiers())) { throw new InvalidMetadataException("@ManagedResource class '" + target.getName() + "' must be public"); } - org.springframework.jmx.export.metadata.ManagedResource managedResource = new org.springframework.jmx.export.metadata.ManagedResource(); - AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource, this.embeddedValueResolver); - return managedResource; + + org.springframework.jmx.export.metadata.ManagedResource bean = new org.springframework.jmx.export.metadata.ManagedResource(); + Map map = ann.asMap(); + List list = new ArrayList<>(map.size()); + map.forEach((attrName, attrValue) -> { + if (!"value".equals(attrName)) { + Object value = attrValue; + if (this.embeddedValueResolver != null && value instanceof String) { + value = this.embeddedValueResolver.resolveStringValue((String) value); + } + list.add(new PropertyValue(attrName, value)); + } + }); + PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(new MutablePropertyValues(list)); + return bean; } @Override @Nullable public org.springframework.jmx.export.metadata.ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException { - ManagedAttribute ann = AnnotationUtils.findAnnotation(method, ManagedAttribute.class); - if (ann == null) { + MergedAnnotation ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE) + .get(ManagedAttribute.class).withNonMergedAttributes(); + if (!ann.isPresent()) { return null; } - org.springframework.jmx.export.metadata.ManagedAttribute managedAttribute = new org.springframework.jmx.export.metadata.ManagedAttribute(); - AnnotationBeanUtils.copyPropertiesToBean(ann, managedAttribute, "defaultValue"); - if (ann.defaultValue().length() > 0) { - managedAttribute.setDefaultValue(ann.defaultValue()); + + org.springframework.jmx.export.metadata.ManagedAttribute bean = new org.springframework.jmx.export.metadata.ManagedAttribute(); + Map map = ann.asMap(); + MutablePropertyValues pvs = new MutablePropertyValues(map); + pvs.removePropertyValue("defaultValue"); + PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(pvs); + String defaultValue = (String) map.get("defaultValue"); + if (defaultValue.length() > 0) { + bean.setDefaultValue(defaultValue); } - return managedAttribute; + return bean; } @Override @Nullable public org.springframework.jmx.export.metadata.ManagedMetric getManagedMetric(Method method) throws InvalidMetadataException { - ManagedMetric ann = AnnotationUtils.findAnnotation(method, ManagedMetric.class); + MergedAnnotation ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE) + .get(ManagedMetric.class).withNonMergedAttributes(); + return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedMetric.class); } @Override @Nullable public org.springframework.jmx.export.metadata.ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException { - ManagedOperation ann = AnnotationUtils.findAnnotation(method, ManagedOperation.class); + MergedAnnotation ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE) + .get(ManagedOperation.class).withNonMergedAttributes(); + return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedOperation.class); } @@ -112,8 +146,9 @@ public org.springframework.jmx.export.metadata.ManagedOperation getManagedOperat public org.springframework.jmx.export.metadata.ManagedOperationParameter[] getManagedOperationParameters(Method method) throws InvalidMetadataException { - Set anns = AnnotationUtils.getRepeatableAnnotations( + List> anns = getRepeatableAnnotations( method, ManagedOperationParameter.class, ManagedOperationParameters.class); + return copyPropertiesToBeanArray(anns, org.springframework.jmx.export.metadata.ManagedOperationParameter.class); } @@ -121,29 +156,45 @@ public org.springframework.jmx.export.metadata.ManagedOperationParameter[] getMa public org.springframework.jmx.export.metadata.ManagedNotification[] getManagedNotifications(Class clazz) throws InvalidMetadataException { - Set anns = AnnotationUtils.getRepeatableAnnotations( + List> anns = getRepeatableAnnotations( clazz, ManagedNotification.class, ManagedNotifications.class); + return copyPropertiesToBeanArray(anns, org.springframework.jmx.export.metadata.ManagedNotification.class); } + private static List> getRepeatableAnnotations( + AnnotatedElement annotatedElement, Class annotationType, + Class containerAnnotationType) { + + return MergedAnnotations.from(annotatedElement, SearchStrategy.EXHAUSTIVE, + RepeatableContainers.of(annotationType, containerAnnotationType), AnnotationFilter.PLAIN) + .stream(annotationType) + .filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex)) + .map(MergedAnnotation::withNonMergedAttributes) + .collect(Collectors.toList()); + } + @SuppressWarnings("unchecked") - private static T[] copyPropertiesToBeanArray(Collection anns, Class beanClass) { + private static T[] copyPropertiesToBeanArray( + List> anns, Class beanClass) { + T[] beans = (T[]) Array.newInstance(beanClass, anns.size()); int i = 0; - for (Annotation ann : anns) { + for (MergedAnnotation ann : anns) { beans[i++] = copyPropertiesToBean(ann, beanClass); } return beans; } @Nullable - private static T copyPropertiesToBean(@Nullable Annotation ann, Class beanClass) { - if (ann == null) { + private static T copyPropertiesToBean(MergedAnnotation ann, Class beanClass) { + if (!ann.isPresent()) { return null; } T bean = BeanUtils.instantiateClass(beanClass); - AnnotationBeanUtils.copyPropertiesToBean(ann, bean); + BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); + bw.setPropertyValues(new MutablePropertyValues(ann.asMap())); return bean; }