Class dec.trek.Statement

The Statement object allows you to manipulate the statements of a Method.

The Statement class does not have a public constructor. To reach a method’s statements, you must use firstStatement() or lastStatement() in the Method class, or override atStartOf(Statement) or atEndOf(Statement) in the Trek class.

Method Summary

  • canCall – returns the first instruction of this statement that can call the specified method
  • delete – deletes this statement from the method
  • firstInstruction – returns the Instruction object of this statement’s first instruction
  • firstOfThen – returns the first statement in the if statement's Then block
  • firstOfElse – returns the first statement in the if statement's Else block
  • getLevel – returns the level of this statement
  • getMethod – returns the Method object of this statement’s method
  • getNumber – returns the index of this statement within its method
  • getPc – returns the location of this statement within its method
  • getSourceNumber – returns the source line number of this statement within its method
  • getType – returns this statement’s type
  • hasChange – returns true if code has been added to or deleted from this statement
  • lastInstruction – returns the Instruction object of this statement’s last instruction
  • modifies – returns the first instruction of this statement that stores a value in the specified variable
  • next – returns the Statement object of the next statement in its method
  • prev – returns the Statement object of the previous statement in its method
  • references – returns the first instruction of this statement that uses the specified member
  • toIndent – returns a string containing the proper number of spaces for indenting statements with this statement's level
  • toLocation – returns a string representing this statement’s location in its program
  • toObjectId – returns the object Id of this statement
  • toString – returns a string containing the source code of this statement
  • undoChange – removes code that has been added to or deleted from this statement

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 Instruction canCall(Method meth)

Returns the first instruction in this statement that can call the specified method. Returns null if this statement does not call the specified method. Throws an exception if it cannot determine a return value because the referenced member's classfile could not be accessed via getClassFile.

Because of method overriding, the method that will be called at runtime can be defined in a subclass of the class referred to in the source code. For example, a statement containing member.toString() will actually call Method.toString or Field.toString.

Parameters:

meth – the Method object to check

public void delete(String changid)

Deletes this statement from the in-memory representation of its classfile.

Parameters:

changid – the change ID to assign to this code change, or null to assign "".

public Instruction firstInstruction()

Returns the Instruction object of the first instruction of this statement. If this statement has no code, null is returned instead.

public Instruction firstOfThen()

Returns the first statement of the if statement's Then block. If the if statement's if instruction itself does a break or continue, null is returned instead.

public Instruction firstOfElse()

Returns the first statement of the if statement's Else block. If the source code contains no else or the compiler optimized the else away, null is returned instead.

public Method getLevel()

Returns this statement’s block level. Top-level statements have a level of 1. The statement types that start blocks are then, else, loops, switch cases, try, catch, finally, synchronize, and labels.

public Method getMethod()

Returns the Method object of this statement’s method.

public int getNumber()

Returns the index of this statement within its method. The first statement of a method returns 1.

public int getPc()

Returns the byte offset of this statement within its method. The offset of a method’s first statement is 0. Note that it is possible for consecutive statements to start at the same PC. For example, most Trek.ST_ENDtype statements are 0-length.

public int getSourceNumber()

Returns -1 if there is no LineNumberTable for this statement's method. (A LineNumberTable normally exists only when a file is compiled for debugging).

Returns the line number of this statement within its source file if it has an entry in the LineNumberTable.

Returns 0 for statements that do not have an entry in the LineNumberTable (eg declarative statements like try).

public int getType()

Returns this statement’s type. For example, if this statement is an assignment statement, Trek.ST_ASSIGN is returned.

public boolean hasChange(String changid)

Returns true if a change with a matching change ID has been done to this statement. The ways to change a statement are Code.add*, Call.add*, instruction.delete, and statement.delete.

Parameters:

changid – identifies the change IDs to look for. If it is null, any change ID matches. If it ends in *, a change ID that starts with changid matches. Otherwise only change IDs that equal changid match.

public Instruction lastInstruction()

Returns the Instruction object of the last instruction of this statement. If this statement has no code, null is returned instead.

public Instruction modifies(Local loc)

Returns the first instruction in this statement that stores a value in the local variable. However if this statement does not use loc, null is returned.

Parameters:

loc – the Local object of the local variable to check.

public Instruction modifies(Field fld)

Returns the first instruction in this statement that stores a value in the field. However if this statement does not use fld, null is returned.

Parameters:

fld – the Field object of the field to check

public Statement next()

Returns the Statement object of the statement with the next higher statement number. If there are no more statements, null is returned instead.

public Statement prev()

Returns the Statement object of the statement with the next lower statement number. If there are no more statements, null is returned instead.

public Instruction references(Local loc)

Returns the first instruction in this statement that fetches or stores a value in the indicated local variable.

Parameters:

locidx – the Local object of the local variable to check.

public Instruction references(Member mem)

Returns the first instruction in this statement that uses the specified field or method. If mem is a method, this means this statement calls the specified method. If mem is a field, this means this statement fetches or stores a value in the field. However if this statement does not use mem, null is returned.

Parameters:

mem – the Field object or Method object to check

public String toIndent(int prepad, int levpad, int maxpad)

Returns a string containing prepad + (levpad*(getLevel()-1)) spaces – but no more than min(maxpad, 100) spaces. If the expression is less than 0, a 0-length string is returned.

Parameters:

prepad – the number of spaces by which top level statements are indented

levpad – the number of additional spaces to indent per level

maxpad – the maximum allowed length of indent string

public String toLocation()

Returns a string containing the class-name.method-name(statement-number) of this Statement.

public String toObjectId()

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

An object Id for a statement becomes obsolete if its method's object Id becomes obsolete or its statement number becomes invalid. Thus a statement's object Id identifies its position in its method, not a particular piece of source code.

public String toString()

Returns a string containing the source code of this statement.

The notion of what constitutes a statement depends on the code that the Java compiler generated. When a class is compiled optimized, the classfile may not exactly reflect the original source code. For example, ii=1; if (ii==1) j=2; might become just j=2;. In other words, the compiler could realize ii is not used later in the program and that the if is always true.

When a class is compiled optimized, the names of its local variables are usually not stored in the classfile. When a classfile does not contain local variable tables for its methods, toString constructs names for local variables based on their data types and other context.

public boolean undoChange(String changid)

Removes each change to this statement that has the specified change ID. If there are any such changes, it returns true; otherwise it returns false. The ways to change a statement are Code.add*, Call.add*, instruction.delete, and statement.delete.

If you wanted to replace a statement with different code, you would need to both add the desired code and delete the statement. If you wanted to be able to remove this entire change in one operation, you would specify the same change ID (eg "replace7") on both the Code.add* call and the statement.delete call.

Parameters:

changid – identifies the change IDs to look for. If it is null, any change ID matches. If it ends in *, a change ID that starts with changid matches. Otherwise only change IDs that equal changid match.