|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.context.properties; |
| 18 | + |
| 19 | +import java.beans.PropertyDescriptor; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +import org.springframework.beans.BeanUtils; |
| 26 | +import org.springframework.beans.factory.BeanCreationException; |
| 27 | +import org.springframework.boot.context.properties.source.ConfigurationPropertyName; |
| 28 | +import org.springframework.core.annotation.AnnotationUtils; |
| 29 | +import org.springframework.util.ClassUtils; |
| 30 | +import org.springframework.util.ReflectionUtils; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Utility class that reads {@link org.springframework.boot.context.properties.ConfigurationPropertyValue} annotations |
| 35 | + * from getters and fields of a bean and provides methods to access the metadata contained in the annotations. |
| 36 | + * |
| 37 | + * @author Tom Hombergs |
| 38 | + * @since 2.0.0 |
| 39 | + * @see org.springframework.boot.context.properties.ConfigurationPropertyValue |
| 40 | + */ |
| 41 | +public class ConfigurationPropertyValueReader { |
| 42 | + |
| 43 | + private Map<ConfigurationPropertyName, ConfigurationPropertyValue> annotations = new HashMap<>(); |
| 44 | + |
| 45 | + public ConfigurationPropertyValueReader(Object bean, String beanName, String prefix) { |
| 46 | + this.annotations = findAnnotations(bean, beanName, prefix); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns a map that maps configuration property names to their fallback properties. If this map does not contain |
| 51 | + * a value for a certain configuration property, it means that there is no fallback specified for this property. |
| 52 | + * |
| 53 | + * @return a map of configuration property names to their fallback property names. |
| 54 | + */ |
| 55 | + public Map<ConfigurationPropertyName, ConfigurationPropertyName> getPropertyFallbacks() { |
| 56 | + return this.annotations.entrySet().stream() |
| 57 | + .filter(entry -> !StringUtils.isEmpty(entry.getValue().fallback())) |
| 58 | + .collect(Collectors.toMap(Map.Entry::getKey, entry -> ConfigurationPropertyName.of(entry.getValue().fallback()))); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Walks through the methods and fields of the specified bean to extract {@link ConfigurationPropertyValue} |
| 63 | + * annotations. A field can be annotated either by directly annotating the field or by annotating |
| 64 | + * the corresponding getter or setter method. This method will throw a {@link BeanCreationException} if |
| 65 | + * multiple annotations are found for the same field. |
| 66 | + * |
| 67 | + * @param bean the bean whose annotations to retrieve. |
| 68 | + * @param beanName the name of the bean (only used for proper error messages). |
| 69 | + * @param prefix the prefix of the superordinate {@link ConfigurationProperties} annotation. May be null. |
| 70 | + * @return a map that maps configuration property names to the annotations that were found for them. |
| 71 | + * @throws BeanCreationException if multiple {@link ConfigurationPropertyValue} annotations have been found for |
| 72 | + * the same field. |
| 73 | + */ |
| 74 | + private Map<ConfigurationPropertyName, ConfigurationPropertyValue> findAnnotations(Object bean, String beanName, String prefix) { |
| 75 | + Map<ConfigurationPropertyName, ConfigurationPropertyValue> fieldAnnotations = new HashMap<>(); |
| 76 | + ReflectionUtils.doWithMethods(bean.getClass(), method -> { |
| 77 | + ConfigurationPropertyValue annotation = AnnotationUtils |
| 78 | + .findAnnotation(method, ConfigurationPropertyValue.class); |
| 79 | + if (annotation != null) { |
| 80 | + PropertyDescriptor propertyDescriptor = findPropertyDescriptorOrFail( |
| 81 | + beanName, method); |
| 82 | + ConfigurationPropertyName name = getConfigurationPropertyName(prefix, propertyDescriptor.getName()); |
| 83 | + if (fieldAnnotations.containsKey(name)) { |
| 84 | + throw new BeanCreationException(beanName, "Invalid use of " |
| 85 | + + ClassUtils |
| 86 | + .getShortName(ConfigurationPropertyValue.class) |
| 87 | + + " on method '" + method.getName() |
| 88 | + + "'. You may either annotate a field, a getter or a setter but not two of these."); |
| 89 | + } |
| 90 | + fieldAnnotations.put(name, annotation); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + ReflectionUtils.doWithFields(bean.getClass(), field -> { |
| 95 | + ConfigurationPropertyValue annotation = AnnotationUtils |
| 96 | + .findAnnotation(field, ConfigurationPropertyValue.class); |
| 97 | + if (annotation != null) { |
| 98 | + ConfigurationPropertyName name = getConfigurationPropertyName(prefix, field.getName()); |
| 99 | + if (fieldAnnotations.containsKey(name)) { |
| 100 | + throw new BeanCreationException(beanName, "Invalid use of " |
| 101 | + + ClassUtils |
| 102 | + .getShortName(ConfigurationPropertyValue.class) |
| 103 | + + " on field '" + field.getName() |
| 104 | + + "'. You may either annotate a field, a getter or a setter but not two of these."); |
| 105 | + } |
| 106 | + fieldAnnotations.put(name, annotation); |
| 107 | + } |
| 108 | + }); |
| 109 | + return fieldAnnotations; |
| 110 | + } |
| 111 | + |
| 112 | + private PropertyDescriptor findPropertyDescriptorOrFail(String beanName, |
| 113 | + Method method) { |
| 114 | + PropertyDescriptor propertyDescriptor = BeanUtils.findPropertyForMethod(method); |
| 115 | + if (propertyDescriptor == null) { |
| 116 | + throw new BeanCreationException(beanName, "Invalid use of " |
| 117 | + + ClassUtils.getShortName(ConfigurationPropertyValue.class) |
| 118 | + + " on method '" + method.getName() |
| 119 | + + "'. This annotation may only be used on getter and setter methods or fields."); |
| 120 | + } |
| 121 | + return propertyDescriptor; |
| 122 | + } |
| 123 | + |
| 124 | + private ConfigurationPropertyName getConfigurationPropertyName(String prefix, String fieldName) { |
| 125 | + if (StringUtils.isEmpty(prefix)) { |
| 126 | + return ConfigurationPropertyName.of(fieldName); |
| 127 | + } |
| 128 | + else { |
| 129 | + return ConfigurationPropertyName.of(prefix + "." + fieldName); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments