01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.BeanWrapperImpl;
27: import org.springframework.util.ReflectionUtils;
28:
29: /**
30: * General utility methods for working with annotations in JavaBeans style.
31: *
32: * @author Rob Harrop
33: * @since 2.0
34: */
35: public abstract class AnnotationBeanUtils {
36:
37: /**
38: * Copy the properties of the supplied {@link Annotation} to the supplied target bean.
39: * Any properties defined in <code>excludedProperties</code> will not be copied.
40: * @see BeanWrapperImpl
41: */
42: public static void copyPropertiesToBean(Annotation ann,
43: Object bean, String... excludedProperties) {
44: Set<String> excluded = new HashSet<String>(Arrays
45: .asList(excludedProperties));
46: Method[] annotationProperties = ann.annotationType()
47: .getDeclaredMethods();
48:
49: BeanWrapper bw = new BeanWrapperImpl(bean);
50: for (Method annotationProperty : annotationProperties) {
51: String propertyName = annotationProperty.getName();
52:
53: if ((!excluded.contains(propertyName))
54: && bw.isWritableProperty(propertyName)) {
55: Object value = ReflectionUtils.invokeMethod(
56: annotationProperty, ann);
57: bw.setPropertyValue(propertyName, value);
58: }
59: }
60: }
61:
62: }
|