KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > annotation > AnnotationBeanUtils


1 /*
2  * Copyright 2002-2006 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 JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
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 JavaDoc ann, Object JavaDoc bean, String JavaDoc... excludedProperties) {
43         Set JavaDoc<String JavaDoc> excluded = new HashSet JavaDoc<String JavaDoc>(Arrays.asList(excludedProperties));
44         Method JavaDoc[] annotationProperties = ann.annotationType().getDeclaredMethods();
45
46         BeanWrapper bw = new BeanWrapperImpl(bean);
47         for (Method JavaDoc annotationProperty : annotationProperties) {
48             String JavaDoc propertyName = annotationProperty.getName();
49
50             if ((!excluded.contains(propertyName)) && bw.isWritableProperty(propertyName)) {
51                 Object JavaDoc value = ReflectionUtils.invokeMethod(annotationProperty, ann);
52                 bw.setPropertyValue(propertyName, value);
53             }
54         }
55     }
56
57 }
58
Popular Tags