Package org.eclipse.jdt.core.dom

Examples of org.eclipse.jdt.core.dom.Block


        // Add a call to the restore method in the enclosing anonymous class.
        MethodInvocation invocation = ast.newMethodInvocation();
        invocation.setName(ast.newSimpleName(_getCommitMethodName(true)));
        invocation.arguments().add(ast.newSimpleName("timestamp"));

        Block body = ast.newBlock();
        body.statements().add(ast.newExpressionStatement(invocation));
        commit.setBody(body);

        commit.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        commit.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
        classDeclaration.bodyDeclarations().add(commit);

        // Add a restore method.
        MethodDeclaration restore = ast.newMethodDeclaration();
        restore.setName(ast.newSimpleName(_getRestoreMethodName(false)));

        // Add two parameters.
        timestamp = ast.newSingleVariableDeclaration();
        timestamp.setType(ast.newPrimitiveType(PrimitiveType.LONG));
        timestamp.setName(ast.newSimpleName("timestamp"));
        restore.parameters().add(timestamp);

        SingleVariableDeclaration trim = ast.newSingleVariableDeclaration();
        trim.setType(ast.newPrimitiveType(PrimitiveType.BOOLEAN));
        trim.setName(ast.newSimpleName("trim"));
        restore.parameters().add(trim);

        // Add a call to the restore method in the enclosing anonymous class.
        invocation = ast.newMethodInvocation();
        invocation.setName(ast.newSimpleName(_getRestoreMethodName(true)));
        invocation.arguments().add(ast.newSimpleName("timestamp"));
        invocation.arguments().add(ast.newSimpleName("trim"));

        body = ast.newBlock();
        body.statements().add(ast.newExpressionStatement(invocation));
        restore.setBody(body);

        restore.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        restore.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
        classDeclaration.bodyDeclarations().add(restore);

        // Add a get checkpoint method.
        MethodDeclaration getCheckpoint = ast.newMethodDeclaration();
        String checkpointType = getClassName(Checkpoint.class, state, root);
        getCheckpoint.setName(ast
                .newSimpleName(_getGetCheckpointMethodName(false)));
        getCheckpoint.setReturnType2(createType(ast, checkpointType));
        invocation = ast.newMethodInvocation();
        invocation
                .setName(ast.newSimpleName(_getGetCheckpointMethodName(true)));
        body = ast.newBlock();

        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(invocation);
        body.statements().add(returnStatement);
        getCheckpoint.setBody(body);

        getCheckpoint.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        getCheckpoint.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
        classDeclaration.bodyDeclarations().add(getCheckpoint);

        // Add a set checkpoint method.
        MethodDeclaration setCheckpoint = ast.newMethodDeclaration();
        setCheckpoint.setName(ast
                .newSimpleName(_getSetCheckpointMethodName(false)));
        setCheckpoint.setReturnType2(createType(ast, getClassName(Object.class,
                state, root)));

        // Add a single checkpoint parameter.
        SingleVariableDeclaration checkpoint = ast
                .newSingleVariableDeclaration();
        checkpoint.setType(createType(ast, checkpointType));
        checkpoint.setName(ast.newSimpleName("checkpoint"));
        setCheckpoint.parameters().add(checkpoint);

        // Add a call to the setcheckpoint method in the enclosing anonymous
        // class.
        invocation = ast.newMethodInvocation();
        invocation
                .setName(ast.newSimpleName(_getSetCheckpointMethodName(true)));
        invocation.arguments().add(ast.newSimpleName("checkpoint"));

        // Return this object.
        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ast.newThisExpression());

        body = ast.newBlock();
        body.statements().add(ast.newExpressionStatement(invocation));
        body.statements().add(returnStatement);
        setCheckpoint.setBody(body);

        setCheckpoint.modifiers().add(
                ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        setCheckpoint.modifiers().add(
View Full Code Here


        method.parameters().add(trim);

        // Return type default to "void".
        if (!isInterface) {
            // The method body.
            Block body = ast.newBlock();
            method.setBody(body);

            // Create a restore statement for each managed field.
            Iterator namesIter = fieldNames.iterator();
            Iterator typesIter = fieldTypes.iterator();

            while (namesIter.hasNext()) {
                String fieldName = (String) namesIter.next();
                Type fieldType = (Type) typesIter.next();

                MethodInvocation restoreMethodCall = ast.newMethodInvocation();
                restoreMethodCall.setExpression(ast
                        .newSimpleName(_getRecordName(fieldName)));

                // Set the restore method name.
                restoreMethodCall.arguments().add(ast.newSimpleName(fieldName));
                restoreMethodCall.setName(ast.newSimpleName("restore"));

                // Add two arguments to the restore method call.
                restoreMethodCall.arguments().add(
                        ast.newSimpleName("timestamp"));
                restoreMethodCall.arguments().add(ast.newSimpleName("trim"));

                boolean isFinal = false;

                try {
                    Field field = currentClass.getDeclaredField(fieldName);

                    if (java.lang.reflect.Modifier
                            .isFinal(field.getModifiers())) {
                        isFinal = true;
                    }
                } catch (NoSuchFieldException e) {
                }

                if (isFinal) {
                    if ((_getAccessedField(currentClass.getName(), fieldName) != null)
                            || !Type.isPrimitive(Type.getElementType(fieldType
                                    .getName()))) {
                        body.statements().add(
                                ast.newExpressionStatement(restoreMethodCall));
                    }
                } else {
                    Expression rightHandSide;

                    if (fieldType.isPrimitive()) {
                        rightHandSide = restoreMethodCall;
                    } else {
                        CastExpression castExpression = ast.newCastExpression();
                        String typeName = getClassName(fieldType.getName(),
                                state, root);
                        castExpression.setType(createType(ast, typeName));
                        castExpression.setExpression(restoreMethodCall);
                        rightHandSide = castExpression;
                    }

                    Assignment assignment = ast.newAssignment();
                    assignment.setLeftHandSide(ast.newSimpleName(fieldName));
                    assignment.setRightHandSide(rightHandSide);
                    body.statements().add(
                            ast.newExpressionStatement(assignment));
                }
            }

            // Add a call to the restore method in the superclass, if necessary.
            SuperMethodInvocation superRestore = ast.newSuperMethodInvocation();
            superRestore.setName(ast
                    .newSimpleName(_getRestoreMethodName(false)));
            superRestore.arguments().add(ast.newSimpleName("timestamp"));
            superRestore.arguments().add(ast.newSimpleName("trim"));

            Statement superRestoreStatement = ast
                    .newExpressionStatement(superRestore);

            if ((parent != null)
                    && (state.getCrossAnalyzedTypes()
                            .contains(parent.getName()) || hasMethod(parent,
                            methodName,
                            new Class[] { int.class, boolean.class }))) {
                body.statements().add(superRestoreStatement);
            } else {
                // Restore the previous checkpoint, if necessary.
                IfStatement restoreCheckpoint = ast.newIfStatement();

                InfixExpression timestampTester = ast.newInfixExpression();
                timestampTester.setLeftOperand(ast.newSimpleName("timestamp"));
                timestampTester
                        .setOperator(InfixExpression.Operator.LESS_EQUALS);

                MethodInvocation topTimestamp = ast.newMethodInvocation();
                topTimestamp.setExpression(ast
                        .newSimpleName(CHECKPOINT_RECORD_NAME));
                topTimestamp.setName(ast.newSimpleName("getTopTimestamp"));
                timestampTester.setRightOperand(topTimestamp);
                restoreCheckpoint.setExpression(timestampTester);

                Block restoreBlock = ast.newBlock();
                restoreCheckpoint.setThenStatement(restoreBlock);

                // Assign the old checkpoint.
                Assignment assignCheckpoint = ast.newAssignment();
                assignCheckpoint.setLeftHandSide(ast
                        .newSimpleName(CHECKPOINT_NAME));

                MethodInvocation restoreCheckpointInvocation = ast
                        .newMethodInvocation();
                restoreCheckpointInvocation.setExpression(ast
                        .newSimpleName(CHECKPOINT_RECORD_NAME));
                restoreCheckpointInvocation.setName(ast
                        .newSimpleName("restore"));
                restoreCheckpointInvocation.arguments().add(
                        ast.newSimpleName(CHECKPOINT_NAME));
                restoreCheckpointInvocation.arguments().add(
                        _createRollbackableObject(ast, isAnonymous));
                restoreCheckpointInvocation.arguments().add(
                        ast.newSimpleName("timestamp"));
                restoreCheckpointInvocation.arguments().add(
                        ast.newSimpleName("trim"));
                assignCheckpoint.setRightHandSide(restoreCheckpointInvocation);
                restoreBlock.statements().add(
                        ast.newExpressionStatement(assignCheckpoint));

                // Pop the old states.
                MethodInvocation popStates = ast.newMethodInvocation();
                String recordType = getClassName(FieldRecord.class, state, root);
                popStates.setExpression(createName(ast, recordType));
                popStates.setName(ast.newSimpleName("popState"));
                popStates.arguments().add(ast.newSimpleName(RECORDS_NAME));
                restoreBlock.statements().add(
                        ast.newExpressionStatement(popStates));

                // Recall the restore method.
                MethodInvocation recursion = ast.newMethodInvocation();
                recursion.setName(ast.newSimpleName(methodName));
                recursion.arguments().add(ast.newSimpleName("timestamp"));
                recursion.arguments().add(ast.newSimpleName("trim"));
                restoreBlock.statements().add(
                        ast.newExpressionStatement(recursion));

                body.statements().add(restoreCheckpoint);

                if (parent != null) {
View Full Code Here

        test.setRightOperand(condition2);

        IfStatement ifStatement = ast.newIfStatement();
        ifStatement.setExpression(test);

        Block thenBranch = ast.newBlock();
        ifStatement.setThenStatement(thenBranch);

        MethodInvocation setCheckpoint = ast.newMethodInvocation();
        setCheckpoint.setExpression(ast.newSimpleName("newValue"));
        setCheckpoint.setName(ast.newSimpleName(SET_CHECKPOINT_NAME));
        setCheckpoint.arguments().add(ast.newSimpleName(CHECKPOINT_NAME));
        thenBranch.statements().add(ast.newExpressionStatement(setCheckpoint));

        return ifStatement;
    }
View Full Code Here

            testExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
            testExpression.setRightOperand(ast.newSimpleName("checkpoint"));
            test.setExpression(testExpression);

            // The "then" branch of the test.
            Block thenBranch = ast.newBlock();
            test.setThenStatement(thenBranch);

            Block body = ast.newBlock();
            body.statements().add(test);
            method.setBody(body);

            // Backup the old checkpoint.
            VariableDeclarationFragment fragment = ast
                    .newVariableDeclarationFragment();
            fragment.setName(ast.newSimpleName("oldCheckpoint"));
            fragment.setInitializer(ast.newSimpleName(CHECKPOINT_NAME));

            VariableDeclarationStatement tempDeclaration = ast
                    .newVariableDeclarationStatement(fragment);
            tempDeclaration.setType(createType(ast, checkpointType));
            thenBranch.statements().add(tempDeclaration);

            // Record the old checkpoint if the new checkpoint is not null.
            // If it is null, it is impossible to roll back to the previous
            // checkpoint.
            IfStatement testNewCheckpoint = ast.newIfStatement();
            InfixExpression testNull = ast.newInfixExpression();
            testNull.setLeftOperand(ast.newSimpleName("checkpoint"));
            testNull.setOperator(InfixExpression.Operator.NOT_EQUALS);
            testNull.setRightOperand(ast.newNullLiteral());
            testNewCheckpoint.setExpression(testNull);

            Block testNewCheckpointBody = ast.newBlock();
            testNewCheckpoint.setThenStatement(testNewCheckpointBody);

            MethodInvocation record = ast.newMethodInvocation();
            record.setExpression(ast.newSimpleName(CHECKPOINT_RECORD_NAME));
            record.setName(ast.newSimpleName("add"));
            record.arguments().add(ast.newSimpleName(CHECKPOINT_NAME));

            MethodInvocation getTimestamp = ast.newMethodInvocation();
            getTimestamp.setExpression(ast.newSimpleName("checkpoint"));
            getTimestamp.setName(ast.newSimpleName("getTimestamp"));
            record.arguments().add(getTimestamp);
            testNewCheckpointBody.statements().add(
                    ast.newExpressionStatement(record));

            MethodInvocation pushStates = ast.newMethodInvocation();
            String recordType = getClassName(FieldRecord.class, state, root);
            pushStates.setExpression(createName(ast, recordType));
            pushStates.setName(ast.newSimpleName("pushState"));
            pushStates.arguments().add(ast.newSimpleName(RECORDS_NAME));
            testNewCheckpointBody.statements().add(
                    ast.newExpressionStatement(pushStates));
            thenBranch.statements().add(testNewCheckpoint);

            // Assign the new checkpoint.
            Assignment assignment = ast.newAssignment();
View Full Code Here

        }

        if (isAnonymous && !ignore) {
            // Create a simple initializer.
            Initializer initializer = ast.newInitializer();
            Block body = ast.newBlock();
            initializer.setBody(body);

            MethodInvocation addInvocation = ast.newMethodInvocation();
            addInvocation.setExpression(ast.newSimpleName(CHECKPOINT_NAME));
            addInvocation.setName(ast.newSimpleName("addObject"));

            ClassInstanceCreation proxy = ast.newClassInstanceCreation();
            proxy
                    .setType(ast.newSimpleType(ast
                            .newSimpleName(_getProxyName())));
            addInvocation.arguments().add(proxy);
            body.statements().add(ast.newExpressionStatement(addInvocation));
            bodyDeclarations.add(initializer);

            if (declarationRecord != null) {
                declarationRecord._addExtendedDeclaration(initializer);
            }
View Full Code Here

            if (blockList != null) {
                Iterator nodesIter = blockList.iterator();

                while (nodesIter.hasNext()) {
                    Block body = (Block) nodesIter.next();
                    AST ast = body.getAST();
                    Statement invocation = _createSetCheckpointInvocation(ast);
                    List<Statement> statements = body.statements();
                    statements.add(statements.size() - 2, invocation);
                    nodesIter.remove();
                }
            }
View Full Code Here

      assert source instanceof MethodMetric : "The calculation is set on method";
      MethodDeclaration astNode = (MethodDeclaration) source.getASTNode();
      if (astNode == null) {
        return;
      }
      Block body = astNode.getBody();
      if (body == null) {
        return;
      }
      String sourceCode = null;
      try {
View Full Code Here

      MethodDeclaration astNode = (MethodDeclaration) source.getASTNode();

      if (astNode == null) {
        return;
      } else {
        Block body = astNode.getBody();
        if (body == null) {
          return;
        } else {
          LevelCounter lc = new LevelCounter();
          astNode.accept(lc);
View Full Code Here

         
          try {
            if (invokedMethod.getElementName().equals(fromMethod.getElementName())
              && invokedMethod.getSignature().equals(fromMethod.getSignature())){

              Block newBlock = parseCode(code);
             
              ASTNode target = node.getParent();
             
              if (target instanceof VariableDeclarationFragment) {
                target = (Statement) target.getParent();
              }
             
              Block block = getBlock(target);
             
              int index = 0;
             
              for (int i = 0; i < block.statements().size(); i++) {
                if (block.statements().get(i).equals(target)) {
                  index = i;
                  break;
                }
              }
             
              List statements = ASTNode.copySubtrees(node.getAST(), newBlock.statements());
             
              for (int i = statements.size() - 1; i >= 0; i--) {
                block.statements().add(index, statements.get(i));
              }
             
              int expressionIndex = index + statements.size() - 1;
              boolean assignmentAdded = false;
             
              if (node.getParent() instanceof VariableDeclarationFragment) {
                Statement expressionStatement = (Statement) block.statements().get(expressionIndex);
               
                if (expressionStatement instanceof ExpressionStatement) {
                  VariableDeclarationFragment vdf = (VariableDeclarationFragment) node.getParent();
                  Expression expression = (Expression) ASTNode.copySubtree(node.getAST(), ((ExpressionStatement) expressionStatement).getExpression());
                  vdf.setInitializer(expression);
                  block.statements().remove(expressionIndex);
                  assignmentAdded = true;
                }
              }
             
              if (! assignmentAdded) {
                block.statements().remove(expressionIndex + 1);
              }
            }
          } catch (JavaModelException e) {
            e.printStackTrace();
          }

        return true;
      }

      @SuppressWarnings("unchecked")
      private Block getBlock(ASTNode target) {
        ASTNode parent = target.getParent();
        if (parent instanceof Block) {
          return (Block) parent;
        }
       
        Block newBlock = target.getAST().newBlock();
        newBlock.statements().add(ASTNode.copySubtree(target.getAST(), target));
        Field[] declaredFields = parent.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
          if (!(field.getType().isAssignableFrom(Statement.class))) {
            continue;
          }
View Full Code Here

    parser.setKind(ASTParser.K_STATEMENTS);

    parser.setSource(source);
    parser.setResolveBindings(true);

    Block val = (Block) parser.createAST(null);
    return val;
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.dom.Block

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.