We're facing a task of conversion of a java method into a state machine. This is like to convert a SAX Parser, pushing data, into an Xml Reader, which pulls data.
The task is formalized as:
We have defined rules converting all statements into a state machine except of the statement synchronized. In fact the logic is rather linear, however the most untrivial conversion is for try statement. Consider an example:
synchronized
try
public class Test { void method() throws Exception { try { A(); B(); } catch(Exception e) { C(e); } finally { D(); } E(); } private void A() throws Exception { // logic A } private void B() throws Exception { // logic B } private void C(Exception e) throws Exception { // logic C } private void D() throws Exception { // logic D } private void E() throws Exception { // logic E } }
Suppose we want to see method() as a state machine in a way that split markers are after calls to methods A(), B(), C(), D(), E(). This is how it looks as a state machine:
method()
A()
B()
C()
D()
E()
Callable<Boolean> methodAsStateMachine() throws Exception { return new Callable<Boolean>() { public Boolean call() throws Exception { do { try { switch(state) { case 0: { A(); state = 1; return true; } case 1: { B(); state = 3; return true; } case 2: { C(ex); state = 3; return true; } case 3: { D(); if (currentException != null) { throw currentException; } state = 4; return true; } case 4: { E(); state = -1; return false; } } if (currentException == null) { currentException = new IllegalStateException(); } } catch(Throwable e) { currentException = null; switch(state) { case 0: case 1: { if (e instanceof Exception) { ex = (Exception)e; state = 2; } else { currentException = e; state = 3; } continue; } case 2: { currentException = e; state = 3; continue; } } currentException = e; state = -1; } } while(false); return this.<Exception>error(); } @SuppressWarnings("unchecked") private <T extends Throwable> boolean error() throws T { throw (T)currentException; } private int state = 0; private Throwable currentException = null; private Exception ex = null; }; }
Believe it, or not but this transformation can be done purely in xslt 2.0 with the help of the jxom (Java xml object model). We shall update jxom.zip whenever this module will be implemented and tested.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u