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.Method;
19  import java.lang.reflect.Modifier;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  /**
24   * Implementation of {@link PropertyReaderAlgorithm} that looks for method
25   * (getter) of the beanClass.
26   * 
27   * @author Vincent Demeester <vincent@demeester.fr>
28   * 
29   */
30  public class GetterReaderAlgorithm implements PropertyReaderAlgorithm {
31  
32  	public GetterReaderAlgorithm() {
33  	}
34  
35  	/**
36  	 * {@inheritDoc}
37  	 */
38  	@Override
39  	public List<PropertyMetadata> getProperties(Class<?> beanClass) {
40  		if (beanClass == null) {
41  			throw new IllegalArgumentException("beanClass cannot be null.");
42  		}
43  		List<PropertyMetadata> metadatas = new LinkedList<PropertyMetadata>();
44  		for (Method method : beanClass.getDeclaredMethods()) {
45  			if (Modifier.isPublic(method.getModifiers())
46  					&& method.getName().startsWith("get")) {
47  				String propertyName = method.getName().substring(3);
48  				Class<?> propertyClass = method.getReturnType();
49  				String propertyAttribute = propertyName;
50  				PropertyMetadata metadata = new PropertyMetadata(propertyName,
51  						propertyClass, null, propertyAttribute);
52  				metadatas.add(metadata);
53  			}
54  		}
55  		return metadatas;
56  	}
57  }