RSS 2.0
Sign In
# Friday, September 5, 2008

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:

  • for a given method containing split markers create a class perimitting iteration;
  • each iteration performs part of a logic of a method.

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:

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:

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.

Friday, September 5, 2008 3:39:50 PM UTC  #    Comments [0] -
xslt
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview
Archive
<December 2019>
SunMonTueWedThuFriSat
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
Statistics
Total Posts: 366
This Year: 2
This Month: 0
This Week: 0
Comments: 252
Locations of visitors to this page
Disclaimer
The opinions expressed herein are our own personal opinions and do not represent our employer's view in anyway.

© 2019, Nesterovsky bros
All Content © 2019, Nesterovsky bros
DasBlog theme 'Business' created by Christoph De Baene (delarou)