Example usage for org.eclipse.jdt.internal.compiler.ast Initializer parseStatements

List of usage examples for org.eclipse.jdt.internal.compiler.ast Initializer parseStatements

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast Initializer parseStatements.

Prototype

public void parseStatements(Parser parser, TypeDeclaration typeDeclaration, CompilationUnitDeclaration unit) 

Source Link

Usage

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

License:Open Source License

public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length,
        CompilationUnitDeclaration unit) {
    boolean oldDiet = this.diet;
    /* automaton initialization */
    initialize();//from ww w .j a va2 s  .  c o  m
    goForClassBodyDeclarations();
    /* scanner initialization */
    this.scanner.setSource(source);
    this.scanner.resetTo(offset, offset + length - 1);
    if (this.javadocParser != null && this.javadocParser.checkDocComment) {
        this.javadocParser.scanner.setSource(source);
        this.javadocParser.scanner.resetTo(offset, offset + length - 1);
    }

    /* type declaration should be parsed as member type declaration */
    this.nestedType = 1;

    /* unit creation */
    TypeDeclaration referenceContextTypeDeclaration = new TypeDeclaration(unit.compilationResult);
    referenceContextTypeDeclaration.name = Util.EMPTY_STRING.toCharArray();
    referenceContextTypeDeclaration.fields = new FieldDeclaration[0];
    this.compilationUnit = unit;
    unit.types = new TypeDeclaration[1];
    unit.types[0] = referenceContextTypeDeclaration;
    this.referenceContext = unit;

    /* run automaton */
    try {
        this.diet = true;
        parse();
    } catch (AbortCompilation ex) {
        this.lastAct = ERROR_ACTION;
    } finally {
        this.diet = oldDiet;
    }

    ASTNode[] result = null;
    if (this.lastAct == ERROR_ACTION) {
        if (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery) {
            return null;
        }
        // collect all body declaration inside the compilation unit except the default constructor
        final List bodyDeclarations = new ArrayList();
        ASTVisitor visitor = new ASTVisitor() {
            public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
                if (!methodDeclaration.isDefaultConstructor()) {
                    bodyDeclarations.add(methodDeclaration);
                }
                return false;
            }

            public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
                bodyDeclarations.add(fieldDeclaration);
                return false;
            }

            public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
                bodyDeclarations.add(memberTypeDeclaration);
                return false;
            }
        };
        unit.ignoreFurtherInvestigation = false;
        unit.traverse(visitor, unit.scope);
        unit.ignoreFurtherInvestigation = true;
        result = (ASTNode[]) bodyDeclarations.toArray(new ASTNode[bodyDeclarations.size()]);
    } else {
        int astLength;
        if (this.astLengthPtr > -1 && (astLength = this.astLengthStack[this.astLengthPtr--]) != 0) {
            result = new ASTNode[astLength];
            this.astPtr -= astLength;
            System.arraycopy(this.astStack, this.astPtr + 1, result, 0, astLength);
        } else {
            // empty class body declaration (like ';' see https://bugs.eclipse.org/bugs/show_bug.cgi?id=280079).
            result = new ASTNode[0];
        }
    }
    boolean containsInitializers = false;
    TypeDeclaration typeDeclaration = null;
    for (int i = 0, max = result.length; i < max; i++) {
        // parse each class body declaration
        ASTNode node = result[i];
        if (node instanceof TypeDeclaration) {
            ((TypeDeclaration) node).parseMethods(this, unit);
        } else if (node instanceof AbstractMethodDeclaration) {
            ((AbstractMethodDeclaration) node).parseStatements(this, unit);
        } else if (node instanceof FieldDeclaration) {
            FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
            switch (fieldDeclaration.getKind()) {
            case AbstractVariableDeclaration.INITIALIZER:
                containsInitializers = true;
                if (typeDeclaration == null) {
                    typeDeclaration = referenceContextTypeDeclaration;
                }
                if (typeDeclaration.fields == null) {
                    typeDeclaration.fields = new FieldDeclaration[1];
                    typeDeclaration.fields[0] = fieldDeclaration;
                } else {
                    int length2 = typeDeclaration.fields.length;
                    FieldDeclaration[] temp = new FieldDeclaration[length2 + 1];
                    System.arraycopy(typeDeclaration.fields, 0, temp, 0, length2);
                    temp[length2] = fieldDeclaration;
                    typeDeclaration.fields = temp;
                }
                break;
            }
        }
        if (((node.bits & ASTNode.HasSyntaxErrors) != 0)
                && (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery)) {
            return null;
        }
    }
    if (containsInitializers) {
        FieldDeclaration[] fieldDeclarations = typeDeclaration.fields;
        for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
            Initializer initializer = (Initializer) fieldDeclarations[i];
            initializer.parseStatements(this, typeDeclaration, unit);
            if (((initializer.bits & ASTNode.HasSyntaxErrors) != 0)
                    && (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery)) {
                return null;
            }
        }
    }
    return result;
}