net.sf.jauvm
Class Continuation

java.lang.Object
  extended by net.sf.jauvm.Continuation
All Implemented Interfaces:
Serializable

public class Continuation
extends Object
implements Serializable

A continuation representing the stored execution state of an Interpreter object.

A Continuation object wraps a given method invocation, which can later (and even repeatedly) be returned from or otherwise terminated by throwing an exception.

A Continuation object can be created and returned to only in interpreted code, i.e. in methods tagged as interpretable and ran by an Interpreter.

See Also:
Interpreter, interpretable

Constructor Summary
Continuation()
          Captures the current execution state of the Interpreter as a new Continuation object.
 
Method Summary
static Thread forkThread()
          Forks a new thread at the current execution point.
static Continuation getCurrent()
          Gets this method invocation's current continuation as a new Continuation object.
 Class<?>[] getExceptionTypes()
          Gets the exception types declared by the method invocation wrapped in this Continuation object.
 Class<?> getReturnType()
          Gets the return type of the method invocation wrapped in this Continuation object.
 void returnTo()
          Returns to the execution point represented by this Continuation object (with no return value).
 void returnTo(Object value)
          Returns value to the execution point represented by this Continuation object.
static boolean saveCurrentTo(File file)
          Serializes this method invocation's current continuation to the file.
 void throwTo(Throwable thrwbl)
          Throws thrwbl to the execution point represented by this Continuation object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Continuation

public Continuation()
Captures the current execution state of the Interpreter as a new Continuation object.

The call to this constructor in the following example captures the execution state of the caller of aMethod(), wrapping the current aMethod() invocation in a Continuation object:

  public static @interpretable void aMethod() {
      Continuation cont = new Continuation();
  }
A continuation can only be captured in interpreted code.

Throws:
UnsupportedOperationException - if the constructor is not invoked from interpreted code
Method Detail

returnTo

public void returnTo()
Returns to the execution point represented by this Continuation object (with no return value).

Returning to a continuation that wraps a given method invocation, is the same as returning, possibly once again, from that method invocation.

Accordingly, this method can only be invoked on continuations of void return type, that is, continuations captured in a method of void return type. Also, a continuation can only be returned to in interpreted code.

Throws:
UnsupportedOperationException - if this method is not invoked from interpreted code
IllegalArgumentException - if this continuation is not of void return type
See Also:
getReturnType()

returnTo

public void returnTo(Object value)
Returns value to the execution point represented by this Continuation object.

Returning to a continuation that wraps a given method invocation, is the same as returning, possibly once again, from that method invocation.

Accordingly, this method can only be invoked on continuations whose return type is assignable from value, that is, value must be assignable to the return type of the method the continuation was captured in. The return value value is assignable to that return type if there is an unwrapping and/or widening conversion that goes from value's type to that return type. Also, a continuation can only be returned to in interpreted code.

Parameters:
value - the value to return to this continuation
Throws:
UnsupportedOperationException - if this method is not invoked from interpreted code
IllegalArgumentException - if value is not assignable to this continuation's return type
See Also:
getReturnType()

throwTo

public void throwTo(Throwable thrwbl)
Throws thrwbl to the execution point represented by this Continuation object.

Throwing an exception to a continuation that wraps a given method invocation, is the same as having that method invocation throw the given exception.

Accordingly, thrwbl must either be an unchecked exception, or a checked exception accepted by this continuation, that is, a checked exception declared by method the continuation was captured in. Also, a continuation can only be thrown to in interpreted code.

Parameters:
thrwbl - the throwable to throw to this continuation
Throws:
UnsupportedOperationException - if this method is not invoked from interpreted code
IllegalArgumentException - if thrwbl is a checked exception not declared by this continuation
See Also:
getExceptionTypes()

getReturnType

public Class<?> getReturnType()
Gets the return type of the method invocation wrapped in this Continuation object.

Returns:
this continuation's return type

getExceptionTypes

public Class<?>[] getExceptionTypes()
Gets the exception types declared by the method invocation wrapped in this Continuation object.

Returns:
an array with this continuation's accepted exception types

getCurrent

public static Continuation getCurrent()
Gets this method invocation's current continuation as a new Continuation object.

Returns:
the current continuation

saveCurrentTo

public static boolean saveCurrentTo(File file)
                             throws IOException
Serializes this method invocation's current continuation to the file.

Parameters:
file - the file to which to save the continuation
Returns:
true if the Continuation object was just saved, false if returning from the file
Throws:
IOException - if an I/O error occurs writing to the file

forkThread

public static Thread forkThread()
Forks a new thread at the current execution point.

Returns:
null in the new thread, the new Thread object in the current thread