1 package nl.tudelft.simulation.introspection.fields;
2
3 import java.lang.reflect.Field;
4
5 import nl.tudelft.simulation.introspection.AbstractProperty;
6 import nl.tudelft.simulation.introspection.Property;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public class FieldProperty extends AbstractProperty implements Property
23 {
24
25 private Object owner = null;
26
27
28 private Field descriptor = null;
29
30
31 private boolean editable = false;
32
33
34
35
36
37
38
39 public FieldProperty(final Object owner, final Field descriptor, final boolean editable)
40 {
41
42 this.owner = owner;
43 this.descriptor = descriptor;
44 this.descriptor.setAccessible(true);
45 this.editable = editable;
46 }
47
48
49
50
51
52
53 public FieldProperty(final Object owner, final Field descriptor)
54 {
55 this(owner, descriptor, true);
56 }
57
58
59 @Override
60 public String getName()
61 {
62 return this.descriptor.getName();
63 }
64
65
66 @Override
67 public Class<?> getType()
68 {
69 return this.descriptor.getType();
70 }
71
72
73 @Override
74 public void setRegularValue(final Object value)
75 {
76 Class<?> type = this.descriptor.getType();
77 if (!type.isInstance(value) || !this.editable)
78 {
79 throw new IllegalArgumentException("Cannot assign " + value + " to " + this.owner + ", " + this.descriptor);
80 }
81 synchronized (this.owner)
82 {
83 try
84 {
85 this.descriptor.set(this.owner, value);
86 }
87 catch (Exception exception)
88 {
89 throw new IllegalArgumentException(this + " - setRegularValue", exception);
90 }
91 }
92 }
93
94
95 @Override
96 public Object getValue()
97 {
98 try
99 {
100 return this.descriptor.get(this.owner);
101 }
102 catch (Exception exception)
103 {
104 throw new IllegalArgumentException(this + " - getValue", exception);
105 }
106 }
107
108
109 @Override
110 public Object getInstance()
111 {
112 return this.owner;
113 }
114
115
116 @Override
117 public boolean isEditable()
118 {
119 return this.editable;
120 }
121 }