View Javadoc
1   package nl.tudelft.simulation.introspection.beans;
2   
3   import java.beans.BeanInfo;
4   import java.beans.PropertyDescriptor;
5   import java.util.LinkedHashSet;
6   import java.util.Set;
7   
8   import nl.tudelft.simulation.introspection.DelegateIntrospection;
9   import nl.tudelft.simulation.introspection.Introspector;
10  import nl.tudelft.simulation.introspection.Property;
11  
12  /**
13   * The Bean introspector provides a simplified JavaBean implementation of the introspection interfaces. Its behavior adheres to
14   * the following:
15   * <ul>
16   * <li>Properties are discovered by searching for 'getter' and / or 'setter' methods</li>
17   * <li>Property value are manipulated via a property's 'setter' method. If no such method is found, the property cannot be
18   * altered</li>
19   * <li>Indexed properties are probably not correctly supported.</li>
20   * </ul>
21   * <p>
22   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
23   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
24   * project is distributed under a three-clause BSD-style license, which can be found at
25   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
26   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
27   * </p>
28   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
29   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30   * @author Niels Lang.
31   * @since 1.5
32   */
33  public class BeanIntrospector implements Introspector
34  {
35      /** {@inheritDoc} */
36      @Override
37      public Property[] getProperties(final Object introspectedObject)
38      {
39          Object introspected = introspectedObject;
40          while (introspected instanceof DelegateIntrospection)
41          {
42              introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
43          }
44          Set<Property> props = new LinkedHashSet<Property>();
45          try
46          {
47              BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
48  
49              PropertyDescriptor[] descrips = info.getPropertyDescriptors();
50              for (int i = 0; i < descrips.length; i++)
51              {
52                  props.add(new BeanProperty(introspected, descrips[i]));
53              }
54          }
55          catch (Exception e)
56          {
57              throw new IllegalArgumentException(this + " - getProperties", e);
58          }
59          return props.toArray(new Property[props.size()]);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public Property getProperty(final Object introspectedObject, final String property)
65      {
66          Object introspected = introspectedObject;
67          while (introspected instanceof DelegateIntrospection)
68          {
69              introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
70          }
71          try
72          {
73              BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
74              PropertyDescriptor[] descrips = info.getPropertyDescriptors();
75              for (int i = 0; i < descrips.length; i++)
76              {
77                  if (descrips[i].getName().equals(property))
78                  {
79                      return new BeanProperty(introspected, descrips[i]);
80                  }
81              }
82          }
83          catch (Exception e)
84          {
85              throw new IllegalArgumentException(this + " - getProperty", e);
86          }
87          throw new IllegalArgumentException("Property '" + property + "' not found for " + introspected);
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public String[] getPropertyNames(final Object introspectedObject)
93      {
94          Object introspected = introspectedObject;
95          while (introspected instanceof DelegateIntrospection)
96          {
97              introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
98          }
99          Set<String> props = new LinkedHashSet<String>();
100         try
101         {
102             BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
103             PropertyDescriptor[] descrips = info.getPropertyDescriptors();
104             for (int i = 0; i < descrips.length; i++)
105             {
106                 props.add(descrips[i].getName());
107             }
108         }
109         catch (Exception e)
110         {
111             throw new IllegalArgumentException(this + " - getPropertyNames", e);
112         }
113         return props.toArray(new String[props.size()]);
114     }
115 }