Example usage for org.eclipse.jdt.core.dom WhileStatement setExpression

List of usage examples for org.eclipse.jdt.core.dom WhileStatement setExpression

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom WhileStatement setExpression.

Prototype

public void setExpression(Expression expression) 

Source Link

Document

Sets the expression of this while statement.

Usage

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.WhileStatement node) {
    WhileStatement element = (WhileStatement) this.binding.get(node);
    this.initializeNode(element, node);

    if (this.binding.get(node.getExpression()) != null)
        element.setExpression((Expression) this.binding.get(node.getExpression()));
    if (this.binding.get(node.getBody()) != null)
        element.setBody((Statement) this.binding.get(node.getBody()));
}

From source file:org.asup.dk.compiler.rpj.writer.JDTStatementWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  ww.j  a  v  a 2 s.c  o  m*/
public boolean visit(QWhile statement) {

    if (statement.getCondition() == null)
        statement.setCondition("true");

    Block block = blocks.peek();

    WhileStatement whileSt = ast.newWhileStatement();

    QPredicateExpression condition = expressionParser.parsePredicate(statement.getCondition());
    Expression expression = buildExpression(ast, condition,
            CompilationContextHelper.getJavaClass(compilationUnit, condition));
    whileSt.setExpression(expression);

    block.statements().add(whileSt);

    // body
    Block bodyBlock = ast.newBlock();
    whileSt.setBody(bodyBlock);

    blocks.push(bodyBlock);

    return super.visit(statement);
}

From source file:org.autorefactor.refactoring.rules.ReduceVariableScopeRefactoring.java

License:Open Source License

private void replace(VariableAccess varDecl, VariableAccess varAccess) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final AST ast = b.getAST();
    final ASTNode scope = varAccess.getScope();
    final Name varName = varAccess.getVariableName();
    final Type varType = getType(varDecl.getVariableName().getParent());
    if (scope instanceof Block) {
        final List<Statement> stmts = statements((Block) scope);
        for (int i = 0; i < stmts.size(); i++) {
            final Statement stmt = stmts.get(i);
            final Expression parentExpr = getAncestor(varName, Expression.class); // FIXME i=0
            final Statement parentStmt = getAncestor(parentExpr, Statement.class); // FIXME i=0
            if (stmt.equals(parentStmt)) {
                final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
                final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
                vds.setType(varType);//from   www .  j  a  v a  2 s. c om
                this.ctx.getRefactorings().replace(stmt, vds);
                break;
            }
        }
    } else if (scope instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) scope;
        final EnhancedForStatement newEfs = b.copy(efs);
        newEfs.setParameter(b.copy(efs.getParameter()));
        newEfs.setExpression(b.copy(efs.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(efs.getBody(), parentStmt)) {
            newEfs.setBody(copy(efs.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(efs, newEfs);
    } else if (scope instanceof ForStatement) {
        final ForStatement fs = (ForStatement) scope;
        final ForStatement newFs = b.copy(fs);
        final List<Expression> initializers = initializers(newFs);
        if (initializers.size() == 1) {
            final Expression init = initializers.remove(0);
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
            final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
            vde.setType(varType);
            initializers.add(vde);
            this.ctx.getRefactorings().replace(fs, newFs);
            // TODO JNR
            // if (equalNotNull(fs.getBody(), parentStmt)) {
            // newFs.setBody(copy(fs.getBody()));
            // }
        } else {
            throw new NotImplementedException(scope, "for more than one initializer in for loop.");
        }
    } else if (scope instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) scope;
        final WhileStatement newWs = ast.newWhileStatement();
        newWs.setExpression(b.copy(ws.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(ws.getBody(), parentStmt)) {
            newWs.setBody(copy(ws.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(ws, newWs);
    } else if (scope instanceof IfStatement) {
        final IfStatement is = (IfStatement) scope;
        final IfStatement newIs = ast.newIfStatement();
        newIs.setExpression(b.copy(is.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(is.getThenStatement(), parentStmt)) {
            newIs.setThenStatement(copy(is.getThenStatement(), varName));
            if (is.getElseStatement() != null) {
                newIs.setElseStatement(b.copy(is.getElseStatement()));
            }
            this.ctx.getRefactorings().replace(is, newIs);
        } else if (equalNotNull(is.getElseStatement(), parentStmt)) {
            if (is.getThenStatement() != null) {
                newIs.setThenStatement(b.copy(is.getThenStatement()));
            }
            newIs.setElseStatement(copy(is.getElseStatement(), varName));
            this.ctx.getRefactorings().replace(is, newIs);
        } else {
            throw new IllegalStateException(is,
                    "Parent statement should be inside the then or else statement of this if statement: " + is);
        }
    } else {
        throw new NotImplementedException(scope);
    }
}

From source file:org.decojer.cavaj.transformers.TrControlFlowStmts.java

License:Open Source License

@Nullable
private Statement transformLoop(@Nonnull final Loop loop) {
    final BB head = loop.getHead();
    boolean negate = true;
    switch (loop.getKind()) {
    case WHILE://from  www .jav  a  2 s  . c  o  m
        negate = false;
    case WHILENOT: {
        final IfStatement ifStatement = (IfStatement) head.getStmt(0);

        final WhileStatement whileStatement = setOp(getAst().newWhileStatement(), getOp(ifStatement));
        final Expression expression = ifStatement.getExpression();
        assert expression != null;
        whileStatement.setExpression(wrap(negate ? not(expression) : expression));

        final E out = negate ? head.getFalseOut() : head.getTrueOut();
        assert out != null;
        whileStatement.setBody(transformSequence(loop, out));
        return whileStatement;
    }
    case DO_WHILE:
        negate = false;
    case DO_WHILENOT: {
        final BB last = loop.getLast();
        final IfStatement ifStatement = (IfStatement) last.getFinalStmt();
        assert ifStatement != null;
        final DoStatement doStatement = setOp(getAst().newDoStatement(), getOp(ifStatement));

        final Expression expression = ifStatement.getExpression();
        assert expression != null;
        doStatement.setExpression(wrap(negate ? not(expression) : expression));

        final List<Statement> statements = ((org.eclipse.jdt.core.dom.Block) doStatement.getBody())
                .statements();
        assert statements != null;
        transformSequence(loop, head, statements);
        return doStatement;
    }
    case ENDLESS: {
        // this while statement hasn't an operation, line number is before first statement,
        // do { ... } while(true); wouldn't change the entry line part, always use while(true)
        final WhileStatement whileStatement = getAst().newWhileStatement();

        whileStatement.setExpression(getAst().newBooleanLiteral(true));

        whileStatement.setBody(transformSequence(loop, head));
        return whileStatement;
    }
    default:
        log.warn(getM() + ": Unknown loop type '" + loop.getKind() + "'!");
        return null;
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public WhileStatement convert(org.eclipse.jdt.internal.compiler.ast.WhileStatement statement) {
    final WhileStatement whileStatement = new WhileStatement(this.ast);
    whileStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
    whileStatement.setExpression(convert(statement.condition));
    final Statement action = convert(statement.action);
    if (action == null)
        return null;
    whileStatement.setBody(action);/* ww  w. j a  v a2  s.  co  m*/
    return whileStatement;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.WhileStatement node) {
    WhileStatement element = (WhileStatement) this.binding.get(node);
    initializeNode(element, node);/*from   w w w  . ja  v a  2 s  . co  m*/

    if (this.binding.get(node.getExpression()) != null) {
        element.setExpression(JDTVisitorUtils.completeExpression(this.binding.get(node.getExpression()), this));
    }

    if (this.binding.get(node.getBody()) != null) {
        element.setBody((Statement) this.binding.get(node.getBody()));
    }
}

From source file:org.mpi.vasco.sieve.staticanalysis.templatecreator.DatabaseTableClassCreator.java

License:Open Source License

/**
 * Creates the unique insert function.//from w ww .  j  a  v  a2 s .  co  m
 *
 * @return the method declaration
 */
public MethodDeclaration createUniqueInsertFunction() {
    String methodName = "uniqueInsert";
    MethodDeclaration methodDeclaration = super.createMethodDeclaration(PrimitiveType.VOID, methodName,
            ModifierKeyword.PUBLIC_KEYWORD);
    String recordTypeName = tableInstance.get_Table_Name() + DatabaseRecordClassCreator.RECORDSTRING;
    String insertRecordArg = "arg0";
    SingleVariableDeclaration varDecl = super.createVariableDeclaration(recordTypeName, insertRecordArg, false);
    methodDeclaration.parameters().add(varDecl);

    org.eclipse.jdt.core.dom.Block block = super.getASTNode().newBlock();
    //add block here
    //new array Type
    ArrayCreation arrayC = super.getASTNode().newArrayCreation();
    arrayC.setType(super.getASTNode()
            .newArrayType(super.getASTNode().newSimpleType(super.getASTNode().newSimpleName(recordTypeName))));
    InfixExpression infixExpr1 = super.getASTNode().newInfixExpression();
    infixExpr1.setOperator(InfixExpression.Operator.PLUS);
    FieldAccess sizeFieldA1 = super.getASTNode().newFieldAccess();
    sizeFieldA1.setExpression(super.getASTNode().newThisExpression());
    sizeFieldA1.setName(super.getASTNode().newSimpleName("size"));
    infixExpr1.setLeftOperand(sizeFieldA1);
    NumberLiteral incrementValue1 = super.getASTNode().newNumberLiteral("1");
    infixExpr1.setRightOperand(incrementValue1);
    arrayC.dimensions().add(infixExpr1);

    //new a table
    VariableDeclarationStatement varDeclStmt = super.createVariableDeclarationStatement(recordTypeName,
            "newTable", true, arrayC);
    block.statements().add(varDeclStmt);

    //for loop to copy data from table to new table

    //add the index variable
    String indexVar = "i";
    NumberLiteral indexLiter = super.getASTNode().newNumberLiteral("0");
    VariableDeclarationStatement intVarDeclStmt = super.createVariableDeclarationStatement(PrimitiveType.INT,
            indexVar, false, indexLiter);
    block.statements().add(intVarDeclStmt);

    //put a for while here
    WhileStatement whileStmt = super.getASTNode().newWhileStatement();

    //set condition expression
    InfixExpression infixExpr = super.getASTNode().newInfixExpression();
    infixExpr.setOperator(InfixExpression.Operator.LESS);
    infixExpr.setLeftOperand(super.getASTNode().newSimpleName(indexVar));
    FieldAccess fieldA = super.getASTNode().newFieldAccess();
    fieldA.setExpression(super.getASTNode().newThisExpression());
    fieldA.setName(super.getASTNode().newSimpleName("size"));
    infixExpr.setRightOperand(fieldA);
    whileStmt.setExpression(infixExpr);

    //set for loop body
    org.eclipse.jdt.core.dom.Block whileLoopBlock = super.getASTNode().newBlock();

    //assignment 
    Assignment tableRecordAssignExpr = super.getASTNode().newAssignment();
    tableRecordAssignExpr.setOperator(Assignment.Operator.ASSIGN);

    //access to the new tale
    ArrayAccess newTableArrayA = super.getASTNode().newArrayAccess();
    newTableArrayA.setArray(super.getASTNode().newSimpleName("newTable"));
    newTableArrayA.setIndex(super.getASTNode().newSimpleName(indexVar));
    tableRecordAssignExpr.setLeftHandSide(newTableArrayA);

    //access to table and get table[i]
    ArrayAccess oldTableArrayA = super.getASTNode().newArrayAccess();
    FieldAccess tableFieldA = super.getASTNode().newFieldAccess();
    tableFieldA.setExpression(super.getASTNode().newThisExpression());
    tableFieldA.setName(super.getASTNode().newSimpleName("table"));
    oldTableArrayA.setArray(tableFieldA);
    oldTableArrayA.setIndex(super.getASTNode().newSimpleName(indexVar));
    tableRecordAssignExpr.setRightHandSide(oldTableArrayA);

    Statement tableCopyAssignStmt = super.getASTNode().newExpressionStatement(tableRecordAssignExpr);

    whileLoopBlock.statements().add(tableCopyAssignStmt);

    //set update
    Assignment updateExpr = super.getASTNode().newAssignment();
    updateExpr.setLeftHandSide(super.getASTNode().newSimpleName(indexVar));
    updateExpr.setOperator(Assignment.Operator.ASSIGN);
    InfixExpression indexIncrementExpr = super.getASTNode().newInfixExpression();
    indexIncrementExpr.setLeftOperand(super.getASTNode().newSimpleName(indexVar));
    indexIncrementExpr.setOperator(InfixExpression.Operator.PLUS);
    indexIncrementExpr.setRightOperand(super.getASTNode().newNumberLiteral("1"));
    updateExpr.setRightHandSide(indexIncrementExpr);

    ExpressionStatement indexUpdateStmt = super.getASTNode().newExpressionStatement(updateExpr);
    whileLoopBlock.statements().add(indexUpdateStmt);

    whileStmt.setBody(whileLoopBlock);
    block.statements().add(whileStmt);

    //newTable[size] = newRecord

    Assignment newRecordAddAssignExpr = super.getASTNode().newAssignment();
    newRecordAddAssignExpr.setOperator(Assignment.Operator.ASSIGN);

    //access to the new tale
    ArrayAccess newRecordAddArrayA = super.getASTNode().newArrayAccess();
    newRecordAddArrayA.setArray(super.getASTNode().newSimpleName("newTable"));
    FieldAccess newRecordAddFieldA = super.getASTNode().newFieldAccess();
    newRecordAddFieldA.setExpression(super.getASTNode().newThisExpression());
    newRecordAddFieldA.setName(super.getASTNode().newSimpleName("size"));
    newRecordAddArrayA.setIndex(newRecordAddFieldA);
    newRecordAddAssignExpr.setLeftHandSide(newRecordAddArrayA);

    //equal to the newRecord
    newRecordAddAssignExpr.setRightHandSide(super.getASTNode().newSimpleName(insertRecordArg));

    Statement newRecordAddAssignStmt = super.getASTNode().newExpressionStatement(newRecordAddAssignExpr);
    block.statements().add(newRecordAddAssignStmt);

    //assign table by newTable
    Assignment tableAssignExpr = super.getASTNode().newAssignment();
    tableAssignExpr.setOperator(Assignment.Operator.ASSIGN);

    //get old table
    FieldAccess tableFieldA1 = super.getASTNode().newFieldAccess();
    tableFieldA1.setExpression(super.getASTNode().newThisExpression());
    tableFieldA1.setName(super.getASTNode().newSimpleName("table"));
    tableAssignExpr.setLeftHandSide(tableFieldA1);

    tableAssignExpr.setRightHandSide(super.getASTNode().newSimpleName("newTable"));
    Statement tableAssignStmt = super.getASTNode().newExpressionStatement(tableAssignExpr);
    block.statements().add(tableAssignStmt);

    //increase size by one
    Assignment sizeAssignExpr = super.getASTNode().newAssignment();
    sizeAssignExpr.setOperator(Assignment.Operator.ASSIGN);
    FieldAccess sizeFieldA = super.getASTNode().newFieldAccess();
    sizeFieldA.setExpression(super.getASTNode().newThisExpression());
    sizeFieldA.setName(super.getASTNode().newSimpleName("size"));
    sizeAssignExpr.setLeftHandSide(sizeFieldA);
    InfixExpression sizeInfixExpr = super.getASTNode().newInfixExpression();
    sizeInfixExpr.setOperator(InfixExpression.Operator.PLUS);
    FieldAccess sizeFieldA2 = super.getASTNode().newFieldAccess();
    sizeFieldA2.setExpression(super.getASTNode().newThisExpression());
    sizeFieldA2.setName(super.getASTNode().newSimpleName("size"));
    sizeInfixExpr.setLeftOperand(sizeFieldA2);
    NumberLiteral incrementValue = super.getASTNode().newNumberLiteral("1");
    sizeInfixExpr.setRightOperand(incrementValue);
    sizeAssignExpr.setRightHandSide(sizeInfixExpr);

    ExpressionStatement exprStmt = super.getASTNode().newExpressionStatement(sizeAssignExpr);

    block.statements().add(exprStmt);
    methodDeclaration.setBody(block);

    //add specs
    List<String> specs = new ArrayList<String>();
    String requireSpecs = JahobSpecsUtil.requirePrefix
            + JahobSpecsUtil.getUniqueInsertRequireSpecs(this.getClassName(), insertRecordArg) + "\"";
    String modifySpecs = JahobSpecsUtil.modifyPrefix
            + JahobSpecsUtil.getModifyContentsAndSize(this.getClassName());
    String ensureSpecs = JahobSpecsUtil.ensurePrefix
            + JahobSpecsUtil.getEnsureSizeIncreaseByOne(this.getClassName()) + " & "
            + JahobSpecsUtil.getEnsureContentEnlargedByOne(this.getClassName(), insertRecordArg) + "\"";
    specs.add(requireSpecs);
    specs.add(modifySpecs);
    specs.add(ensureSpecs);
    this.methodSpecs.put(methodName, specs);

    //loop invariants
    String loopInvSpecs = " inv \" "
            + JahobSpecsUtil.getUniqueInsertLoopInvStr(this.getClassName(), indexVar, "newTable") + "\"";
    this.loopInvartiantSpecs.put(methodName, loopInvSpecs);
    return methodDeclaration;
}

From source file:org.mpi.vasco.sieve.staticanalysis.templatecreator.DatabaseTableClassCreator.java

License:Open Source License

/**
 * Creates the get record function.// w  ww  .ja v  a2  s  . co m
 *
 * @return the method declaration
 */
public MethodDeclaration createGetRecordFunction() {
    String methodName = "getRecord";
    String recordTypeName = tableInstance.get_Table_Name() + DatabaseRecordClassCreator.RECORDSTRING;
    MethodDeclaration methodDeclaration = super.createMethodDeclaration(recordTypeName, methodName,
            ModifierKeyword.PUBLIC_KEYWORD);

    String requireSpecs = JahobSpecsUtil.requirePrefix;
    String ensureSpecs = JahobSpecsUtil.ensurePrefix;
    List<String> specs = new ArrayList<String>();
    requireSpecs += JahobSpecsUtil.getTableInit(this.getClassName());
    String ensureExist = "(" + JahobSpecsUtil.getExistencePrefix(this.getClassName());
    String ensureNoExist = "";
    //get all primary key as parameters
    List<DataField> primaryKeyList = this.tableInstance.getPrimaryKeyDataFieldList();
    int index = 0;
    for (DataField df : primaryKeyList) {
        String crdtObjectType = CrdtFactory.getProperCrdtObject(df.get_Crdt_Data_Type(), df.get_Data_Type());
        String primaryParaName = "pk" + index;
        SingleVariableDeclaration varDecl = super.createVariableDeclaration(crdtObjectType, primaryParaName,
                false);
        methodDeclaration.parameters().add(varDecl);
        index = index + 1;
        requireSpecs += " & " + JahobSpecsUtil.getRequireNotNullClause(primaryParaName);
        ensureExist += " & " + JahobSpecsUtil.getTableRecordEnsureFieldEqual(recordTypeName,
                df.get_Data_Field_Name(), primaryParaName);
    }
    requireSpecs += "\"";
    ensureNoExist = "(~" + ensureExist + ")";
    ensureExist += " --> " + JahobSpecsUtil.getGetRecordResultEqual(true) + ")";
    ensureNoExist += "-->" + JahobSpecsUtil.getGetRecordResultEqual(false) + ")";

    ensureSpecs += ensureExist + " \\<or> \n\t" + ensureNoExist + "\"";

    org.eclipse.jdt.core.dom.Block block = super.getASTNode().newBlock();
    //add block here
    //for loop
    //add the index variable
    String indexVar = "i";
    NumberLiteral indexInitializedValue = super.getASTNode().newNumberLiteral("0");
    VariableDeclarationStatement varDeclStmt = super.createVariableDeclarationStatement(PrimitiveType.INT,
            indexVar, false, indexInitializedValue);
    block.statements().add(varDeclStmt);

    //put a while loop here
    WhileStatement whileStmt = super.getASTNode().newWhileStatement();

    //set condition expression
    InfixExpression infixExpr = super.getASTNode().newInfixExpression();
    infixExpr.setOperator(InfixExpression.Operator.LESS);
    infixExpr.setLeftOperand(super.getASTNode().newSimpleName(indexVar));

    FieldAccess fieldA = super.getASTNode().newFieldAccess();
    fieldA.setExpression(super.getASTNode().newThisExpression());
    fieldA.setName(super.getASTNode().newSimpleName("size"));
    infixExpr.setRightOperand(fieldA);

    whileStmt.setExpression(infixExpr);

    //set for loop body
    org.eclipse.jdt.core.dom.Block whileLoopBlock = super.getASTNode().newBlock();

    //add a if statement

    IfStatement ifStmt = super.getASTNode().newIfStatement();

    //get all infix sub expression
    List<InfixExpression> infixExprList = new ArrayList<InfixExpression>();
    for (int i = 0; i < primaryKeyList.size(); i++) {
        DataField df = primaryKeyList.get(i);
        //set if condition
        //MethodInvocation
        //MethodInvocation outerMethodInvocation = super.getASTNode().newMethodInvocation();
        MethodInvocation methodInvocation = super.getASTNode().newMethodInvocation();

        //left expr is in the form: this.table[i]
        ArrayAccess arrayA = super.getASTNode().newArrayAccess();
        FieldAccess tableFieldA = super.getASTNode().newFieldAccess();
        tableFieldA.setExpression(super.getASTNode().newThisExpression());
        tableFieldA.setName(super.getASTNode().newSimpleName("table"));
        arrayA.setArray(tableFieldA);
        arrayA.setIndex(super.getASTNode().newSimpleName(indexVar));

        methodInvocation.setExpression(arrayA);
        methodInvocation.setName(super.getASTNode().newSimpleName("get" + df.get_Data_Field_Name()));

        InfixExpression ifConditionExpr = super.getASTNode().newInfixExpression();
        ifConditionExpr.setOperator(InfixExpression.Operator.EQUALS);
        ifConditionExpr.setLeftOperand(methodInvocation);
        ifConditionExpr.setRightOperand(super.getASTNode().newSimpleName("pk" + i));

        //outerMethodInvocation.setExpression(methodInvocation);
        //outerMethodInvocation.setName(super.getASTNode().newSimpleName("equalTo"));
        //outerMethodInvocation.arguments().add(super.getASTNode().newSimpleName("pk"+i));

        infixExprList.add(ifConditionExpr);
    }

    if (infixExprList.size() == 1) {
        ifStmt.setExpression(infixExprList.get(0));
    } else {
        InfixExpression expr = this.combineInfixExprs(infixExprList);
        ifStmt.setExpression(expr);
    }

    //add block for ifStmt
    org.eclipse.jdt.core.dom.Block ifBlock = super.getASTNode().newBlock();

    //return the current record
    ArrayAccess returnArrayA = super.getASTNode().newArrayAccess();
    FieldAccess returnTableFieldA = super.getASTNode().newFieldAccess();
    returnTableFieldA.setExpression(super.getASTNode().newThisExpression());
    returnTableFieldA.setName(super.getASTNode().newSimpleName("table"));
    returnArrayA.setArray(returnTableFieldA);
    returnArrayA.setIndex(super.getASTNode().newSimpleName(indexVar));
    ReturnStatement indexReturnStmt = super.getASTNode().newReturnStatement();
    indexReturnStmt.setExpression(returnArrayA);

    ifBlock.statements().add(indexReturnStmt);
    ifStmt.setThenStatement(ifBlock);
    whileLoopBlock.statements().add(ifStmt);

    //set update
    Assignment updateExpr = super.getASTNode().newAssignment();
    updateExpr.setLeftHandSide(super.getASTNode().newSimpleName(indexVar));
    updateExpr.setOperator(Assignment.Operator.ASSIGN);
    InfixExpression indexIncrementExpr = super.getASTNode().newInfixExpression();
    indexIncrementExpr.setLeftOperand(super.getASTNode().newSimpleName(indexVar));
    indexIncrementExpr.setOperator(InfixExpression.Operator.PLUS);
    indexIncrementExpr.setRightOperand(super.getASTNode().newNumberLiteral("1"));
    updateExpr.setRightHandSide(indexIncrementExpr);

    ExpressionStatement indexUpdateStmt = super.getASTNode().newExpressionStatement(updateExpr);
    whileLoopBlock.statements().add(indexUpdateStmt);
    whileStmt.setBody(whileLoopBlock);

    block.statements().add(whileStmt);
    methodDeclaration.setBody(block);

    //return null if not found
    ReturnStatement noFoundReturnStmt = super.getASTNode().newReturnStatement();
    NullLiteral noFoundValue = super.getASTNode().newNullLiteral();
    noFoundReturnStmt.setExpression(noFoundValue);
    block.statements().add(noFoundReturnStmt);

    specs.add(requireSpecs);
    specs.add(ensureSpecs);
    this.methodSpecs.put(methodName, specs);

    //loop invariants
    String loopInvSpecs = " inv \" "
            + JahobSpecsUtil.getGetRecordLoopInvariant(indexVar, recordTypeName, primaryKeyList) + "\"";
    this.loopInvartiantSpecs.put(methodName, loopInvSpecs);
    return methodDeclaration;
}

From source file:org.smeup.sys.dk.compiler.rpj.writer.JDTStatementWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w w  w.  jav  a  2 s  .  co m
public boolean visit(QWhile statement) {

    Block block = blocks.peek();

    WhileStatement whileSt = ast.newWhileStatement();

    QPredicateExpression condition = buildIterationCondition(statement.getCondition());
    Expression expression = JDTStatementHelper.buildExpression(ast, compilationUnit, condition,
            RPJContextHelper.getTargetClass(compilationUnit, condition, true));
    whileSt.setExpression(expression);

    block.statements().add(whileSt);

    // body
    Block bodyBlock = ast.newBlock();
    whileSt.setBody(bodyBlock);

    blocks.push(bodyBlock);

    return super.visit(statement);
}

From source file:org.whole.lang.java.util.JDTTransformerVisitor.java

License:Open Source License

public boolean visit(WhileStatement node) {
    org.whole.lang.java.model.WhileStatement whileStm = lf.create(JavaEntityDescriptorEnum.WhileStatement);

    acceptChild(node.getExpression());//from  w w  w. j a va 2  s.com
    whileStm.setExpression(exp);

    acceptChild(node.getBody());
    whileStm.setBody(stm);

    stm = whileStm;
    return false;
}