FileDocCategorySizeDatePackage
BeanshellGenerator.javaAPI DocJBoss 4.2.14080Fri Jul 13 21:02:28 BST 2007org.jboss.aspects.dbc.condition.parser

BeanshellGenerator

public class BeanshellGenerator extends Object implements ExpressionVisitor
author
Kabir Khan
version
$Revision: 57186 $

Fields Summary
Expression
expr
StringBuffer
code
int
indent
Constructors Summary
public BeanshellGenerator(Expression expr)

      this.expr = expr;
   
Methods Summary
public java.lang.StringcreateBeanshellCode()

      code = new StringBuffer();
      //code.append("boolean " + RET_NAME + " = false;\n");
      expr.accept(this);
      return code.toString();
   
private voidcreateLoopCode(LoopExpression expr, boolean breakType)

      String declaration = expr.getDeclaration();
      String collection = expr.getCollection();
      Expression body = expr.getBody();
      
      indent();
      code.append("for (" + declaration + " : " + collection + "){\n");
      indent++;

      if (body instanceof BooleanExpression)
      {
         indent();
         code.append("if (");
         
         if (!breakType) 
         {
            code.append("!");
         }

         code.append("(");         
       	body.accept(this);
         code.append(")){\n");

         indent++;
         indent();
         code.append("return "+ breakType + ";\n");
         
         indent--;
         indent();         
         code.append("}\n");
      }
      else if (body instanceof ImpliesExpression)
      {
         body.accept(this);
      }
      else
      {
       	body.accept(this);
      }
      indent--;
      indent();
      code.append("}\n");
      if (expr.isTopLevel())
      {
         indent();
         code.append("return "+ !breakType + ";\n");         
      }
   
private voidindent()

      for (int i = 0 ; i < indent * 2 ; i++)
      {
         code.append(" ");
      }
   
public voidvisit(Expression expr)

      expr.accept(this);
   
public voidvisit(BooleanExpression expr)

      if (expr.isTopLevel())
      {
         code.append("return (" + expr.getExpression() + ");\n");
      }
      else
      {
         code.append(expr.getExpression());
      }
   
public voidvisit(ForAllExpression expr)

      createLoopCode(expr, false);
   
public voidvisit(ExistsExpression expr)

      createLoopCode(expr, true);
   
public voidvisit(ImpliesExpression expr)

      indent();
      code.append("if (");
      expr.getLhs().accept(this);
      code.append("){\n");
      indent++;
      indent();
      code.append("if(!(");
      expr.getRhs().accept(this);
      code.append(")){\n");
      indent++;
      indent();
      code.append("return false;\n");
      indent--;
      indent();
      code.append("}\n");
      
      indent--;
      indent();
      code.append("}\n");
      
      if (expr.isTopLevel())
      {
         code.append("return true;\n");         
      }
   
public voidvisit(JavaExpression expr)

      code.append(expr.getJava());
      code.append("\nreturn true;\n");