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