Search in sources :

Example 1 with CommentRecorderParser

use of org.eclipse.jdt.internal.core.util.CommentRecorderParser in project spoon by INRIA.

the class JDTBatchCompiler method getUnits.

/**
 * Calls JDT to retrieve the list of compilation unit declarations.
 * Depends on the actual implementation of {@link #getCompilationUnits()}
 */
public CompilationUnitDeclaration[] getUnits() {
    startTime = System.currentTimeMillis();
    INameEnvironment environment = this.jdtCompiler.environment;
    if (environment == null) {
        environment = getLibraryAccess();
    }
    CompilerOptions compilerOptions = new CompilerOptions(this.options);
    compilerOptions.parseLiteralExpressionsAsConstants = false;
    IErrorHandlingPolicy errorHandlingPolicy;
    if (jdtCompiler.getEnvironment().getNoClasspath()) {
        // in no classpath, we should proceed on error,
        // as we will encounter some
        errorHandlingPolicy = new IErrorHandlingPolicy() {

            @Override
            public boolean proceedOnErrors() {
                return true;
            }

            @Override
            public boolean stopOnFirstError() {
                return false;
            }

            // we cannot ignore them, because JDT will continue its process
            // and it led to NPE in several places
            @Override
            public boolean ignoreAllErrors() {
                return false;
            }
        };
    } else {
        // when there is a classpath, we should not have any error
        errorHandlingPolicy = new IErrorHandlingPolicy() {

            @Override
            public boolean proceedOnErrors() {
                return false;
            }

            // we wait for all errors to be gathered before stopping
            @Override
            public boolean stopOnFirstError() {
                return false;
            }

            @Override
            public boolean ignoreAllErrors() {
                return false;
            }
        };
    }
    TreeBuilderCompiler treeBuilderCompiler = new TreeBuilderCompiler(environment, errorHandlingPolicy, compilerOptions, this.jdtCompiler.requestor, getProblemFactory(), this.out, null);
    if (jdtCompiler.getEnvironment().getNoClasspath()) {
        treeBuilderCompiler.lookupEnvironment.mayTolerateMissingType = true;
    }
    // they have to be done all at once
    final CompilationUnitDeclaration[] result = treeBuilderCompiler.buildUnits(getCompilationUnits());
    // now adding the doc
    if (jdtCompiler.getEnvironment().isCommentsEnabled()) {
        // compile comments only if they are needed
        for (int i = 0; i < result.length; i++) {
            CompilationUnitDeclaration unit = result[i];
            CommentRecorderParser parser = new CommentRecorderParser(new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions, new DefaultProblemFactory(Locale.getDefault())), false);
            // reuse the source compilation unit
            ICompilationUnit sourceUnit = unit.compilationResult.compilationUnit;
            final CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
            CompilationUnitDeclaration tmpDeclForComment = parser.dietParse(sourceUnit, compilationResult);
            unit.comments = tmpDeclForComment.comments;
        }
    }
    return result;
}
Also used : IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ProblemReporter(org.eclipse.jdt.internal.compiler.problem.ProblemReporter) CommentRecorderParser(org.eclipse.jdt.internal.core.util.CommentRecorderParser) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult)

Aggregations

CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 IErrorHandlingPolicy (org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)1 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)1 ProblemReporter (org.eclipse.jdt.internal.compiler.problem.ProblemReporter)1 CommentRecorderParser (org.eclipse.jdt.internal.core.util.CommentRecorderParser)1