Example usage for org.eclipse.jdt.core.dom TryStatement setFinally

List of usage examples for org.eclipse.jdt.core.dom TryStatement setFinally

Introduction

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

Prototype

public void setFinally(Block block) 

Source Link

Document

Sets or clears the finally block of this try statement.

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.emptycode.EmptyFinallyBlockQuickFix.java

License:Open Source License

/**
 * Removes the finally block. Additionally removes the try if there are no catch blocks while keeping the try block
 * statements./*  w  w  w  . j  a  v a  2s.c  o m*/
 */
@Override
protected boolean apply(final TryStatement node) {
    boolean success = true;
    if (node.catchClauses().isEmpty()) {
        @SuppressWarnings("unchecked")
        final List<Statement> statements = node.getBody().statements();
        if (replace(node, copy(statements))) {
            node.delete();
        } else {
            success = false;
        }
    } else {
        node.setFinally(null);
    }
    return success;
}

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

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

    if (this.binding.get(node.getBody()) != null)
        element.setBody((Block) this.binding.get(node.getBody()));
    if (this.binding.get(node.getFinally()) != null)
        element.setFinally((Block) this.binding.get(node.getFinally()));
    for (Iterator<?> i = node.catchClauses().iterator(); i.hasNext();) {
        CatchClause itElement = (CatchClause) this.binding.get(i.next());
        if (itElement != null)
            element.getCatchClauses().add(itElement);
    }//from  www . ja v a 2s. c o  m
}

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

License:Open Source License

@Nullable
private TryStatement transformCatch(@Nonnull final Catch catchStruct) {
    final BB head = catchStruct.getHead();
    final TryStatement tryStatement = getAst().newTryStatement();
    final List<Statement> statements = tryStatement.getBody().statements();
    assert statements != null;
    transformSequence(catchStruct, head, statements);
    // now add handlers
    for (final E catchE : head.getOuts()) {
        if (!catchE.isCatch()) {
            continue;
        }//from  ww  w  . j av  a 2s  .c  om
        final BB handler = catchE.getEnd();
        final T[] catchTypes = (T[]) catchE.getValue();
        assert catchTypes != null;
        if (!catchStruct.hasHandler(catchTypes, handler)) {
            continue;
        }
        // extract exception name from temporary throwable declaration, don't remove in CFG
        final Statement throwableDeclaration = handler.getStmt(0);
        assert throwableDeclaration instanceof VariableDeclarationStatement;
        final List<VariableDeclarationFragment> throwableDeclarationFragments = ((VariableDeclarationStatement) throwableDeclaration)
                .fragments();
        assert throwableDeclarationFragments.size() == 1;
        final VariableDeclarationFragment throwableDeclarationFragment = throwableDeclarationFragments.get(0);
        final SimpleName exceptionName = throwableDeclarationFragment.getName();
        assert exceptionName != null;

        if (catchE.isFinally()) {
            // finally handler
            tryStatement.setFinally(getAst().newBlock());
            final List<Statement> finallyStatements = tryStatement.getFinally().statements();
            assert finallyStatements != null;
            transformSequence(catchStruct, handler, finallyStatements);
            // remove temporary throwable declaration from finally block, could be nested
            Statement firstStatement = finallyStatements.get(0);
            if (firstStatement instanceof TryStatement) {
                firstStatement = (Statement) ((TryStatement) firstStatement).getBody().statements().get(0);
            }
            if (firstStatement instanceof VariableDeclarationStatement) {
                firstStatement.delete();
            }
            // remove final throws from finally block, should never be nested
            if (!finallyStatements.isEmpty()
                    && finallyStatements.get(finallyStatements.size() - 1) instanceof ThrowStatement) {
                finallyStatements.remove(finallyStatements.size() - 1);
            }
            continue;
        }
        // normal typed catch handler
        final CatchClause catchClause = getAst().newCatchClause();

        final SingleVariableDeclaration singleVariableDeclaration = getAst().newSingleVariableDeclaration();
        singleVariableDeclaration.setName((SimpleName) wrap(exceptionName));
        if (catchTypes.length == 1) {
            final T handlerType = catchTypes[0];
            assert handlerType != null;
            singleVariableDeclaration.setType(newType(handlerType, getM()));
        } else {
            // Multi-Catch
            final UnionType unionType = getAst().newUnionType();
            for (final T t : catchTypes) {
                assert t != null;
                unionType.types().add(newType(t, getM()));
            }
            singleVariableDeclaration.setType(unionType);
        }
        catchClause.setException(singleVariableDeclaration);

        tryStatement.catchClauses().add(catchClause);

        final List<Statement> handlerStatements = catchClause.getBody().statements();
        assert handlerStatements != null;
        transformSequence(catchStruct, handler, handlerStatements);
        // remove temporary throwable declaration from catch
        if (handlerStatements.isEmpty()) {
            log.warn(getM() + ": Missing temporary throwable declaration in handler statements for BB"
                    + handler.getPc() + ":\n" + catchStruct);
        } else {
            handlerStatements.remove(0);
        }
    }
    return tryStatement;
}

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

License:Open Source License

public TryStatement convert(org.eclipse.jdt.internal.compiler.ast.TryStatement statement) {
    final TryStatement tryStatement = new TryStatement(this.ast);
    tryStatement.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
    LocalDeclaration[] localDeclarations = statement.resources;
    int resourcesLength = localDeclarations.length;
    if (resourcesLength > 0) {
        switch (this.ast.apiLevel) {
        case AST.JLS2_INTERNAL:
        case AST.JLS3:
            // convert it to a simple try statement tagged as MALFORMED
            tryStatement.setFlags(tryStatement.getFlags() | ASTNode.MALFORMED);
            break;
        default:/*from  w w w .  j  a va  2 s.c o  m*/
            for (int i = 0; i < resourcesLength; i++) {
                LocalDeclaration localDeclaration = localDeclarations[i];
                VariableDeclarationExpression variableDeclarationExpression = convertToVariableDeclarationExpression(
                        localDeclaration);
                int start = variableDeclarationExpression.getStartPosition();
                int end = localDeclaration.declarationEnd;
                variableDeclarationExpression.setSourceRange(start, end - start + 1);
                tryStatement.resources().add(variableDeclarationExpression);
            }
        }
    }
    tryStatement.setBody(convert(statement.tryBlock));
    org.eclipse.jdt.internal.compiler.ast.Argument[] catchArguments = statement.catchArguments;
    if (catchArguments != null) {
        int catchArgumentsLength = catchArguments.length;
        org.eclipse.jdt.internal.compiler.ast.Block[] catchBlocks = statement.catchBlocks;
        int start = statement.tryBlock.sourceEnd;
        for (int i = 0; i < catchArgumentsLength; i++) {
            CatchClause catchClause = new CatchClause(this.ast);
            int catchClauseSourceStart = retrieveStartingCatchPosition(start, catchArguments[i].sourceStart);
            catchClause.setSourceRange(catchClauseSourceStart,
                    catchBlocks[i].sourceEnd - catchClauseSourceStart + 1);
            catchClause.setBody(convert(catchBlocks[i]));
            catchClause.setException(convert(catchArguments[i]));
            tryStatement.catchClauses().add(catchClause);
            start = catchBlocks[i].sourceEnd;
        }
    }
    if (statement.finallyBlock != null) {
        tryStatement.setFinally(convert(statement.finallyBlock));
    }
    return tryStatement;
}

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.TryStatement node) {
    TryStatement element = (TryStatement) this.binding.get(node);
    initializeNode(element, node);/*from   w ww  . ja  va  2  s  . c om*/

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

    if (this.binding.get(node.getFinally()) != null) {
        element.setFinally((Block) this.binding.get(node.getFinally()));
    }

    for (Iterator<?> i = node.catchClauses().iterator(); i.hasNext();) {
        CatchClause itElement = (CatchClause) this.binding.get(i.next());
        if (itElement != null) {
            element.getCatchClauses().add(itElement);
        }
    }
}