Example usage for org.eclipse.jdt.core.dom Block statements

List of usage examples for org.eclipse.jdt.core.dom Block statements

Introduction

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

Prototype

ASTNode.NodeList statements

To view the source code for org.eclipse.jdt.core.dom Block statements.

Click Source Link

Document

The list of statements (element type: Statement ).

Usage

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

License:Open Source License

/**
 * Creates the unique insert function./*from   ww w  .j  a  va2  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 insert function./*from   w  w w  .j  a  v a  2s .  com*/
 *
 * @return the method declaration
 */
public MethodDeclaration createInsertFunction() {
    String methodName = "insert";
    MethodDeclaration methodDeclaration = super.createMethodDeclaration(PrimitiveType.VOID, methodName,
            ModifierKeyword.PUBLIC_KEYWORD);
    String recordTypeName = tableInstance.get_Table_Name() + DatabaseRecordClassCreator.RECORDSTRING;
    String argName = "arg0";
    String recordName = "temp";
    SingleVariableDeclaration varDecl = super.createVariableDeclaration(recordTypeName, argName, false);
    methodDeclaration.parameters().add(varDecl);

    List<DataField> updateDataFieldList = this.tableInstance.getModifiableDataFieldList();
    List<DataField> primaryKeyList = this.tableInstance.getPrimaryKeyDataFieldList();

    org.eclipse.jdt.core.dom.Block block = super.getASTNode().newBlock();

    //try to getRecord from the table
    //define a variable declaration
    //MethodInvocation
    MethodInvocation methodInvocation = super.getASTNode().newMethodInvocation();
    methodInvocation.setExpression(super.getASTNode().newThisExpression());
    methodInvocation.setName(super.getASTNode().newSimpleName("getRecord"));
    int index = 0;
    for (DataField df : primaryKeyList) {
        MethodInvocation innerMInvocation = super.getASTNode().newMethodInvocation();
        innerMInvocation.setExpression(super.getASTNode().newSimpleName(argName));
        innerMInvocation.setName(super.getASTNode().newSimpleName("get" + df.get_Data_Field_Name()));
        methodInvocation.arguments().add(innerMInvocation);
        index = index + 1;
    }
    VariableDeclarationStatement varDeclStmt = super.createVariableDeclarationStatement(recordTypeName,
            recordName, false, methodInvocation);
    block.statements().add(varDeclStmt);
    //check if it is contained in the table
    IfStatement ifStmt = super.getASTNode().newIfStatement();
    //set if condition
    InfixExpression infixExpr = super.getASTNode().newInfixExpression();
    infixExpr.setOperator(InfixExpression.Operator.NOT_EQUALS);
    infixExpr.setLeftOperand(super.getASTNode().newSimpleName(recordName));

    NullLiteral nullLiteral = super.getASTNode().newNullLiteral();
    infixExpr.setRightOperand(nullLiteral);
    ifStmt.setExpression(infixExpr);
    //set if block
    org.eclipse.jdt.core.dom.Block ifBlock = super.getASTNode().newBlock();
    //get lww logical timestamp
    MethodInvocation fieldAccessMInvoc = super.getASTNode().newMethodInvocation();
    fieldAccessMInvoc.setExpression(super.getASTNode().newSimpleName(argName));
    fieldAccessMInvoc
            .setName(super.getASTNode().newSimpleName("get" + tableInstance.getLwwTs().get_Data_Field_Name()));
    VariableDeclarationStatement fieldAccessVarDeclStmt = super.createVariableDeclarationStatement(
            CrdtFactory.getLwwLogicalTimestampCrdtTypeString(), "lwwLts", false, fieldAccessMInvoc);
    ifBlock.statements().add(fieldAccessVarDeclStmt);
    //call record
    for (int i = 0; i < updateDataFieldList.size(); i++) {
        DataField df = updateDataFieldList.get(i);
        if (!CrdtFactory.isLwwLogicalTimestamp(df.get_Crdt_Data_Type()) && !df.is_Primary_Key()) {

            if (CrdtFactory.isLwwDeletedFlag(df.get_Crdt_Data_Type())) {
                //undelete this field
                MethodInvocation updateLwwMInvoc = super.getASTNode().newMethodInvocation();
                updateLwwMInvoc.setExpression(super.getASTNode().newSimpleName(recordName));
                updateLwwMInvoc
                        .setName(super.getASTNode().newSimpleName("undelete" + df.get_Data_Field_Name()));
                updateLwwMInvoc.arguments().add(super.getASTNode().newSimpleName("lwwLts"));
                ExpressionStatement updateLwwMInvocExprStmt = super.getASTNode()
                        .newExpressionStatement(updateLwwMInvoc);
                ifBlock.statements().add(updateLwwMInvocExprStmt);
            } else {
                String crdtType = CrdtFactory.getProperCrdtObject(df.get_Crdt_Data_Type(), df.get_Data_Type());
                fieldAccessMInvoc = super.getASTNode().newMethodInvocation();
                fieldAccessMInvoc.setExpression(super.getASTNode().newSimpleName(argName));
                fieldAccessMInvoc.setName(super.getASTNode().newSimpleName("get" + df.get_Data_Field_Name()));
                fieldAccessVarDeclStmt = super.createVariableDeclarationStatement(crdtType,
                        df.get_Data_Field_Name(), false, fieldAccessMInvoc);
                ifBlock.statements().add(fieldAccessVarDeclStmt);
                //call update for the field
                if (CrdtFactory.isLwwType(df.get_Crdt_Data_Type())) {
                    //add lwwlogicaltimestamp as parameter
                    MethodInvocation updateLwwMInvoc = super.getASTNode().newMethodInvocation();
                    updateLwwMInvoc.setExpression(super.getASTNode().newSimpleName(recordName));
                    updateLwwMInvoc
                            .setName(super.getASTNode().newSimpleName("update" + df.get_Data_Field_Name()));
                    updateLwwMInvoc.arguments().add(super.getASTNode().newSimpleName(df.get_Data_Field_Name()));
                    updateLwwMInvoc.arguments().add(super.getASTNode().newSimpleName("lwwLts"));
                    ExpressionStatement updateLwwMInvocExprStmt = super.getASTNode()
                            .newExpressionStatement(updateLwwMInvoc);
                    ifBlock.statements().add(updateLwwMInvocExprStmt);
                } else {
                    MethodInvocation updateMInvoc = super.getASTNode().newMethodInvocation();
                    updateMInvoc.setExpression(super.getASTNode().newSimpleName(recordName));
                    updateMInvoc.setName(super.getASTNode().newSimpleName("update" + df.get_Data_Field_Name()));
                    updateMInvoc.arguments().add(super.getASTNode().newSimpleName(df.get_Data_Field_Name()));
                    ExpressionStatement updateMInvocExprStmt = super.getASTNode()
                            .newExpressionStatement(updateMInvoc);
                    ifBlock.statements().add(updateMInvocExprStmt);
                }
            }
        }
    }

    //update lww logical timestamp
    MethodInvocation updateLwwLogicalTimestampMInvoc = super.getASTNode().newMethodInvocation();
    updateLwwLogicalTimestampMInvoc.setExpression(super.getASTNode().newSimpleName(recordName));
    updateLwwLogicalTimestampMInvoc
            .setName(super.getASTNode().newSimpleName("update" + LWW_LOGICALTIMESTAMP.logical_Timestamp_Name));
    updateLwwLogicalTimestampMInvoc.arguments().add(super.getASTNode().newSimpleName("lwwLts"));
    ExpressionStatement updateLwwLogicalTimestampExprStmt = super.getASTNode()
            .newExpressionStatement(updateLwwLogicalTimestampMInvoc);
    ifBlock.statements().add(updateLwwLogicalTimestampExprStmt);
    // if not then please call insert
    ifStmt.setThenStatement(ifBlock);
    //set then block
    org.eclipse.jdt.core.dom.Block thenBlock = super.getASTNode().newBlock();

    //call uniqueInsert      
    MethodInvocation uInsertMInvocation = super.getASTNode().newMethodInvocation();
    uInsertMInvocation.setExpression(super.getASTNode().newThisExpression());
    uInsertMInvocation.setName(super.getASTNode().newSimpleName("uniqueInsert"));
    uInsertMInvocation.arguments().add(super.getASTNode().newSimpleName(argName));

    ExpressionStatement exprStmt = super.getASTNode().newExpressionStatement(uInsertMInvocation);
    thenBlock.statements().add(exprStmt);

    ifStmt.setElseStatement(thenBlock);
    block.statements().add(ifStmt);
    methodDeclaration.setBody(block);

    //add specs
    List<String> specs = new ArrayList<String>();
    String requireSpecs = JahobSpecsUtil.requirePrefix
            + JahobSpecsUtil.getInsertRequireSpecs(this.getClassName(), argName) + "\"";
    String modifySpecs = JahobSpecsUtil.modifyPrefix
            + JahobSpecsUtil.getInsertModifySpecs(this.getClassName(), tableInstance.getDataFieldList());
    String ensureSpecs = JahobSpecsUtil.ensurePrefix + JahobSpecsUtil.getInsertEnsure(this.getClassName(),
            recordTypeName, "v", argName, tableInstance.getDataFieldList()) + "\"";
    specs.add(requireSpecs);
    specs.add(modifySpecs);
    specs.add(ensureSpecs);
    this.methodSpecs.put(methodName, specs);

    return methodDeclaration;
}

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

License:Open Source License

/**
 * Creates the get index function./*from  ww  w.j  av  a 2 s  .c  o  m*/
 *
 * @return the method declaration
 */
public MethodDeclaration createGetIndexFunction() {
    String methodName = "getIndex";
    MethodDeclaration methodDeclaration = super.createMethodDeclaration(PrimitiveType.INT, methodName,
            ModifierKeyword.PUBLIC_KEYWORD);
    String recordName = tableInstance.get_Table_Name() + DatabaseRecordClassCreator.RECORDSTRING;
    SingleVariableDeclaration varDecl = super.createVariableDeclaration(recordName, recordName.toLowerCase(),
            false);
    methodDeclaration.parameters().add(varDecl);
    org.eclipse.jdt.core.dom.Block block = super.getASTNode().newBlock();
    //add the index variable
    String indexVar = "i";
    VariableDeclarationStatement varDeclStmt = super.createVariableDeclarationStatement(PrimitiveType.INT,
            indexVar, false, null);
    block.statements().add(varDeclStmt);

    //put a for loop here
    ForStatement forStmt = super.getASTNode().newForStatement();
    //set initializer
    Assignment assignExpr = super.getASTNode().newAssignment();
    assignExpr.setOperator(Assignment.Operator.ASSIGN);
    assignExpr.setLeftHandSide(super.getASTNode().newSimpleName(indexVar));
    NumberLiteral initializedValue = super.getASTNode().newNumberLiteral("0");
    assignExpr.setRightHandSide(initializedValue);
    forStmt.initializers().add(assignExpr);

    //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);

    forStmt.setExpression(infixExpr);

    //set update
    PostfixExpression updateExpr = super.getASTNode().newPostfixExpression();
    updateExpr.setOperator(PostfixExpression.Operator.INCREMENT);
    updateExpr.setOperand(super.getASTNode().newSimpleName(indexVar));

    forStmt.updaters().add(updateExpr);

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

    //add a if statement

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

    //set if condition
    InfixExpression ifCondExp = super.getASTNode().newInfixExpression();
    //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));
    ifCondExp.setLeftOperand(arrayA);
    //equal operator
    ifCondExp.setOperator(InfixExpression.Operator.EQUALS);
    //right expr
    ifCondExp.setRightOperand(super.getASTNode().newSimpleName(recordName.toLowerCase()));

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

    ReturnStatement indexReturnStmt = super.getASTNode().newReturnStatement();
    indexReturnStmt.setExpression(super.getASTNode().newSimpleName(indexVar));

    ifBlock.statements().add(indexReturnStmt);
    ifStmt.setThenStatement(ifBlock);
    forLoopBlock.statements().add(ifStmt);
    forStmt.setBody(forLoopBlock);

    block.statements().add(forStmt);

    //return false
    ReturnStatement noIndexReturnStmt = super.getASTNode().newReturnStatement();
    NumberLiteral noIndexValue = super.getASTNode().newNumberLiteral("-1");
    noIndexReturnStmt.setExpression(noIndexValue);
    block.statements().add(noIndexReturnStmt);

    methodDeclaration.setBody(block);
    return methodDeclaration;
}

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

License:Open Source License

/**
 * Creates the get record function.//from   w ww .j a  v  a2 s  .  c  o  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.mpi.vasco.sieve.staticanalysis.templatecreator.DatabaseTableClassCreator.java

License:Open Source License

/**
 * Creates the delete function./*from  w ww.j  a va 2 s.  c  om*/
 *
 * @return the method declaration
 */
public MethodDeclaration createDeleteFunction() {
    String methodName = "delete";
    MethodDeclaration methodDeclaration = super.createMethodDeclaration(PrimitiveType.VOID, methodName,
            ModifierKeyword.PUBLIC_KEYWORD);
    String recordName = tableInstance.get_Table_Name() + DatabaseRecordClassCreator.RECORDSTRING;

    String requireSpecs = JahobSpecsUtil.requirePrefix;
    String modifySpecs = JahobSpecsUtil.modifyPrefix;
    String ensureSpecs = JahobSpecsUtil.ensurePrefix;
    List<String> specs = new ArrayList<String>();
    requireSpecs += JahobSpecsUtil.getTableInit(this.getClassName());

    //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);
    }

    //add lwwlogicaltimestamp
    String lwwLtsType = CrdtFactory.getLwwLogicalTimestampCrdtTypeString();
    String lwwLtsName = "lwwLTS";
    SingleVariableDeclaration varDecl = super.createVariableDeclaration(lwwLtsType, lwwLtsName, false);
    methodDeclaration.parameters().add(varDecl);
    requireSpecs += " & " + JahobSpecsUtil.getRequireNotNullClause(lwwLtsName);
    //existence
    requireSpecs += " & "
            + JahobSpecsUtil.getExistenceStrByPrimaryKeyList(this.getClassName(), recordName, primaryKeyList);
    requireSpecs += "\"";
    org.eclipse.jdt.core.dom.Block block = super.getASTNode().newBlock();
    //add block here
    //get record from call the getRecord function
    //define a variable declaration
    //MethodInvocation
    MethodInvocation methodInvocation = super.getASTNode().newMethodInvocation();
    methodInvocation.setExpression(super.getASTNode().newThisExpression());
    methodInvocation.setName(super.getASTNode().newSimpleName("getRecord"));
    index = 0;
    for (DataField df : primaryKeyList) {
        methodInvocation.arguments().add(super.getASTNode().newSimpleName("pk" + index));
        index = index + 1;
    }
    VariableDeclarationStatement varDeclStmt = super.createVariableDeclarationStatement(recordName,
            recordName.toLowerCase(), false, methodInvocation);
    block.statements().add(varDeclStmt);
    //call update lwwDeletedFlag

    MethodInvocation updateLwwDeletedFlagMInvoc = super.getASTNode().newMethodInvocation();
    updateLwwDeletedFlagMInvoc.setExpression(super.getASTNode().newSimpleName(recordName.toLowerCase()));
    updateLwwDeletedFlagMInvoc
            .setName(super.getASTNode().newSimpleName("update" + LWW_DELETEDFLAG.deleted_Flag_Name));
    updateLwwDeletedFlagMInvoc.arguments().add(super.getASTNode().newSimpleName("lwwLTS"));
    ExpressionStatement updateLwwDeletedFlagExprStmt = super.getASTNode()
            .newExpressionStatement(updateLwwDeletedFlagMInvoc);
    block.statements().add(updateLwwDeletedFlagExprStmt);

    methodDeclaration.setBody(block);

    modifySpecs += JahobSpecsUtil.getModifyField(CrdtDataFieldType.LWWBOOLEAN);
    ensureSpecs += JahobSpecsUtil.getExistencePrefix(this.getClassName()) + " & (";
    //two conditions
    ensureSpecs += JahobSpecsUtil.getEnsureClauseForLwwDeleteFlagForTable("v.." + recordName,
            LWW_DELETEDFLAG.deleted_Flag_Name, CrdtDataFieldType.LWWBOOLEAN, "lwwLTS");
    ensureSpecs += ")\"";
    specs.add(requireSpecs);
    specs.add(modifySpecs);
    specs.add(ensureSpecs);
    this.methodSpecs.put(methodName, specs);
    return methodDeclaration;
}

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

License:Open Source License

/**
 * Generate code for shadow op template.
 *
 * @param shdOpTemplate the shd op template
 * @return the method declaration/*w w w. j a va 2 s  .co  m*/
 */
private MethodDeclaration generateCodeForShadowOpTemplate(ShadowOperationTemplate shdOpTemplate) {
    String templateName = shdOpTemplate.getUniqueTemplateId();
    MethodDeclaration methodDeclaration = super.createMethodDeclaration(PrimitiveType.VOID, templateName,
            ModifierKeyword.PUBLIC_KEYWORD);
    String requireSpecs = JahobSpecsUtil.requirePrefix;
    String modifySpecs = JahobSpecsUtil.modifyPrefix;
    String ensureSpecs = JahobSpecsUtil.ensurePrefix;
    org.eclipse.jdt.core.dom.Block block = super.getASTNode().newBlock();
    List<Operation> opList = shdOpTemplate.getCrdtOpList();
    HashSet<String> tableNameList = new HashSet<String>();
    List<Statement> statementList = new ArrayList<Statement>();
    List<SingleVariableDeclaration> varList = new ArrayList<SingleVariableDeclaration>();
    List<String> insertTables = new ArrayList<String>();
    List<String> deleteTables = new ArrayList<String>();
    List<DataField> updateFields = new ArrayList<DataField>();
    for (int i = 0; i < opList.size(); i++) {
        Operation op = opList.get(i);
        tableNameList.add(op.getTableName());
        LinkedHashMap<Statement, List<SingleVariableDeclaration>> returnValue = null;
        if (op instanceof UniqueInsertOperation) {
            returnValue = this.createStatementsForUniqueInsertOperation((UniqueInsertOperation) op);
            insertTables.add(op.getTableName());
        } else if (op instanceof InsertOperation) {
            returnValue = this.createStatementsForInsertOperation((InsertOperation) op);
            insertTables.add(op.getTableName());
            updateFields.addAll(op.getTableInstance().getModifiableDataFieldList());
        } else if (op instanceof UpdateOperation) {
            returnValue = this.createStatementsForUpdateOperation((UpdateOperation) op);
            updateFields.addAll(((UpdateOperation) op).getModifiedDataFields());
        } else if (op instanceof DeleteOperation) {
            returnValue = this.createStatementsForDeleteOperation((DeleteOperation) op);
            deleteTables.add(op.getTableName());
        } else {
            throw new RuntimeException("can not be here");
        }

        Iterator<Entry<Statement, List<SingleVariableDeclaration>>> it = returnValue.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Statement, List<SingleVariableDeclaration>> itEntry = it.next();
            statementList.add(itEntry.getKey());
            varList.addAll(itEntry.getValue());
        }
    }
    //get table vars
    List<String> missingTables = JahobSpecsUtil.findMissingTableForInvariants(tableNameList,
            this.getInvParser().getAllTables());
    tableNameList.addAll(missingTables);
    List<SingleVariableDeclaration> tableVars = this.createAllTableVariables(tableNameList);
    //requireSpecs += JahobSpecsUtil.getAllTableInitAndNotNull(tableVars);
    String invSpecs = JahobSpecsUtil.getInvSpecs(this.getInvParser().getInvariants(), tableVars);
    tableVars.addAll(varList);
    //requireSpecs += " & " + JahobSpecsUtil.getAllArgumentsNotNull(varList);
    for (SingleVariableDeclaration var : tableVars) {
        methodDeclaration.parameters().add(var);
    }
    //add the logical timestamp var
    SingleVariableDeclaration lwwLts = super.createVariableDeclaration(
            CrdtFactory.getLwwLogicalTimestampCrdtTypeString(), LWWLTS_VALUE, false);
    methodDeclaration.parameters().add(lwwLts);
    block.statements().addAll(statementList);
    methodDeclaration.setBody(block);

    List<String> specs = new ArrayList<String>();
    //requireSpecs += " & " + JahobSpecsUtil.getRequireNotNullClause(LWWLTS_VALUE);
    //requireSpecs += " & " +invSpecs + "\"";
    requireSpecs += invSpecs + "\"";
    modifySpecs += JahobSpecsUtil.getModifyClauseForTemplate(insertTables, deleteTables, updateFields);
    specs.add(requireSpecs);
    specs.add(modifySpecs);
    ensureSpecs += invSpecs + "\"";
    specs.add(ensureSpecs);
    this.methodSpecs.put(templateName, specs);
    return methodDeclaration;
}

From source file:org.onehippo.cms7.essentials.dashboard.utils.code.NoAnnotationMethodVisitor.java

License:Apache License

@Override
public boolean visit(MethodDeclaration node) {
    final List<?> modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof NormalAnnotation) {
            final NormalAnnotation annotation = (NormalAnnotation) modifier;
            final String fullyQualifiedName = annotation.getTypeName().getFullyQualifiedName();
            if (HippoEssentialsGenerated.class.getSimpleName().equals(fullyQualifiedName)) {
                return super.visit(node);
            }/*from  ww  w.jav  a  2  s.  c o  m*/
        }
    }
    // no HippoEssentialsGenerated annotation found, analyze method and see if we can annotate it:
    final Block body = node.getBody();
    @SuppressWarnings("rawtypes")
    final List statements = body.statements();
    processStatements(node, statements);
    return super.visit(node);
}

From source file:org.onehippo.cms7.essentials.dashboard.utils.JavaSourceUtils.java

License:Apache License

@SuppressWarnings(UNCHECKED)
private static void addSimpleMethod(final String hippoMethodName, final Path path, final String methodName,
        final String propertyName, final String returnType) {
    final CompilationUnit unit = getCompilationUnit(path);
    unit.recordModifications();//  w w w. j av a  2  s  .c o  m
    final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0);
    final AST ast = unit.getAST();
    final MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
    methodDeclaration.setName(ast.newSimpleName(methodName));
    if (returnType.indexOf('[') != -1) {
        final String type = ARRAY_PATTERN.matcher(returnType).replaceAll("");
        methodDeclaration.setReturnType2(ast.newArrayType(ast.newSimpleType(ast.newSimpleName(type))));
    } else {
        methodDeclaration.setReturnType2(ast.newSimpleType(ast.newSimpleName(returnType)));
    }
    methodDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    methodDeclaration.setConstructor(false);
    final Block body = ast.newBlock();
    methodDeclaration.setBody(body);
    final ReturnStatement statement = ast.newReturnStatement();
    final MethodInvocation expression = ast.newMethodInvocation();
    expression.setName(ast.newSimpleName(hippoMethodName));
    final StringLiteral literal = ast.newStringLiteral();
    literal.setLiteralValue(propertyName);
    expression.arguments().add(literal);
    statement.setExpression(expression);
    body.statements().add(statement);
    classType.bodyDeclarations().add(methodDeclaration);
    // add annotation
    final MarkerAnnotation generatedAnnotation = ast.newMarkerAnnotation();
    generatedAnnotation.setTypeName(ast.newName(HippoEssentialsGenerated.class.getSimpleName()));
    addHippoGeneratedAnnotation(propertyName, unit, methodDeclaration, ast);
    replaceFile(path, unit, ast);
}

From source file:org.onehippo.cms7.essentials.dashboard.utils.JavaSourceUtils.java

License:Apache License

@SuppressWarnings(UNCHECKED)

public static void addParameterizedMethod(final String methodName, final String returnType,
        final String genericsType, final Path path, final String returnMethodName, final String propertyName) {
    final CompilationUnit unit = getCompilationUnit(path);
    unit.recordModifications();//from   w w w . ja  v a 2 s . c  o  m
    final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0);
    final AST ast = unit.getAST();
    final MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
    methodDeclaration.setName(ast.newSimpleName(methodName));
    final ParameterizedType type = ast.newParameterizedType(ast.newSimpleType(ast.newName(returnType)));
    type.typeArguments().add(ast.newSimpleType(ast.newSimpleName(genericsType)));
    methodDeclaration.setReturnType2(type);
    methodDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    methodDeclaration.setConstructor(false);
    final Block body = ast.newBlock();
    methodDeclaration.setBody(body);
    final ReturnStatement statement = ast.newReturnStatement();
    final MethodInvocation expression = ast.newMethodInvocation();
    expression.setName(ast.newSimpleName(returnMethodName));
    // arguments
    final StringLiteral literal = ast.newStringLiteral();
    literal.setLiteralValue(propertyName);
    expression.arguments().add(literal);
    // Class argument
    TypeLiteral classLiteral = ast.newTypeLiteral();

    classLiteral.setType(ast.newSimpleType(ast.newName(genericsType)));
    expression.arguments().add(classLiteral);
    //
    statement.setExpression(expression);
    body.statements().add(statement);
    classType.bodyDeclarations().add(methodDeclaration);
    // add annotation
    final MarkerAnnotation generatedAnnotation = ast.newMarkerAnnotation();
    generatedAnnotation.setTypeName(ast.newName(HippoEssentialsGenerated.class.getSimpleName()));
    addHippoGeneratedAnnotation(propertyName, unit, methodDeclaration, ast);
    replaceFile(path, unit, ast);

}

From source file:org.onehippo.cms7.essentials.dashboard.utils.JavaSourceUtils.java

License:Apache License

@SuppressWarnings(UNCHECKED)
public static void addTwoArgumentsMethod(final String returnMethodName, final String returnType,
        final Path path, final String methodName, final String propertyName) {
    final CompilationUnit unit = getCompilationUnit(path);
    unit.recordModifications();//from w w w.  ja v  a 2  s .  co  m
    final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0);
    final AST ast = unit.getAST();
    final MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
    methodDeclaration.setName(ast.newSimpleName(methodName));
    methodDeclaration.setReturnType2(ast.newSimpleType(ast.newSimpleName(returnType)));
    methodDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    methodDeclaration.setConstructor(false);
    final Block body = ast.newBlock();
    methodDeclaration.setBody(body);
    final ReturnStatement statement = ast.newReturnStatement();
    final MethodInvocation expression = ast.newMethodInvocation();
    expression.setName(ast.newSimpleName(returnMethodName));
    // arguments
    final StringLiteral literal = ast.newStringLiteral();
    literal.setLiteralValue(propertyName);
    expression.arguments().add(literal);
    // Class argument
    TypeLiteral classLiteral = ast.newTypeLiteral();
    classLiteral.setType(ast.newSimpleType(ast.newName(returnType)));
    expression.arguments().add(classLiteral);
    //
    statement.setExpression(expression);
    body.statements().add(statement);
    classType.bodyDeclarations().add(methodDeclaration);
    // add annotation
    final MarkerAnnotation generatedAnnotation = ast.newMarkerAnnotation();
    generatedAnnotation.setTypeName(ast.newName(HippoEssentialsGenerated.class.getSimpleName()));
    addHippoGeneratedAnnotation(propertyName, unit, methodDeclaration, ast);
    replaceFile(path, unit, ast);
}