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-2025 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/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
24   * project is distributed under a three-clause BSD-style license, which can be found at
25   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
26   * </p>
27   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
28   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29   * @author Niels Lang.
30   * @since 1.5
31   */
32  public class BeanIntrospector implements Introspector
33  {
34      @Override
35      public Property[] getProperties(final Object introspectedObject)
36      {
37          Object introspected = introspectedObject;
38          while (introspected instanceof DelegateIntrospection)
39          {
40              introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
41          }
42          Set<Property> props = new LinkedHashSet<Property>();
43          try
44          {
45              BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
46  
47              PropertyDescriptor[] descrips = info.getPropertyDescriptors();
48              for (int i = 0; i < descrips.length; i++)
49              {
50                  props.add(new BeanProperty(introspected, descrips[i]));
51              }
52          }
53          catch (Exception e)
54          {
55              throw new IllegalArgumentException(this + " - getProperties", e);
56          }
57          return props.toArray(new Property[props.size()]);
58      }
59  
60      @Override
61      public Property getProperty(final Object introspectedObject, final String property)
62      {
63          Object introspected = introspectedObject;
64          while (introspected instanceof DelegateIntrospection)
65          {
66              introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
67          }
68          try
69          {
70              BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
71              PropertyDescriptor[] descrips = info.getPropertyDescriptors();
72              for (int i = 0; i < descrips.length; i++)
73              {
74                  if (descrips[i].getName().equals(property))
75                  { return new BeanProperty(introspected, descrips[i]); }
76              }
77          }
78          catch (Exception e)
79          {
80              throw new IllegalArgumentException(this + " - getProperty", e);
81          }
82          throw new IllegalArgumentException("Property '" + property + "' not found for " + introspected);
83      }
84  
85      @Override
86      public String[] getPropertyNames(final Object introspectedObject)
87      {
88          Object introspected = introspectedObject;
89          while (introspected instanceof DelegateIntrospection)
90          {
91              introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
92          }
93          Set<String> props = new LinkedHashSet<String>();
94          try
95          {
96              BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
97              PropertyDescriptor[] descrips = info.getPropertyDescriptors();
98              for (int i = 0; i < descrips.length; i++)
99              {
100                 props.add(descrips[i].getName());
101             }
102         }
103         catch (Exception e)
104         {
105             throw new IllegalArgumentException(this + " - getPropertyNames", e);
106         }
107         return props.toArray(new String[props.size()]);
108     }
109 }