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

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

Introduction

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

Prototype

CompilationUnit(AST ast) 

Source Link

Document

Creates a new AST node for a compilation owned by the given AST.

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  www  .  ja va 2s .c o 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;
    }
}