Example usage for org.eclipse.jdt.core.dom CatchClause setSourceRange

List of usage examples for org.eclipse.jdt.core.dom CatchClause setSourceRange

Introduction

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

Prototype

public final void setSourceRange(int startPosition, int length) 

Source Link

Document

Sets the source range of the original source file where the source fragment corresponding to this node was found.

Usage

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  2s  . co  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;
}