Example usage for org.eclipse.jdt.internal.core CompilationUnitProblemFinder CompilationUnitProblemFinder

List of usage examples for org.eclipse.jdt.internal.core CompilationUnitProblemFinder CompilationUnitProblemFinder

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core CompilationUnitProblemFinder CompilationUnitProblemFinder.

Prototype

protected CompilationUnitProblemFinder(INameEnvironment environment, IErrorHandlingPolicy policy,
        CompilerOptions compilerOptions, ICompilerRequestor requestor, IProblemFactory problemFactory) 

Source Link

Document

Answer a new CompilationUnitVisitor using the given name environment and compiler options.

Usage

From source file:org.eclipse.jdt.internal.core.CompilationUnitProblemFinder.java

License:Open Source License

public static CompilationUnitDeclaration process(CompilationUnit unitElement, SourceElementParser parser,
        WorkingCopyOwner workingCopyOwner, HashMap problems, boolean creatingAST, int reconcileFlags,
        IProgressMonitor monitor) throws JavaModelException {

    JavaProject project = (JavaProject) unitElement.getJavaProject();
    CancelableNameEnvironment environment = null;
    CancelableProblemFactory problemFactory = null;
    CompilationUnitProblemFinder problemFinder = null;
    CompilationUnitDeclaration unit = null;
    try {/*w w  w .  j  a  v  a  2  s .co m*/
        environment = new CancelableNameEnvironment(project, workingCopyOwner, monitor);
        problemFactory = new CancelableProblemFactory(monitor);
        CompilerOptions compilerOptions = getCompilerOptions(project.getOptions(true), creatingAST,
                ((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0));
        boolean ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
        compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
        // GROOVY start
        // options fetched prior to building problem finder then configured based on project
        CompilerUtils.configureOptionsBasedOnNature(compilerOptions, project);
        // GROOVY end
        problemFinder = new CompilationUnitProblemFinder(environment, getHandlingPolicy(), compilerOptions,
                getRequestor(), problemFactory);
        boolean analyzeAndGenerateCode = true;
        if (ignoreMethodBodies) {
            analyzeAndGenerateCode = false;
        }
        try {
            if (parser != null) {
                problemFinder.parser = parser;
                unit = parser.parseCompilationUnit(unitElement, true/*full parse*/, monitor);
                problemFinder.resolve(unit, unitElement, true, // verify methods
                        analyzeAndGenerateCode, // analyze code
                        analyzeAndGenerateCode); // generate code
            } else {
                unit = problemFinder.resolve(unitElement, true, // verify methods
                        analyzeAndGenerateCode, // analyze code
                        analyzeAndGenerateCode); // generate code
            }
        } catch (AbortCompilation e) {
            problemFinder.handleInternalException(e, unit);
        }
        if (unit != null) {
            CompilationResult unitResult = unit.compilationResult;
            CategorizedProblem[] unitProblems = unitResult.getProblems();
            int length = unitProblems == null ? 0 : unitProblems.length;
            if (length > 0) {
                CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
                System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
                problems.put(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, categorizedProblems);
            }
            unitProblems = unitResult.getTasks();
            length = unitProblems == null ? 0 : unitProblems.length;
            if (length > 0) {
                CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
                System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
                problems.put(IJavaModelMarker.TASK_MARKER, categorizedProblems);
            }
            if (NameLookup.VERBOSE) {
                System.out.println(
                        Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " //$NON-NLS-1$
                                + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$
                System.out.println(
                        Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " //$NON-NLS-1$
                                + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$
            }
        }
    } catch (OperationCanceledException e) {
        // catch this exception so as to not enter the catch(RuntimeException e) below
        throw e;
    } catch (RuntimeException e) {
        // avoid breaking other tools due to internal compiler failure (40334)
        String lineDelimiter = unitElement.findRecommendedLineSeparator();
        StringBuffer message = new StringBuffer("Exception occurred during problem detection:"); //$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(
                "----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(unitElement.getSource());
        message.append(lineDelimiter);
        message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$
        Util.log(e, message.toString());
        throw new JavaModelException(e, IJavaModelStatusConstants.COMPILER_FAILURE);
    } finally {
        if (environment != null)
            environment.setMonitor(null); // don't hold a reference to this external object
        if (problemFactory != null)
            problemFactory.monitor = null; // don't hold a reference to this external object
        // NB: unit.cleanUp() is done by caller
        if (problemFinder != null && !creatingAST)
            problemFinder.lookupEnvironment.reset();
    }
    return unit;
}