View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.metadata.annotations;
18  
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Method;
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  import org.springframework.core.annotation.AnnotationUtils;
26  
27  import ch.elca.el4j.util.metadata.Attributes;
28  
29  /**
30   * Helper class to get annotations of a field, method, and class. Annotations
31   * can also fetched filtered.
32   *
33   * @svnLink $Revision: 4091 $;$Date: 2010-01-15 12:21:07 +0100 (Fr, 15. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/metadata/annotations/Annotations.java $
34   *
35   * @author Martin Zeltner (MZE)
36   */
37  public class Annotations implements Attributes {
38  	/**
39  	 * {@inheritDoc}
40  	 */
41  	public Collection getAttributes(Class targetClass) {
42  		return Arrays.asList(targetClass.getAnnotations());
43  	}
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	public Collection getAttributes(Method targetMethod) {
49  		return Arrays.asList(AnnotationUtils.getAnnotations(targetMethod));
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 */
55  	public Collection getAttributes(Field targetField) {
56  		return Arrays.asList(targetField.getAnnotations());
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	public Collection getAttributes(Class targetClass, Class filter) {
63  		return filter(getAttributes(targetClass), filter);
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 */
69  	public Collection getAttributes(Method targetMethod, Class filter) {
70  		return filter(getAttributes(targetMethod), filter);
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	public Collection getAttributes(Field targetField, Class filter) {
77  		return filter(getAttributes(targetField), filter);
78  	}
79  
80  	/**
81  	 * Filters the given collection. Only items assignable to the given filter
82  	 * class will stay in collection.
83  	 *
84  	 * @param c Is the collection to filter.
85  	 * @param filter Is the class used for filtering.
86  	 * @return Returns the filtered collection.
87  	 */
88  	@SuppressWarnings("unchecked")
89  	protected Collection filter(Collection c, Class filter) {
90  		Iterator it = c.iterator();
91  		while (it.hasNext()) {
92  			Object a = it.next();
93  			if (!filter.isAssignableFrom(a.getClass())) {
94  				it.remove();
95  			}
96  		}
97  		return c;
98  	}
99  }