Class dec.trek.Local

The Local object allows you to manipulate the local variables of a Method.

To scan a method’s normal local variables, you must use firstLocal or lastLocal in the Method class or override at(Local) in the Trek class. This set of local variables is called the method's Local Variables Table (LVT). Compilers supply LVTs to facilitate debugging. If a compiler does not supply LVTs, as in an optimized classfile, JTrek builds LVTs by analyzing each method's code.

When a compiler supplies a method's LVT, it omits local variables that it created during code generation (as opposed to those declared by the developer). When a compiler does not supply an LVT, this distinction is lost. So in a JTrek-built LVT, all of the method's local variables are in the LVT.

The methods getLocal and getReferenceArg in the Instruction class can be used to get an instruction's local variable. If the instruction uses a normal local, the returned Local is an object in the LVT. Otherwise the returned Local is an object that was created by the called method itself.

Method Summary

  • getDefinition – returns the Statement object in which this local variable is defined
  • getMethod – returns the Method object of the method in which this local variable is defined
  • isArg – returns true if this local variable is an argument of its method
  • isArray – returns true if this local variable is an array
  • isThis – returns true if this local variable is the this local variable
  • Local – constructs a Local object
  • next – returns the Local object of the next local variable of its method
  • prev – returns the Local object of the previous local variable of its method
  • toName – returns the name of this local variable
  • toObjectId – returns the object Id of this local variable
  • toString – returns the source code for the declaration of this local variable
  • toType – returns the data type of this local variable

Fields

public Object userDefined

Field that can be used by an application developer to store arbitrary data about this object during the trek.

Methods

public Statement getDefinition()

Returns the Statement object in which this local variable is defined. If this local is a method argument or catch argument, null is returned instead.

public ClassFile getMethod()

Returns the Method object of the method in which this local variable is defined.

public boolean isArg()

Returns true if this local variable is an argument of its method. Note that this is not considered an argument.

public boolean isArray()

Returns true if this field is an array.

public boolean isThis()

Returns true if this local variable is the this variable of its method.

public Local(String name, Object type, Method meth)

Creates a new local variable in the specified method. This local is not placed in the method's LVT. Normally this constructor is used when you need inserted code to store something, but do not want to clobber any of the real data used in the method.

Parameters:

name – the name you want to give the local variable

type – the local's data type is derived from this. If it is a Field, FieldRef, Local, TypeRef, or ValueRef,  the new local is given this thing's data type. If it is a Method or MethodRef, the new local is given this method's return type. If it is a String, the new local's data type is set from the String's value, which must encode a data type as described in The Java Virtual Machine Specification.

meth – the method in which to create the local variable

Example:

Local loc = new Local("save", "I", meth);
Code code = Call.addAt("save-code", inst);
code.append(Code.istore, loc);
code.done();

would insert code to save the value on the top of the stack in a created int local named save.

public Local next()

Returns the Local object for the next local variable in this local’s method, or null if there are no more.

public Field prev()

Returns the Local object for the previous local variable in this local’s method, or null if there are no more.

public String toName()

Returns the name of this local variable.

public String toObjectId()

Returns a persistent value that identifies this local variable. A reference to this local can be acquired by calling getObject on a trek that this local is within.

An object Id for a local variable becomes obsolete if its method's object Id becomes obsolete or if its definition point is no longer within the range of definition of any local variable with its name. (Recall that the same name can mean different local variables in different parts of a method in Java).

public String toString()

Returns the source statement that defined this local variable.

public String toType()

Returns the data type of this local variable, for example int or java.lang.Object.