View Javadoc

1   /*
2    * @(#) BeanIntrospector.java Apr 15, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.introspection.beans;
11  
12  import java.beans.BeanInfo;
13  import java.beans.PropertyDescriptor;
14  import java.util.HashSet;
15  import java.util.Set;
16  
17  import nl.tudelft.simulation.introspection.Introspector;
18  import nl.tudelft.simulation.introspection.Property;
19  import nl.tudelft.simulation.logger.Logger;
20  
21  /***
22   * The Bean introspector provides a simplified JavaBean TM implementation of the
23   * introspection interfaces. Its behavior adhers to the following:
24   * <ul>
25   * <li>Properties are discovered by searching for 'getter' and / or 'setter'
26   * methods</li>
27   * <li>Property value are manipulated via a property's 'setter' method. If no
28   * such method is found, the property cannot be altered</li>
29   * <li>Indexed properties are probably not correctly supported.</li>
30   * </ul>
31   * <p>
32   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
33   * University of Technology </a>, the Netherlands. <br>
34   * See for project information <a
35   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
36   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
37   * License (GPL) </a>, no warranty <br>
38   * 
39   * @author <a
40   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
41   *         Lang </a><a
42   *         href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
43   *         Jacobs </a>
44   * @version 1.1 Apr 15, 2004
45   * @since 1.4
46   */
47  public class BeanIntrospector implements Introspector
48  {
49  	/***
50  	 * @see nl.tudelft.simulation.introspection.Introspector#getProperties(Object)
51  	 */
52  	public Property[] getProperties(final Object introspected)
53  	{
54  		Set props = new HashSet();
55  		try
56  		{
57  			BeanInfo info = java.beans.Introspector.getBeanInfo(introspected
58  					.getClass());
59  
60  
61  
62  
63  			PropertyDescriptor[] descrips = info.getPropertyDescriptors();
64  			for (int i = 0; i < descrips.length; i++)
65  			{
66  				props.add(new BeanProperty(introspected, descrips[i]));
67  			}
68  		} catch (Exception e)
69  		{
70  			Logger.warning(this, "getProperties", e);
71  		}
72  		return (Property[]) props.toArray(new Property[props.size()]);
73  	}
74  
75  	/***
76  	 * @see nl.tudelft.simulation.introspection.Introspector#getProperty(Object,
77  	 *      String)
78  	 */
79  	public Property getProperty(final Object introspected, final String property)
80  	{
81  		try
82  		{
83  			BeanInfo info = java.beans.Introspector.getBeanInfo(introspected
84  					.getClass());
85  			PropertyDescriptor[] descrips = info.getPropertyDescriptors();
86  			for (int i = 0; i < descrips.length; i++)
87  			{
88  				if (descrips[i].getName().equals(property))
89  				{
90  					return new BeanProperty(introspected, descrips[i]);
91  				}
92  			}
93  		} catch (Exception e)
94  		{
95  			Logger.warning(this, "getProperty", e);
96  		}
97  		throw new IllegalArgumentException("Property '" + property
98  				+ "' not found for " + introspected);
99  	}
100 
101 	/***
102 	 * @see nl.tudelft.simulation.introspection.Introspector#getPropertyNames(Object)
103 	 */
104 	public String[] getPropertyNames(final Object introspected)
105 	{
106 		Set props = new HashSet();
107 		try
108 		{
109 			BeanInfo info = java.beans.Introspector.getBeanInfo(introspected
110 					.getClass());
111 			PropertyDescriptor[] descrips = info.getPropertyDescriptors();
112 			for (int i = 0; i < descrips.length; i++)
113 			{
114 				props.add(descrips[i].getName());
115 			}
116 		} catch (Exception e)
117 		{
118 			Logger.warning(this, "getPropertyNames", e);
119 		}
120 		return (String[]) props.toArray(new String[props.size()]);
121 	}
122 }