1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.interpreter;
11
12 import nl.tudelft.simulation.dsol.interpreter.classfile.LocalVariableDescriptor;
13
14 /***
15 * Each frame (§3.6) contains an array of variables known as its local
16 * variables. The length of the local variable array of a frame is determined at
17 * compile time and supplied in the binary representation of a class or
18 * interface along with the code for the method associated with the frame
19 * (§4.7.3). A single local variable can hold a value of type boolean, byte,
20 * char, short, int, float, reference, or returnAddress. A pair of local
21 * variables can hold a value of type long or double.
22 * <p>
23 * Local variables are addressed by indexing. The index of the first local
24 * variable is zero. An integer is be considered to be an index into the local
25 * variable array if and only if that integer is between zero and one less than
26 * the size of the local variable array.
27 * <p>
28 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
29 * University of Technology </a>, the Netherlands. <br>
30 * See for project information <a
31 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
32 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
33 * License (GPL) </a>, no warranty <br>
34 *
35 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36 * Jacobs </a>
37 * @version 1.2 Apr 1, 2004
38 * @since 1.4
39 */
40 public class LocalVariable
41 {
42 /*** the localVariableDescriptor */
43 private LocalVariableDescriptor localVariableDescriptor = null;
44
45 /*** the runtime value of the localVariable */
46 private Object value = null;
47
48 /***
49 * constructs a new LocalVariable
50 *
51 * @param localVariableDescriptor the descriptor
52 */
53 public LocalVariable(final LocalVariableDescriptor localVariableDescriptor)
54 {
55 this.localVariableDescriptor = localVariableDescriptor;
56 }
57
58 /***
59 * @return Returns the localVariableDescriptor.
60 */
61 public LocalVariableDescriptor getLocalVariableDescriptor()
62 {
63 return this.localVariableDescriptor;
64 }
65
66 /***
67 * @return Returns the value.
68 */
69 public synchronized Object getValue()
70 {
71 return this.value;
72 }
73
74 /***
75 * @param value The value to set.
76 */
77 public synchronized void setValue(final Object value)
78 {
79 this.value = value;
80 }
81
82 /***
83 * @see java.lang.Object#toString()
84 */
85 public String toString()
86 {
87 String result = "variable";
88 if (this.localVariableDescriptor != null)
89 {
90 result = result + " descriptor="
91 + this.localVariableDescriptor.toString();
92 }
93 if (this.value != null)
94 {
95 String valueString = null;
96 if (this.value instanceof StringBuffer)
97 {
98 valueString = StringBuffer.class.getName();
99 } else
100 {
101 valueString = this.value.toString();
102 }
103 result = result + valueString;
104 }
105 return result;
106 }
107
108 /***
109 * creates a new array of local variables
110 *
111 * @param descriptors the descriptors
112 * @return LocalVariable[]
113 */
114 public static LocalVariable[] newInstance(
115 final LocalVariableDescriptor[] descriptors)
116 {
117 LocalVariable[] result = new LocalVariable[descriptors.length];
118 for (int i = 0; i < result.length; i++)
119 {
120 result[i] = new LocalVariable(descriptors[i]);
121 }
122 return result;
123 }
124
125 /***
126 * replaces the value of a localVarialbe
127 *
128 * @param localVariables the set to introspect
129 * @param oldValue the oldValue
130 * @param newValue the new value
131 */
132 public static void replace(final LocalVariable[] localVariables,
133 final Object oldValue, final Object newValue)
134 {
135 synchronized (localVariables)
136 {
137 for (int i = 0; i < localVariables.length; i++)
138 {
139 if (oldValue.equals(localVariables[i].getValue()))
140 {
141 localVariables[i].setValue(newValue);
142 }
143 }
144 }
145 }
146
147 /***
148 * parses the localVariables to string
149 *
150 * @param localVariables the localVariables
151 * @return String the result
152 */
153 public static String toString(final LocalVariable[] localVariables)
154 {
155 String result = "";
156 for (int i = 0; i < localVariables.length; i++)
157 {
158 result = result + i + ": " + localVariables[i].toString() + "\n";
159 }
160 return result;
161 }
162 }