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:de.ovgu.cide.export.physical.ahead.JakColorChecker.java

License:Open Source License

/**
 * searches for a sequence of statements that belong together and are
 * colored accordingly around the given node
 * /*from   ww w.  j  a  va 2 s  .  c om*/
 * @param node
 * @return
 */
private List<Statement> findStatementsBelongingToHook(ASTNode node) {
    assert node.getParent() instanceof Block;
    Block block = (Block) node.getParent();
    List<Statement> blockStatements = block.statements();

    boolean foundTargetNode = false;
    List<Statement> statementsToExtract = new ArrayList<Statement>();
    for (int stmtIdx = 0; stmtIdx < blockStatements.size(); stmtIdx++) {
        Statement statement = blockStatements.get(stmtIdx);
        boolean shouldExtract = colorManager.getColors(statement).equals(getColors(node));
        if (statement == node)
            foundTargetNode = true;
        if (shouldExtract)
            statementsToExtract.add(statement);
        else {
            // if uncolored statement was found before the target
            // node, reset this is another hook method, otherwise
            // quit, we found our hook sequence
            if (foundTargetNode)
                stmtIdx = blockStatements.size();
            else
                statementsToExtract.clear();
        }
    }
    return statementsToExtract;
}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

static void replaceStatement(Statement target, Statement replacement) {
    ASTNode p = target.getParent();//www .j a va 2  s .com

    if (target instanceof Block && !(replacement instanceof Block)) {
        Block b = replacement.getAST().newBlock();
        b.statements().add(replacement);
        replacement = b;
    }

    StructuralPropertyDescriptor prop = target.getLocationInParent();
    if (prop.isSimpleProperty() || prop.isChildProperty()) {
        p.setStructuralProperty(prop, replacement);
    } else if (prop.isChildListProperty()) {
        assert false;
    }

}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

/**
 * refactoring the first and last statements per method
 * /*from  www .  java 2s . c  o m*/
 * conditions: * first statements do not expose variables used later * last
 * statements to not consume local variables
 */
protected boolean refactorAroundAdvice(MethodDeclaration node) {
    Block body = node.getBody();
    if (body == null)
        return false;
    List<Statement> statements = body.statements();
    List<Statement> refactorFirst = RefactoringUtils.findBeforeStatements(node, colorManager, derivative);
    List<Statement> refactorLast = RefactoringUtils.findAfterStatements(node, colorManager, derivative);

    boolean canRefactor = RefactoringUtils.canRefactorStatementsBeforeAfter(node, refactorFirst, refactorLast,
            colorManager, derivative);

    if (canRefactor) {
        statements.removeAll(refactorFirst);
        statements.removeAll(refactorLast);
        extractAroundMethodRefinement(node, refactorFirst, refactorLast);
        return true;
    }
    return false;
}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

private void findStatementsSequencesInBlockForRefactoring(Block block) {
    List<Statement> statements = block.statements();
    int idx = statements.size() - 1;

    List<Statement> statementsToReplace = new ArrayList<Statement>();
    // grouping statements from back to front and refactoring groups with
    // hooks/*w w  w. j  a  v  a2 s . c om*/
    while (idx >= 0) {
        Statement stmt = statements.get(idx);
        boolean isColoredStmt = derivative.equals(colorManager.getColors(stmt));
        boolean isSubtreeRuleException = false, isNextSubtreeRuleException = false;
        if (isColoredStmt) {
            isSubtreeRuleException = RefactoringUtils.isSubtreeRuleException(stmt, colorManager, derivative);
            if (isSubtreeRuleException && idx > 0) {
                isNextSubtreeRuleException = derivative.equals(colorManager.getColors(statements.get(idx - 1)))
                        && RefactoringUtils.isSubtreeRuleException(statements.get(idx - 1), colorManager,
                                derivative);
                ;
            }
            statementsToReplace.add(0, stmt);
        }

        boolean refactorCollectedStatements = false;
        if (idx == 0)
            refactorCollectedStatements = true;
        // group by colored statements
        if (!isColoredStmt)
            refactorCollectedStatements = true;
        // only one subtree rule exception per block possible
        if (isSubtreeRuleException && isNextSubtreeRuleException)
            refactorCollectedStatements = true;
        if (refactorCollectedStatements && statementsToReplace.size() > 0) {

            refactorStatementSequenceUsingHook(statementsToReplace, block, idx + (isColoredStmt ? 0 : 1));

            statementsToReplace = new ArrayList<Statement>();
        }
        idx--;

    }
    assert statementsToReplace.size() == 0;

}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

private void refactorStatementSequenceUsingHook(List<Statement> statementSequence, Block containingBlock,
        int replacementPosition) {
    Statement exception = RefactoringUtils.findSubtreeRuleException(statementSequence, colorManager,
            derivative);//from   ww  w.  ja v a2s  . c  o  m
    JakHookMethodHelper hook = new JakHookMethodHelper(statementSequence,
            RefactoringUtils.getMethodDeclaration(containingBlock), exception, colorManager);
    containingBlock.statements().removeAll(statementSequence);
    Statement hookCall = hook.getHookCall();
    containingBlock.statements().add(replacementPosition, hookCall);
    mainType.peek().bodyDeclarations().add(hook.getHookDeclaration());
    JakClassRefinement ref = targetCompUnit.getRefinement();
    ref.addRefinementForMethod(RefactoringUtils.getMethodDeclaration(containingBlock), hook.getRefinement());
    // color hook method with the same color as the call (if the
    // call inherits any colors)
    Set<IFeature> hookColors = colorManager.getInheritedColors(hookCall);
    if (hookColors.size() > 0)
        colorManager.setColors(hook.getHookDeclaration(), hookColors);

    ignoredMethodsForAroundAdvice.add(hook.getHookDeclaration());
}

From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java

License:Open Source License

private void appendReturnStatement(Block block) {
    if (returnValues.size() == 1) {
        Formal formal = returnValues.get(0);
        ReturnStatement returnStmt = ast.newReturnStatement();
        SimpleName v = ast.newSimpleName(formal.name);
        returnStmt.setExpression(v);//from   ww w  . ja va2  s  .  c  o m
        LocalVariableHelper.addLocalVariableAccess(v, formal);
        block.statements().add(returnStmt);
    } else if (returnValues.size() > 0) {
        assert false : "unimplemented yet";
    }
}

From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java

License:Open Source License

private void createRefinement() {
    refinement = createHookMethodSkeleton();
    List<Statement> statementList = refinement.getBody().statements();

    for (Statement stmt : statements) {
        Statement copyStmt = copyStatement(stmt);
        if (hasSubtreeRuleException && subtreeRuleExceptionParent == stmt) {
            assert returnValues.size() <= 1;
            Formal formal = null;// w  w w  .  jav  a 2 s  .  c  o m
            if (returnValues.size() == 1)
                formal = returnValues.get(0);
            Statement replacement = JakFeatureRefactorer.createSuperCall(refinement, ast, false, formal);
            /*
             * we place the supercall at the old position where currently
             * the placeholder is located
             */
            if (subtreeRuleExceptionIsBlock) {
                Block b = ast.newBlock();
                b.statements().add(replacement);
                replacement = b;
            }

            StructuralPropertyDescriptor prop = exceptionPlaceholder.getLocationInParent();
            if (prop.isSimpleProperty() || prop.isChildProperty()) {
                copyStmt.setStructuralProperty(prop, replacement);
            } else if (prop.isChildListProperty()) {
                assert false;
            }

        }

        statementList.add(copyStmt);
    }
    if (subtreeRuleException == null)
        statementList.add(JakFeatureRefactorer.createSuperCall(refinement, ast, true, null));
    else
        appendReturnStatement(refinement.getBody());

}

From source file:de.ovgu.cide.export.physical.ahead.MethodObjectHelper.java

License:Open Source License

public static TypeDeclaration moveMethodToMethodObject(MethodDeclaration method,
        RefactoringColorManager colorManager, boolean isStatic) {
    debug_numberOfMessageObjects++;/*from ww  w.  j  a  va2s  .com*/

    AST ast = method.getAST();
    assert method.getParent() instanceof TypeDeclaration;
    TypeDeclaration containingClass = (TypeDeclaration) method.getParent();

    String moName = getMethodObjectName(containingClass, method);
    // System.out.println(moName);
    boolean isSourceMethodStatic = isMethodStatic(method);
    if (isStatic && !isSourceMethodStatic)
        makeThisAccessExplicit(method);

    // create inner class and execute method
    TypeDeclaration methodObjectClass = ast.newTypeDeclaration();
    methodObjectClass.setName(ast.newSimpleName(moName));
    if (isStatic) {
        Modifier staticModifier = ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD);
        methodObjectClass.modifiers().add(staticModifier);

    }

    MethodDeclaration executeMethod = ast.newMethodDeclaration();
    executeMethod.setName(ast.newSimpleName("execute"));
    executeMethod.setReturnType2((Type) ASTNode.copySubtree(ast, method.getReturnType2()));
    executeMethod.thrownExceptions().addAll(ASTNode.copySubtrees(ast, method.thrownExceptions()));

    methodObjectClass.bodyDeclarations().add(executeMethod);
    containingClass.bodyDeclarations().add(methodObjectClass);

    createConstructor(methodObjectClass, method, isStatic && !isSourceMethodStatic);

    // move body to MO
    Block oldMethodBody = method.getBody();
    Block newMethodBody = ast.newBlock();
    method.setBody(newMethodBody);

    executeMethod.setBody(oldMethodBody);

    if (isStatic && !isSourceMethodStatic)
        changeThisAccessToField(oldMethodBody);

    // call method object
    ClassInstanceCreation moConstructorCall = ast.newClassInstanceCreation();
    moConstructorCall.setType(ast.newSimpleType(ast.newSimpleName(moName)));
    addParamters(moConstructorCall, method, isStatic && !isSourceMethodStatic);
    MethodInvocation moCall = ast.newMethodInvocation();
    moCall.setExpression(moConstructorCall);
    moCall.setName(ast.newSimpleName("execute"));
    if (RefactoringUtils.isVoid(method.getReturnType2())) {
        newMethodBody.statements().add(ast.newExpressionStatement(moCall));
    } else {
        ReturnStatement returnStmt = ast.newReturnStatement();
        returnStmt.setExpression(moCall);
        newMethodBody.statements().add(returnStmt);
    }

    // adjustColors
    colorManager.setColors(methodObjectClass, colorManager.getOwnColors(method));

    moveLocalVariableDeclarationsToFields(executeMethod, methodObjectClass, colorManager);

    addMethodObjectAnnotation(methodObjectClass);

    return methodObjectClass;
}

From source file:de.ovgu.cide.export.physical.ahead.MethodObjectHelper.java

License:Open Source License

private static void moveLocalVariableDeclarationsToFields(final MethodDeclaration executeMethod,
        final TypeDeclaration methodObjectClass, final RefactoringColorManager colorManager) {

    final AST ast = executeMethod.getAST();
    final Set<String> localVariableNames = new HashSet<String>();
    executeMethod.accept(new ASTVisitor() {
        @Override/*  w w  w  . j  ava2  s .  co m*/
        public boolean visit(VariableDeclarationExpression node) {
            assert colorManager.getColors(node).equals(colorManager
                    .getColors(executeMethod)) : "Colored VariableDeclarationExpression not handled yet";
            return super.visit(node);
        }

        @Override
        public void endVisit(VariableDeclarationStatement node) {
            assert node.getLocationInParent().isChildListProperty();
            Set<IFeature> colors = colorManager.getColors(node);
            Block replacementBlock = ast.newBlock();
            Iterator<VariableDeclarationFragment> fragmentIterator = node.fragments().iterator();
            while (fragmentIterator.hasNext()) {
                VariableDeclarationFragment fragment = fragmentIterator.next();
                checkDuplicate(fragment);
                addField(methodObjectClass, node.getType(), fragment.getName().getIdentifier(), colors);
                Expression init = fragment.getInitializer();
                if (init == null)
                    fragmentIterator.remove();
                else {
                    fragment.setInitializer(null);
                    Assignment assignment = ast.newAssignment();
                    assignment.setLeftHandSide(ast.newSimpleName(fragment.getName().getIdentifier()));
                    assignment.setRightHandSide(init);
                    ExpressionStatement assignExpr = ast.newExpressionStatement(assignment);
                    replacementBlock.statements().add(assignExpr);
                    colorManager.addColors(assignExpr, colorManager.getOwnColors(fragment));
                    colorManager.addColors(assignExpr, colorManager.getOwnColors(node));
                }
            }
            Statement replacement = replacementBlock;
            if (replacementBlock.statements().size() == 1) {
                replacement = (Statement) replacementBlock.statements().get(0);
                replacementBlock.statements().clear();
            }
            RefactoringUtils.replaceASTNode(node, replacement);

            /*
             * remove reference from LocalVariableHelper, because this now
             * no longer references a local variable, but a field.
             */
        }

        private void checkDuplicate(VariableDeclarationFragment fragment) {
            String variableName = fragment.getName().getIdentifier();
            assert !localVariableNames.contains(variableName) : "Duplicate local variable name not supported";
            localVariableNames.add(variableName);
        }

        @Override
        public boolean visit(SimpleName node) {
            return visitName(node);
        }

        @Override
        public boolean visit(QualifiedName node) {
            return visitName(node);
        }

        private boolean visitName(Name name) {
            LocalVariableHelper.removeName(name);
            return true;
        }
    });

}

From source file:de.ovgu.cide.export.physical.ahead.ReturnExtractionHelper.java

License:Open Source License

/**
 * surround method body with try catch block.
 * /*from w w  w . j  a va 2  s.  co m*/
 * if the last statement in the body is not a return statement (can happen
 * e.g., when the last statement is a hook method), then also a dummy return
 * statement is introduced that should never occur though.
 * 
 * @param method
 */
private void surroundBodyWithTryCatch(MethodDeclaration method) {
    AST ast = method.getAST();
    Block oldBody = method.getBody();
    boolean lastStatementIsReturn = oldBody.statements()
            .get(oldBody.statements().size() - 1) instanceof ReturnStatement;

    Block newBody = ast.newBlock();
    method.setBody(newBody);

    TryStatement tryStatement = ast.newTryStatement();
    newBody.statements().add(tryStatement);
    tryStatement.setBody(oldBody);

    if (!lastStatementIsReturn && !RefactoringUtils.isVoid(method.getReturnType2())) {
        addDummyReturnStatementHack(method, oldBody);
    }

    CatchClause catchClause = ast.newCatchClause();
    SingleVariableDeclaration exceptionDeclaration = ast.newSingleVariableDeclaration();
    exceptionDeclaration.setName(ast.newSimpleName("r"));
    exceptionDeclaration.setType(createExceptionType(method));

    catchClause.setException(exceptionDeclaration);
    catchClause.setBody(ast.newBlock());
    ReturnStatement returnStatement = ast.newReturnStatement();
    FieldAccess fieldAccess = ast.newFieldAccess();
    fieldAccess.setName(ast.newSimpleName("value"));
    fieldAccess.setExpression(ast.newSimpleName("r"));
    if (RefactoringUtils.isVoid(method.getReturnType2())) {
    } else if (method.getReturnType2().isPrimitiveType()) {
        returnStatement.setExpression(fieldAccess);
    } else {
        CastExpression cast = ast.newCastExpression();
        cast.setExpression(fieldAccess);
        cast.setType((Type) ASTNode.copySubtree(ast, method.getReturnType2()));
        returnStatement.setExpression(cast);
    }
    catchClause.getBody().statements().add(returnStatement);

    tryStatement.catchClauses().add(catchClause);
}