Example usage for org.eclipse.jdt.internal.compiler.parser RecoveredMethod RecoveredMethod

List of usage examples for org.eclipse.jdt.internal.compiler.parser RecoveredMethod RecoveredMethod

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.parser RecoveredMethod RecoveredMethod.

Prototype

public RecoveredMethod(AbstractMethodDeclaration methodDeclaration, RecoveredElement parent, int bracketBalance,
            Parser parser) 

Source Link

Usage

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

public RecoveredElement buildInitialRecoveryState() {

    /* initialize recovery by retrieving available reduced nodes
     * also rebuild bracket balance/*from   ww  w. j a  va  2s  .c  om*/
     */
    this.lastCheckPoint = 0;
    this.lastErrorEndPositionBeforeRecovery = this.scanner.currentPosition;

    RecoveredElement element = null;
    if (this.referenceContext instanceof CompilationUnitDeclaration) {
        element = new RecoveredUnit(this.compilationUnit, 0, this);

        /* ignore current stack state, since restarting from the beginnning
           since could not trust simple brace count */
        // restart recovery from scratch
        this.compilationUnit.currentPackage = null;
        this.compilationUnit.imports = null;
        this.compilationUnit.types = null;
        this.currentToken = 0;
        this.listLength = 0;
        this.listTypeParameterLength = 0;
        this.endPosition = 0;
        this.endStatementPosition = 0;
        return element;
    } else {
        if (this.referenceContext instanceof AbstractMethodDeclaration) {
            element = new RecoveredMethod((AbstractMethodDeclaration) this.referenceContext, null, 0, this);
            this.lastCheckPoint = ((AbstractMethodDeclaration) this.referenceContext).bodyStart;
            if (this.statementRecoveryActivated) {
                element = element.add(new Block(0), 0);
            }
        } else {
            /* Initializer bodies are parsed in the context of the type declaration, we must thus search it inside */
            if (this.referenceContext instanceof TypeDeclaration) {
                TypeDeclaration type = (TypeDeclaration) this.referenceContext;
                FieldDeclaration[] fieldDeclarations = type.fields;
                int length = fieldDeclarations == null ? 0 : fieldDeclarations.length;
                for (int i = 0; i < length; i++) {
                    FieldDeclaration field = fieldDeclarations[i];
                    if (field != null && field.getKind() == AbstractVariableDeclaration.INITIALIZER
                            && ((Initializer) field).block != null
                            && field.declarationSourceStart <= this.scanner.initialPosition
                            && this.scanner.initialPosition <= field.declarationSourceEnd
                            && this.scanner.eofPosition <= field.declarationSourceEnd + 1) {
                        element = new RecoveredInitializer(field, null, 1, this);
                        this.lastCheckPoint = field.declarationSourceStart;
                        break;
                    }
                }
            }
        }
    }

    if (element == null)
        return element;

    for (int i = 0; i <= this.astPtr; i++) {
        ASTNode node = this.astStack[i];
        if (node instanceof AbstractMethodDeclaration) {
            AbstractMethodDeclaration method = (AbstractMethodDeclaration) node;
            if (method.declarationSourceEnd == 0) {
                element = element.add(method, 0);
                this.lastCheckPoint = method.bodyStart;
            } else {
                element = element.add(method, 0);
                this.lastCheckPoint = method.declarationSourceEnd + 1;
            }
            continue;
        }
        if (node instanceof Initializer) {
            Initializer initializer = (Initializer) node;
            // ignore initializer with no block
            if (initializer.block == null)
                continue;
            if (initializer.declarationSourceEnd == 0) {
                element = element.add(initializer, 1);
                this.lastCheckPoint = initializer.sourceStart;
            } else {
                element = element.add(initializer, 0);
                this.lastCheckPoint = initializer.declarationSourceEnd + 1;
            }
            continue;
        }
        if (node instanceof FieldDeclaration) {
            FieldDeclaration field = (FieldDeclaration) node;
            if (field.declarationSourceEnd == 0) {
                element = element.add(field, 0);
                if (field.initialization == null) {
                    this.lastCheckPoint = field.sourceEnd + 1;
                } else {
                    this.lastCheckPoint = field.initialization.sourceEnd + 1;
                }
            } else {
                element = element.add(field, 0);
                this.lastCheckPoint = field.declarationSourceEnd + 1;
            }
            continue;
        }
        if (node instanceof TypeDeclaration) {
            TypeDeclaration type = (TypeDeclaration) node;
            if ((type.modifiers & ClassFileConstants.AccEnum) != 0) {
                // do not allow enums to be build as recovery types
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=340691
                continue;
            }
            if (type.declarationSourceEnd == 0) {
                element = element.add(type, 0);
                this.lastCheckPoint = type.bodyStart;
            } else {
                element = element.add(type, 0);
                this.lastCheckPoint = type.declarationSourceEnd + 1;
            }
            continue;
        }
        if (node instanceof ImportReference) {
            ImportReference importRef = (ImportReference) node;
            element = element.add(importRef, 0);
            this.lastCheckPoint = importRef.declarationSourceEnd + 1;
        }
        if (this.statementRecoveryActivated) {
            if (node instanceof Block) {
                Block block = (Block) node;
                element = element.add(block, 0);
                this.lastCheckPoint = block.sourceEnd + 1;
            } else if (node instanceof LocalDeclaration) {
                LocalDeclaration statement = (LocalDeclaration) node;
                element = element.add(statement, 0);
                this.lastCheckPoint = statement.sourceEnd + 1;
            } else if (node instanceof Expression) {
                if (node instanceof Assignment || node instanceof PrefixExpression
                        || node instanceof PostfixExpression || node instanceof MessageSend
                        || node instanceof AllocationExpression) {
                    // recover only specific expressions
                    Expression statement = (Expression) node;
                    element = element.add(statement, 0);
                    if (statement.statementEnd != -1) {
                        this.lastCheckPoint = statement.statementEnd + 1;
                    } else {
                        this.lastCheckPoint = statement.sourceEnd + 1;
                    }
                }
            } else if (node instanceof Statement) {
                Statement statement = (Statement) node;
                element = element.add(statement, 0);
                this.lastCheckPoint = statement.sourceEnd + 1;
            }
        }
    }

    if (this.statementRecoveryActivated) {
        if (this.pendingRecoveredType != null
                && this.scanner.startPosition - 1 <= this.pendingRecoveredType.declarationSourceEnd) {
            // Add the pending type to the AST if this type isn't already added in the AST.
            element = element.add(this.pendingRecoveredType, 0);
            this.lastCheckPoint = this.pendingRecoveredType.declarationSourceEnd + 1;
            this.pendingRecoveredType = null;
        }
    }
    return element;
}