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.util.ReflectionUtils; 029import org.springframework.util.StringValueResolver; 030 031/** 032 * General utility methods for working with annotations in JavaBeans style. 033 * 034 * @author Rob Harrop 035 * @author Juergen Hoeller 036 * @since 2.0 037 */ 038public abstract class AnnotationBeanUtils { 039 040 /** 041 * Copy the properties of the supplied {@link Annotation} to the supplied target bean. 042 * Any properties defined in {@code excludedProperties} will not be copied. 043 * @param ann the annotation to copy from 044 * @param bean the bean instance to copy to 045 * @param excludedProperties the names of excluded properties, if any 046 * @see org.springframework.beans.BeanWrapper 047 */ 048 public static void copyPropertiesToBean(Annotation ann, Object bean, String... excludedProperties) { 049 copyPropertiesToBean(ann, bean, null, excludedProperties); 050 } 051 052 /** 053 * Copy the properties of the supplied {@link Annotation} to the supplied target bean. 054 * Any properties defined in {@code excludedProperties} will not be copied. 055 * <p>A specified value resolver may resolve placeholders in property values, for example. 056 * @param ann the annotation to copy from 057 * @param bean the bean instance to copy to 058 * @param valueResolver a resolve to post-process String property values (may be {@code null}) 059 * @param excludedProperties the names of excluded properties, if any 060 * @see org.springframework.beans.BeanWrapper 061 */ 062 public static void copyPropertiesToBean(Annotation ann, Object bean, StringValueResolver valueResolver, 063 String... excludedProperties) { 064 065 Set<String> excluded = (excludedProperties.length == 0 ? Collections.<String>emptySet() : 066 new HashSet<String>(Arrays.asList(excludedProperties))); 067 Method[] annotationProperties = ann.annotationType().getDeclaredMethods(); 068 BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); 069 for (Method annotationProperty : annotationProperties) { 070 String propertyName = annotationProperty.getName(); 071 if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) { 072 Object value = ReflectionUtils.invokeMethod(annotationProperty, ann); 073 if (valueResolver != null && value instanceof String) { 074 value = valueResolver.resolveStringValue((String) value); 075 } 076 bw.setPropertyValue(propertyName, value); 077 } 078 } 079 } 080 081}