Top Description Methods
org.apache.derby.iapi.services.compiler

public Interface MethodBuilder

Known Direct Implementers
org.apache.derby.impl.services.bytecode.BCMethod

MethodBuilder is used to generate the code for a method.

The code for a method is built in a way that corresponds to the layout of the stack machine that is the Java Virtual Machine. Values are pushed on the stack, moved about on the stack and then popped off the stack by operations such as method calls. An understanding of hoe the JVM operates is useful before using this class.

All the method descriptions below are generating bytecode to achieved the desired behaviour when the generated class is loaded. None of this class's methods calls actually invoke methods or create objects described by the callers.

Method Summary

Modifier and TypeMethod and Description
public void
addThrownException(String exceptionClass)

Declare the method throws an exception.

public int
callMethod(short
type of method invocation
type
,
String
Class or interface the method is declared in. If it is a non-static method call then if declaringClass is null, the declared type is taken to be the type of the reference that will be popped.
declaringClass
,
String
name of the method
methodName
,
String
class name or primitive type (including "void") of the return type of the method, can not be null.
returnType
,
int
number of arguments to the method (can be 0).
numArgs
)

Call a method.

public int
callMethod(Object methodDescriptor)

Call a method previously described by describeMethod().

public void
callSuper()

Call super().

public void
cast(String
type (primitive, interface or class) to cast to.
className
)

Cast the top stack value.

public void
complete()

Indicate the method is complete.

public void
completeConditional()

Complete a conditional which completes the false code path.

public void
conditionalIf()

Initiate a conditional sequence.

public void
conditionalIfNull()

Initiate a conditional sequence.

public Object
describeMethod(short opcode, String declaringClass, String methodName, String returnType)

Return an object that efficiently (to the implementation) describes a zero-argument method and can be used with the single argument callMethod().

public void
dup()

Duplicate the top value on the stack.

public void
endStatement()

End a statement.

public void
getArrayElement(int
Offset into the array (zero based)
element
)

Pop an array refrence off the stack and push an element from that array.

public void
getField(LocalField field)

Push the contents of the local field onto the stack.

public void
getField(String declaringClass, String fieldName, String fieldType)

Push the contents of the described field onto the stack.

public String
getName()

return the name of the method.

public void
getParameter(int
position of the parameter (zero based).
id
)

Push a parameter value.

public void
getStaticField(String declaringClass, String fieldName, String fieldType)

Push the contents of the described static field onto the stack.

public void
isInstanceOf(String className)

Pop the top stack value and push a boolean that is the result of an instanceof check on the popped reference.

public void
methodReturn()

Return from a method, optionally with a value.

public void
pop()

Pop the top value off the stack

Stack ..., value =>
      ...
public void
push(byte value)

Push a byte constant onto the stack

Stack ...
public void
push(boolean value)

Push a boolean constant onto the stack

Stack ...
public void
push(short value)

Push a short constant onto the stack

Stack ...
public void
push(int value)

Push a int constant onto the stack

Stack ...
public void
push(long value)

Push a long constant onto the stack

Stack ...
public void
push(float value)

Push a float constant onto the stack

Stack ...
public void
push(double value)

Push a double constant onto the stack

Stack ...
public void
push(String value)

Push a String constant onto the stack

Stack ...
public void
pushNewArray(String
- type of array.
className
,
int
- number of elements in the array
size
)

Create an instance of an array and push it onto the stack.

public void
pushNewComplete(int
number of arguments to the constructor (can be 0).
numArgs
)

Complete the sequence that was started with pushNewStart().

public void
pushNewStart(String
class name of object to be created.
className
)

Initiate a sequence that calls a constructor, equivalent to the new operator in Java.

public void
pushNull(String className)

Push a typed null onto the stack

Stack ...
public void
pushThis()

Push this onto the stack.

public void
putField(LocalField field)

Pop the top stack value and store it in the local field.

public void
putField(String fieldName, String fieldType)

Pop the top stack value and store it in the instance field of this class.

public void
putField(String declaringClass, String fieldName, String fieldType)

Pop the top stack value and store it in the field.

public void
setArrayElement(int
Offset into the array (zero based)
element
)

Pop an array reference off the stack, store a value in the array at the passed in offset.

public void
setField(LocalField field)

Pop the top stack value and store it in the local field.

public void
startElseCode()

Complete the true code path of a conditional.

public boolean
statementNumHitLimit(int noStatementsAdded)

Tell if statement number in this method builder hits limit.

public void
swap()

Swap the top two values on the stack.

public void
upCast(String className)

Upcast the top stack value.

Method Detail

addThrownExceptionback to summary
public void addThrownException(String exceptionClass)

Declare the method throws an exception. Must be called before any code is added to the method.

callMethodback to summary
public int callMethod(short type, String declaringClass, String methodName, String returnType, int numArgs)

Call a method. The instance (receiver or reference) for non-static methods must be pushed by the caller. The instance (for non-static) and the arguments are popped of the stack, and the return value (if any) is pushed onto the stack.
The type needs to be one of:

  • VMOpcode.INVOKESTATIC - call a static method
  • VMOpcode.INVOKEVIRTUAL - call method declared in the class or super-class.
  • VMOpcode.INVOKEINTERFACE - call a method declared in an interface
static methods

Stack ...,value* => [numArgs number of values will be popped]
      ...,return_value [void methods will not push a value]

non-static methods

Stack ...,ref,value* => [numArgs number of values will be popped]
      ...,return_value [void methods will not push a value]

The type of the arguments to the methods must exactly match the declared types of the parameters to the methods. If a argument is of the incorrect type the caller must up cast it or down cast it.
Parameters
type:short

type of method invocation

declaringClass:String

Class or interface the method is declared in. If it is a non-static method call then if declaringClass is null, the declared type is taken to be the type of the reference that will be popped.

methodName:String

name of the method

returnType:String

class name or primitive type (including "void") of the return type of the method, can not be null.

numArgs:int

number of arguments to the method (can be 0).

callMethodback to summary
public int callMethod(Object methodDescriptor)

Call a method previously described by describeMethod().

static methods

Stack ...,value* => [numArgs number of values will be popped]
      ...,return_value [void methods will not push a value]

non-static methods

Stack ...,ref,value* => [numArgs number of values will be popped]
      ...,return_value [void methods will not push a value]
callSuperback to summary
public void callSuper()

Call super(). Caller must only add this to a constructor.


Stack ... =>
      ... 
castback to summary
public void cast(String className)

Cast the top stack value. Correctly down-casts a reference or casts a primitive type (e.g. int to short).

Stack ...,value =>
      ...,cast_value
Parameters
className:String

type (primitive, interface or class) to cast to.

completeback to summary
public void complete()

Indicate the method is complete. Once this call has been made the caller must discard the reference to this object.

completeConditionalback to summary
public void completeConditional()

Complete a conditional which completes the false code path.

conditionalIfback to summary
public void conditionalIf()

Initiate a conditional sequence. The top value on the stack must be a boolean and will be popped. If it is true then the code following this call until the startElseCode() will be executed at runtime, otherwise the code following startElseCode() until the completeConditional() is called. See conditionalIfNull() for example and restrictions.

Stack ...,boolean_value =>
      ...
.
conditionalIfNullback to summary
public void conditionalIfNull()

Initiate a conditional sequence. The top value on the stack (a reference) is popped and compared to 'null'. If the value is null then the code following this call until the startElseCode() will be executed at runtime, otherwise the code following startElseCode() until the completeConditional() is called.
E.g.

mb.callMethod(...); // pushes an object onto the stack
mb.conditionalIfNull();
  mb.push(3);
mb.startElseCode();
  mb.push(5);
mb.completeConditional();
// at this point 3 or 5 will be on the stack
Each path through the ?: statement must leave the stack at the same depth as the other.
If the if or else code pops values from the stack that were before the conditional value, then they must use the same number of values from the stack.
Stack ...,ref =>
      ...
.
describeMethodback to summary
public Object describeMethod(short opcode, String declaringClass, String methodName, String returnType)

Return an object that efficiently (to the implementation) describes a zero-argument method and can be used with the single argument callMethod(). Descriptions for the parameters to this method are the same as the five argument callMethod(). This allows the caller to cache frequently used methods. The returned object is only valid for use by this MethodBuilder.
This call does not affect the Stack.

dupback to summary
public void dup()

Duplicate the top value on the stack.

Stack ...,value =>
      ...,value,value
endStatementback to summary
public void endStatement()

End a statement. Pops the top-word of the stack, if any. Must only be called if zero or one item exists on the stack.

Stack value =>
      :empty:
or

Stack :empty: =>
      :empty:

.
getArrayElementback to summary
public void getArrayElement(int element)

Pop an array refrence off the stack and push an element from that array.

Stack ...,array_ref =>
      ...,value
Parameters
element:int

Offset into the array (zero based)

getFieldback to summary
public void getField(LocalField field)

Push the contents of the local field onto the stack. This call pushes the this instance required to access the field itself.

Stack ...  =>
      ...,field_value
getFieldback to summary
public void getField(String declaringClass, String fieldName, String fieldType)

Push the contents of the described field onto the stack. This call requires the instance (reference) to be pushed by the caller.

Stack ...,field_ref  =>
      ...,field_value
getNameback to summary
public String getName()

return the name of the method.

getParameterback to summary
public void getParameter(int id)

Push a parameter value.

Stack ...  =>
      ...,param_value
Parameters
id:int

position of the parameter (zero based).

getStaticFieldback to summary
public void getStaticField(String declaringClass, String fieldName, String fieldType)

Push the contents of the described static field onto the stack.

Stack ...  =>
      ...,field_value
isInstanceOfback to summary
public void isInstanceOf(String className)

Pop the top stack value and push a boolean that is the result of an instanceof check on the popped reference.

Stack ...,ref =>
      ...,boolean_value
.
methodReturnback to summary
public void methodReturn()

Return from a method, optionally with a value. Must only be called if zero or one item exists on the stack. If the stack contains a single value then that is popped and used as the returned value.

Stack value =>
      :empty:
or

Stack :empty: =>
      :empty:

.
popback to summary
public void pop()

Pop the top value off the stack

Stack ..., value =>
      ...
.
pushback to summary
public void push(byte value)

Push a byte constant onto the stack

Stack ...  =>
      ...,byte_value
pushback to summary
public void push(boolean value)

Push a boolean constant onto the stack

Stack ...  =>
      ...,boolean_value
pushback to summary
public void push(short value)

Push a short constant onto the stack

Stack ...  =>
      ...,short_value
pushback to summary
public void push(int value)

Push a int constant onto the stack

Stack ...  =>
      ...,int_value
pushback to summary
public void push(long value)

Push a long constant onto the stack

Stack ...  =>
      ...,long_value
pushback to summary
public void push(float value)

Push a float constant onto the stack

Stack ...  =>
      ...,float_value
pushback to summary
public void push(double value)

Push a double constant onto the stack

Stack ...  =>
      ...,double_value
pushback to summary
public void push(String value)

Push a String constant onto the stack

Stack ...  =>
      ...,String_value
pushNewArrayback to summary
public void pushNewArray(String className, int size)

Create an instance of an array and push it onto the stack.

Stack ...  =>
      ...,array_ref
Parameters
className:String

- type of array.

size:int

- number of elements in the array

pushNewCompleteback to summary
public void pushNewComplete(int numArgs)

Complete the sequence that was started with pushNewStart(). Pop the arguments to the constructor and push the reference to the newly created object.

Stack ...,value* => [numArgs number of values will be popped]
      ...,new_ref
Parameters
numArgs:int

number of arguments to the constructor (can be 0).

pushNewStartback to summary
public void pushNewStart(String className)

Initiate a sequence that calls a constructor, equivalent to the new operator in Java. After this call, the caller must push any arguments and then complete the construction with a call to pushNewComplete(). Only arguments to the constructor can be pushed onto the stack between the pushNewStart() and pushNewComplete() method calls.

Stack ... => [unchanged]
      ...
Parameters
className:String

class name of object to be created.

pushNullback to summary
public void pushNull(String className)

Push a typed null onto the stack

Stack ...  =>
      ...,null
pushThisback to summary
public void pushThis()

Push this onto the stack.

Stack ...  =>
      ...,this_ref
putFieldback to summary
public void putField(LocalField field)

Pop the top stack value and store it in the local field. This call pushes the this instance required to access the field itself. Like the Java language 'field = value', this leaves the value on the stack.

Stack ...,value  =>
      ...,value
putFieldback to summary
public void putField(String fieldName, String fieldType)

Pop the top stack value and store it in the instance field of this class. This call pushes the this instance required to access the field itself. Like the Java language 'field = value', this leaves the value on the stack.

Stack ...,value  =>
      ...,value
putFieldback to summary
public void putField(String declaringClass, String fieldName, String fieldType)

Pop the top stack value and store it in the field. This call requires the instance to be pushed by the caller. Like the Java language 'field = value', this leaves the value on the stack.

Stack ...,field_ref,value  =>
      ...,value
setArrayElementback to summary
public void setArrayElement(int element)

Pop an array reference off the stack, store a value in the array at the passed in offset.

Stack ...,array_ref, value =>
      ...
Parameters
element:int

Offset into the array (zero based)

setFieldback to summary
public void setField(LocalField field)

Pop the top stack value and store it in the local field. This call pushes the this instance required to access the field itself. This call does not leave any value on the stack.

Stack ...,value  =>
      ...
startElseCodeback to summary
public void startElseCode()

Complete the true code path of a conditional.

statementNumHitLimitback to summary
public boolean statementNumHitLimit(int noStatementsAdded)

Tell if statement number in this method builder hits limit. This method builder keeps a counter of how many statements are added to it. Caller should call this function every time it tries to add a statement to this method builder (counter is increased by 1), then the function returns whether the accumulated statement number hits a limit. The reason of doing this is that Java compiler has a limit of 64K code size for each method. We might hit this limit if an extremely long insert statement is issued, for example (see beetle 4293). Counting statement number is an approximation without too much overhead.

swapback to summary
public void swap()

Swap the top two values on the stack.

Stack ...,valueA,valueB =>
      ...,valueB,valueA
upCastback to summary
public void upCast(String className)

Upcast the top stack value. This is used for correct method resolution by upcasting method parameters. It does not put any casting code into the byte code stream. Can only be used for refrences.

Stack ...,ref =>
      ...,ref