Example usage for org.eclipse.jdt.core.dom CompilationUnit getFlags

List of usage examples for org.eclipse.jdt.core.dom CompilationUnit getFlags

Introduction

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

Prototype

public final int getFlags() 

Source Link

Document

Returns the flags associated with this node.

Usage

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

License:Open Source License

public CompilationUnit convert(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration unit,
        char[] source) {
    try {/*from w w  w. j  a v a  2  s . co  m*/
        if (unit.compilationResult.recoveryScannerData != null) {
            RecoveryScanner recoveryScanner = new RecoveryScanner(this.scanner,
                    unit.compilationResult.recoveryScannerData.removeUnused());
            this.scanner = recoveryScanner;
            this.docParser.scanner = this.scanner;
        }
        this.compilationUnitSource = source;
        this.compilationUnitSourceLength = source.length;
        this.scanner.setSource(source, unit.compilationResult);
        // GROOVY start
        /* old {
        CompilationUnit compilationUnit = new CompilationUnit(this.ast);
         } new */
        CompilationUnit compilationUnit = unit.getSpecialDomCompilationUnit(this.ast);
        if (compilationUnit == null) {
            compilationUnit = new CompilationUnit(this.ast);
        }
        // GROOVY end
        compilationUnit.setStatementsRecoveryData(unit.compilationResult.recoveryScannerData);

        // Parse comments
        int[][] comments = unit.comments;
        if (comments != null) {
            buildCommentsTable(compilationUnit, comments);
        }

        // handle the package declaration immediately
        // There is no node corresponding to the package declaration
        if (this.resolveBindings) {
            recordNodes(compilationUnit, unit);
        }
        if (unit.currentPackage != null) {
            PackageDeclaration packageDeclaration = convertPackage(unit);
            compilationUnit.setPackage(packageDeclaration);
        }
        org.eclipse.jdt.internal.compiler.ast.ImportReference[] imports = unit.imports;
        if (imports != null) {
            int importLength = imports.length;
            for (int i = 0; i < importLength; i++) {
                compilationUnit.imports().add(convertImport(imports[i]));
            }
        }

        org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = unit.types;
        if (types != null) {
            int typesLength = types.length;
            for (int i = 0; i < typesLength; i++) {
                org.eclipse.jdt.internal.compiler.ast.TypeDeclaration declaration = types[i];
                if (CharOperation.equals(declaration.name, TypeConstants.PACKAGE_INFO_NAME)) {
                    continue;
                }
                ASTNode type = convert(declaration);
                if (type == null) {
                    compilationUnit.setFlags(compilationUnit.getFlags() | ASTNode.MALFORMED);
                } else {
                    compilationUnit.types().add(type);
                }
            }
        }
        compilationUnit.setSourceRange(unit.sourceStart, unit.sourceEnd - unit.sourceStart + 1);

        int problemLength = unit.compilationResult.problemCount;
        if (problemLength != 0) {
            CategorizedProblem[] resizedProblems = null;
            final CategorizedProblem[] problems = unit.compilationResult.getProblems();
            final int realProblemLength = problems.length;
            if (realProblemLength == problemLength) {
                resizedProblems = problems;
            } else {
                System.arraycopy(problems, 0, (resizedProblems = new CategorizedProblem[realProblemLength]), 0,
                        realProblemLength);
            }
            ASTSyntaxErrorPropagator syntaxErrorPropagator = new ASTSyntaxErrorPropagator(resizedProblems);
            compilationUnit.accept(syntaxErrorPropagator);
            ASTRecoveryPropagator recoveryPropagator = new ASTRecoveryPropagator(resizedProblems,
                    unit.compilationResult.recoveryScannerData);
            compilationUnit.accept(recoveryPropagator);
            compilationUnit.setProblems(resizedProblems);
        }
        if (this.resolveBindings) {
            lookupForScopes();
        }
        compilationUnit.initCommentMapper(this.scanner);
        return compilationUnit;
    } catch (IllegalArgumentException e) {
        StringBuffer message = new StringBuffer("Exception occurred during compilation unit conversion:"); //$NON-NLS-1$
        String lineDelimiter = Util.findLineSeparator(source);
        if (lineDelimiter == null)
            lineDelimiter = System.getProperty("line.separator");//$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(
                "----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(source);
        message.append(lineDelimiter);
        message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$
        Util.log(e, message.toString());
        throw e;
    }
}

From source file:ptolemy.backtrack.eclipse.ast.ASTBuilder.java

License:Open Source License

/** Parse the Java source code given in the source buffer, and
 *  return the root of the AST.//  w  ww.  j  ava2s . c  om
 *
 *  @param source The <tt>char</tt> array that contains the
 *   source code in a single Java source file.
 *  @return The root of the AST.
 *  @exception ASTMalformedException If the Java source file
 *   does not conform to the supported Java grammar.
 */
public static CompilationUnit parse(char[] source) throws ASTMalformedException {
    ASTParser parser = ASTParser.newParser(DEFAULT_LANGUAGE_SPECIFICATION);
    parser.setSource(source);

    CompilationUnit ast = (CompilationUnit) parser.createAST(null);

    if ((ast.getFlags() & ASTNode.MALFORMED) != 0) {
        throw new ASTMalformedException();
    }

    return ast;
}