View Javadoc
1   package nl.tudelft.simulation.introspection.beans;
2   
3   import java.beans.PropertyDescriptor;
4   import java.beans.PropertyEditor;
5   import java.beans.PropertyEditorManager;
6   import java.lang.reflect.Method;
7   
8   import nl.tudelft.simulation.introspection.AbstractProperty;
9   import nl.tudelft.simulation.introspection.Property;
10  
11  /**
12   * The JavaBean TM implementation of the Property interface. See {see BeanIntrospector}for details.
13   * <p>
14   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
16   * project is distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
18   * </p>
19   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
20   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
21   * @author Niels Lang.
22   * @since 1.5
23   */
24  
25  public class BeanProperty extends AbstractProperty implements Property
26  {
27      /** the bean that owns the property. */
28      private Object bean = null;
29  
30      /** the propertyDescriptor. */
31      private PropertyDescriptor descriptor = null;
32  
33      /**
34       * constructs a new BeanProperty.
35       * @param bean the bean to introspect
36       * @param descriptor the descriptor of the property
37       */
38      protected BeanProperty(final Object bean, final PropertyDescriptor descriptor)
39      {
40          this.bean = bean;
41          this.descriptor = descriptor;
42      }
43  
44      @Override
45      public String getName()
46      {
47          return this.descriptor.getName();
48      }
49  
50      @Override
51      public Class<?> getType()
52      {
53          return this.descriptor.getPropertyType();
54      }
55  
56      @Override
57      protected void setRegularValue(final Object values)
58      {
59          Class<?> type = this.descriptor.getPropertyType();
60          PropertyEditor editor = PropertyEditorManager.findEditor(type);
61          Object newValue = values;
62          if (editor != null)
63          {
64              if (values instanceof String)
65              {
66                  editor.setAsText((String) values);
67              }
68              else
69              {
70                  editor.setValue(values);
71              }
72              newValue = editor.getValue();
73          }
74          Method writeMethod = this.descriptor.getWriteMethod();
75          try
76          {
77              writeMethod.invoke(this.bean, new Object[] {newValue});
78          }
79          catch (Throwable throwable)
80          {
81              throw new IllegalArgumentException(this + " - setRegularValue", throwable);
82          }
83      }
84  
85      @Override
86      public Object getValue()
87      {
88          Object result = null;
89          Method readMethod = this.descriptor.getReadMethod();
90          try
91          {
92              if (readMethod != null)
93              { result = readMethod.invoke(this.bean, new Object[0]); }
94          }
95          catch (Exception exception)
96          {
97              throw new IllegalArgumentException(this + "getValue of " + getName(), exception);
98          }
99          return result;
100     }
101 
102     @Override
103     public Object getInstance()
104     {
105         return this.bean;
106     }
107 
108     @Override
109     public boolean isEditable()
110     {
111         return !(this.descriptor.getWriteMethod() == null);
112     }
113 }