View Javadoc

1   /*
2    * @(#) FieldProperty.java Apr 15, 2004 Copyright (c) 2002-2005 Delft University
3    * of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights
4    * reserved. This software is proprietary information of Delft University of
5    * Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.introspection.fields;
8   
9   import java.lang.reflect.Field;
10  
11  import nl.tudelft.simulation.introspection.AbstractProperty;
12  import nl.tudelft.simulation.introspection.Property;
13  import nl.tudelft.simulation.logger.Logger;
14  
15  /***
16   * The field implementation of the Property interface. See for details.
17   * <p>
18   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
19   * University of Technology </a>, the Netherlands. <br>
20   * See for project information <a
21   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
22   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
23   * General Public License (LGPL) </a>, no warranty.
24   * 
25   * @author <a
26   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
27   *         Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
28   *         Jacobs </a>
29   * @version 1.1 Apr 15, 2004
30   * @since 1.5
31   */
32  public class FieldProperty extends AbstractProperty implements Property
33  {
34      /*** the owner of the fieldProperty */
35      private Object owner = null;
36  
37      /*** the descriptor of the field */
38      private Field descriptor = null;
39  
40      /*** is the property editable */
41      private boolean editable = false;
42  
43      /***
44       * constructs a new FieldProperty
45       * 
46       * @param owner its owner
47       * @param descriptor the descriptor
48       * @param editable is the property editable
49       */
50      public FieldProperty(final Object owner, final Field descriptor,
51              final boolean editable)
52      {
53          // Check whether descriptor is valid for owner should be conducted here
54          this.owner = owner;
55          this.descriptor = descriptor;
56          this.descriptor.setAccessible(true);
57          this.editable = editable;
58      }
59  
60      /***
61       * constructs a new FieldProperty
62       * 
63       * @param owner its owner
64       * @param descriptor the descriptor
65       */
66      public FieldProperty(final Object owner, final Field descriptor)
67      {
68          this(owner, descriptor, true);
69      }
70  
71      /***
72       * @see nl.tudelft.simulation.introspection.Property#getName()
73       */
74      public String getName()
75      {
76          return this.descriptor.getName();
77      }
78  
79      /***
80       * @see nl.tudelft.simulation.introspection.Property#getType()
81       */
82      public Class< ? > getType()
83      {
84          return this.descriptor.getType();
85      }
86  
87      /***
88       * @see nl.tudelft.simulation.introspection.AbstractProperty
89       *      #setRegularValue(java.lang.Object)
90       */
91      @Override
92      public void setRegularValue(final Object value)
93      {
94          Class type = this.descriptor.getType();
95          if (!type.isInstance(value) || !this.editable)
96          {
97              throw new IllegalArgumentException("Cannot assign " + value
98                      + " to " + this.owner + ", " + this.descriptor);
99          }
100         synchronized (this.owner)
101         {
102             try
103             {
104                 this.descriptor.set(this.owner, value);
105             } catch (Exception exception)
106             {
107                 Logger.warning(this, "setRegularValue", exception);
108             }
109         }
110     }
111 
112     /***
113      * @see nl.tudelft.simulation.introspection.Property#getValue()
114      */
115     public Object getValue()
116     {
117         try
118         {
119             return this.descriptor.get(this.owner);
120         } catch (Exception exception)
121         {
122             Logger.warning(this, "getValue", exception);
123         }
124         return null;
125     }
126 
127     /***
128      * @see nl.tudelft.simulation.introspection.Property#getInstance()
129      */
130     public Object getInstance()
131     {
132         return this.owner;
133     }
134 
135     /***
136      * @see nl.tudelft.simulation.introspection.Property#isEditable()
137      */
138     public boolean isEditable()
139     {
140         return this.editable;
141     }
142 }