package nl.tudelft.simulation.dsol.interpreter; import nl.tudelft.simulation.dsol.interpreter.classfile.LocalVariableDescriptor; /** * Each frame (par 2.6) contains * an array of variables known as its local variables. The length of the local variable array of a frame is determined * at compile time and supplied in the binary representation of a class or interface along with the code for the method * associated with the frame (par * 4.7.3). A single local variable can hold a value of type boolean, byte, char, short, int, float, reference, or * returnAddress. A pair of local variables can hold a value of type long or double. *
* Local variables are addressed by indexing. The index of the first local variable is zero. An integer is be considered * to be an index into the local variable array if and only if that integer is between zero and one less than the size * of the local variable array. *
** Copyright (c) 2002-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights * reserved. See for project information * https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can * be found at * https://simulation.tudelft.nl/dsol/3.0/license.html. *
* @author Peter Jacobs * @author Alexander Verbraeck * @since 1.5 */ public class LocalVariable implements Cloneable { /** the localVariableDescriptor. */ private final LocalVariableDescriptor localVariableDescriptor; /** the runtime value of the localVariable. */ private Object value = null; /** * constructs a new LocalVariable. * @param localVariableDescriptor LocalVariableDescriptor; the descriptor */ public LocalVariable(final LocalVariableDescriptor localVariableDescriptor) { this.localVariableDescriptor = localVariableDescriptor; } /** * @return Returns the localVariableDescriptor. */ public final LocalVariableDescriptor getLocalVariableDescriptor() { return this.localVariableDescriptor; } /** * @return Returns the value. */ public final synchronized Object getValue() { return this.value; } /** * @param value Object; The value to set. */ public final synchronized void setValue(final Object value) { this.value = value; } /** {@inheritDoc} */ @Override public String toString() { String result = "variable"; if (this.localVariableDescriptor != null) { result = result + " descriptor=" + this.localVariableDescriptor.toString(); } if (this.value != null) { String valueString = null; if (this.value instanceof StringBuffer) { valueString = StringBuffer.class.getName(); } else { valueString = this.value.toString(); } result = result + valueString; } return result; } /** * creates a new array of local variables. * @param descriptors LocalVariableDescriptor[]; the descriptors * @return LocalVariable[] */ public static LocalVariable[] newInstance(final LocalVariableDescriptor[] descriptors) { LocalVariable[] result = new LocalVariable[descriptors.length]; for (int i = 0; i < result.length; i++) { result[i] = new LocalVariable(descriptors[i]); } return result; } /** * replaces the value of a local variable. * @param localVariables LocalVariable[]; the set to introspect * @param oldValue Object; the oldValue * @param newValue Object; the new value */ public static void replace(final LocalVariable[] localVariables, final Object oldValue, final Object newValue) { synchronized (localVariables) { for (int i = 0; i < localVariables.length; i++) { if (oldValue.equals(localVariables[i].getValue())) { localVariables[i].setValue(newValue); } } } } /** * parses the localVariables to string. * @param localVariables LocalVariable[]; the localVariables * @return String the result */ public static String toString(final LocalVariable[] localVariables) { String result = ""; for (int i = 0; i < localVariables.length; i++) { result = result + i + ": " + localVariables[i].toString() + "\n"; } return result; } /** {@inheritDoc} */ @Override public Object clone() { LocalVariable result = new LocalVariable(this.localVariableDescriptor); result.value = this.value; return result; } }