1 /* 2 * Copyright 2002-2012 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.beans.annotation; 18 19 import java.lang.annotation.Annotation; 20 import java.lang.reflect.Method; 21 import java.util.Arrays; 22 import java.util.HashSet; 23 import java.util.Set; 24 25 import org.springframework.beans.BeanWrapper; 26 import org.springframework.beans.PropertyAccessorFactory; 27 import org.springframework.util.ReflectionUtils; 28 import org.springframework.util.StringValueResolver; 29 30 /** 31 * General utility methods for working with annotations in JavaBeans style. 32 * 33 * @author Rob Harrop 34 * @author Juergen Hoeller 35 * @since 2.0 36 */ 37 public abstract class AnnotationBeanUtils { 38 39 /** 40 * Copy the properties of the supplied {@link Annotation} to the supplied target bean. 41 * Any properties defined in {@code excludedProperties} will not be copied. 42 * @param ann the annotation to copy from 43 * @param bean the bean instance to copy to 44 * @param excludedProperties the names of excluded properties, if any 45 * @see org.springframework.beans.BeanWrapper 46 */ 47 public static void copyPropertiesToBean(Annotation ann, Object bean, String... excludedProperties) { 48 copyPropertiesToBean(ann, bean, null, excludedProperties); 49 } 50 51 /** 52 * Copy the properties of the supplied {@link Annotation} to the supplied target bean. 53 * Any properties defined in {@code excludedProperties} will not be copied. 54 * <p>A specified value resolver may resolve placeholders in property values, for example. 55 * @param ann the annotation to copy from 56 * @param bean the bean instance to copy to 57 * @param valueResolver a resolve to post-process String property values (may be {@code null}) 58 * @param excludedProperties the names of excluded properties, if any 59 * @see org.springframework.beans.BeanWrapper 60 */ 61 public static void copyPropertiesToBean(Annotation ann, Object bean, StringValueResolver valueResolver, String... excludedProperties) { 62 Set<String> excluded = new HashSet<String>(Arrays.asList(excludedProperties)); 63 Method[] annotationProperties = ann.annotationType().getDeclaredMethods(); 64 BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); 65 for (Method annotationProperty : annotationProperties) { 66 String propertyName = annotationProperty.getName(); 67 if ((!excluded.contains(propertyName)) && bw.isWritableProperty(propertyName)) { 68 Object value = ReflectionUtils.invokeMethod(annotationProperty, ann); 69 if (valueResolver != null && value instanceof String) { 70 value = valueResolver.resolveStringValue((String) value); 71 } 72 bw.setPropertyValue(propertyName, value); 73 } 74 } 75 } 76 77 }