View Javadoc

1   /*
2    * Copyright 2012 Vincent Demeester<vincent+shortbrain@demeester.fr>.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.shortbrain.vaadin.container.property;
17  
18  import java.lang.reflect.Field;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  /**
25   * Implementation of {@link PropertyReaderAlgorithm} that looks for attribute of
26   * the beanClass
27   * 
28   * @author Vincent Demeester <vincent@demeester.fr>
29   * 
30   */
31  public class AttributeReaderAlgorithm implements PropertyReaderAlgorithm {
32  
33  	/**
34  	 * Constant for default valuer of withSuper attribute.
35  	 */
36  	private static final boolean DEFAULT_WITHSUPER = true;
37  	private static final List<String> DEFAULT_IGNORED = Arrays
38  			.asList(new String[] { "serialVersionUID" });
39  
40  	/**
41  	 * List of attribute to ignore.
42  	 */
43  	private final Collection<String> ignoredAttributes;
44  	private final boolean withSuper;
45  
46  	/**
47  	 * Create an {@link AttributeReaderAlgorithm} with default ignored
48  	 * attributes ("serialVersionUID") and with <code>super</code> class lookup.
49  	 */
50  	public AttributeReaderAlgorithm() {
51  		this(DEFAULT_IGNORED, DEFAULT_WITHSUPER);
52  	}
53  
54  	/**
55  	 * Create an {@link AttributeReaderAlgorithm} with default ignored
56  	 * attributes ("serialVersionUID").
57  	 * 
58  	 * @param withSuper
59  	 *            look up for <code>super</class> class.
60  	 */
61  	public AttributeReaderAlgorithm(boolean withSuper) {
62  		this(DEFAULT_IGNORED, withSuper);
63  	}
64  
65  	/**
66  	 * Create an {@link AttributeReaderAlgorithm} with <code>super</code> class
67  	 * lookup.
68  	 * 
69  	 * @param ignoredAttributes
70  	 *            list of attributes to ignore.
71  	 */
72  	public AttributeReaderAlgorithm(Collection<String> ignoredAttributes) {
73  		this(ignoredAttributes, DEFAULT_WITHSUPER);
74  	}
75  
76  	/**
77  	 * Create an {@link AttributeReaderAlgorithm}.
78  	 * 
79  	 * @param ignoredAttributes
80  	 *            list of attributes to ignore.
81  	 * @param withSuper
82  	 *            look up for <code>super</class> class.
83  	 */
84  	public AttributeReaderAlgorithm(Collection<String> ignoredAttributes,
85  			boolean withSuper) {
86  		this.ignoredAttributes = ignoredAttributes;
87  		this.withSuper = withSuper;
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 * 
93  	 * If the ignoredAttributes attribute is null, we won't fail, it will be
94  	 * just as if it is an empty List.
95  	 * 
96  	 * @throw {@link IllegalArgumentException} if beanClass is null.
97  	 */
98  	@Override
99  	public List<PropertyMetadata> getProperties(Class<?> beanClass) {
100 		if (beanClass == null) {
101 			throw new IllegalArgumentException("beanClass cannot be null.");
102 		}
103 		List<PropertyMetadata> metadatas = new LinkedList<PropertyMetadata>();
104 		List<Field> fields = new LinkedList<Field>();
105 		fields.addAll(Arrays.asList(beanClass.getDeclaredFields()));
106 		if (withSuper) {
107 			Class<?> superClass = beanClass.getSuperclass();
108 			while (superClass != Object.class) {
109 				fields.addAll(Arrays.asList(superClass.getDeclaredFields()));
110 				superClass = superClass.getSuperclass();
111 			}
112 		}
113 		for (Field field : fields) {
114 			if (ignoredAttributes == null
115 					|| !ignoredAttributes.contains(field.getName())) {
116 				String propertyName = field.getName();
117 				Class<?> propertyClass = field.getType();
118 				String propertyAttribute = field.getName();
119 				PropertyMetadata metadata = new PropertyMetadata(propertyName,
120 						propertyClass, null, propertyAttribute);
121 				metadatas.add(metadata);
122 			}
123 		}
124 		return metadatas;
125 	}
126 }