001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.annotation;
018
019import java.lang.annotation.Annotation;
020import java.lang.reflect.Method;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.HashSet;
024import java.util.Set;
025
026import org.springframework.beans.BeanWrapper;
027import org.springframework.beans.PropertyAccessorFactory;
028import org.springframework.lang.Nullable;
029import org.springframework.util.ReflectionUtils;
030import org.springframework.util.StringValueResolver;
031
032/**
033 * General utility methods for working with annotations in JavaBeans style.
034 *
035 * @author Rob Harrop
036 * @author Juergen Hoeller
037 * @since 2.0
038 * @deprecated as of 5.2, in favor of custom annotation attribute processing
039 */
040@Deprecated
041public abstract class AnnotationBeanUtils {
042
043        /**
044         * Copy the properties of the supplied {@link Annotation} to the supplied target bean.
045         * Any properties defined in {@code excludedProperties} will not be copied.
046         * @param ann the annotation to copy from
047         * @param bean the bean instance to copy to
048         * @param excludedProperties the names of excluded properties, if any
049         * @see org.springframework.beans.BeanWrapper
050         */
051        public static void copyPropertiesToBean(Annotation ann, Object bean, String... excludedProperties) {
052                copyPropertiesToBean(ann, bean, null, excludedProperties);
053        }
054
055        /**
056         * Copy the properties of the supplied {@link Annotation} to the supplied target bean.
057         * Any properties defined in {@code excludedProperties} will not be copied.
058         * <p>A specified value resolver may resolve placeholders in property values, for example.
059         * @param ann the annotation to copy from
060         * @param bean the bean instance to copy to
061         * @param valueResolver a resolve to post-process String property values (may be {@code null})
062         * @param excludedProperties the names of excluded properties, if any
063         * @see org.springframework.beans.BeanWrapper
064         */
065        public static void copyPropertiesToBean(Annotation ann, Object bean, @Nullable StringValueResolver valueResolver,
066                        String... excludedProperties) {
067
068                Set<String> excluded = (excludedProperties.length == 0 ? Collections.emptySet() :
069                                new HashSet<>(Arrays.asList(excludedProperties)));
070                Method[] annotationProperties = ann.annotationType().getDeclaredMethods();
071                BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean);
072                for (Method annotationProperty : annotationProperties) {
073                        String propertyName = annotationProperty.getName();
074                        if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) {
075                                Object value = ReflectionUtils.invokeMethod(annotationProperty, ann);
076                                if (valueResolver != null && value instanceof String) {
077                                        value = valueResolver.resolveStringValue((String) value);
078                                }
079                                bw.setPropertyValue(propertyName, value);
080                        }
081                }
082        }
083
084}