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