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

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

Introduction

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

Prototype

public CancelableProblemFactory(IProgressMonitor monitor) 

Source Link

Usage

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

public static CompilationUnitDeclaration processAJ( // AspectJ Change
        CompilationUnit unitElement, // AspectJ Change
        CommentRecorderParser parser, // AspectJ Change
        WorkingCopyOwner workingCopyOwner, HashMap<String, CategorizedProblem[]> problems, boolean creatingAST,
        int reconcileFlags, IProgressMonitor monitor) throws JavaModelException {

    boolean isJavaFileInAJEditor = (reconcileFlags & JAVA_FILE_IN_AJ_EDITOR) != 0;

    JavaProject project = (JavaProject) unitElement.getJavaProject();
    ITDAwareNameEnvironment environment = null;
    CancelableProblemFactory problemFactory = null;
    AJCompilationUnitProblemFinder problemFinder = null; // AspectJ Change
    try {//from   w w w .  ja v a  2  s .c  o m

        // AspectJ Change begin
        // use an ITDAware environment to ensure that ITDs are included for source types
        environment = new ITDAwareNameEnvironment(project, workingCopyOwner, monitor);
        // AspectJ Change end

        problemFactory = new CancelableProblemFactory(monitor);
        problemFinder = new AJCompilationUnitProblemFinder( // AspectJ Change
                environment, getHandlingPolicy(),
                getCompilerOptions(project.getOptions(true), creatingAST,
                        ((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0)),
                getRequestor(), problemFactory, unitElement);
        CompilationUnitDeclaration unit = null;

        // AspectJ Change begin 
        // the parser should be a SourceElementParser or AJSourceElementParser2.
        // this ensures that a diet parse can be done, while at the same time
        // all declarations be reported
        if (parser != null) {
            problemFinder.parser = parser;
        }
        try {
            if (problemFinder.parser instanceof SourceElementParser) {
                unit = ((SourceElementParser) problemFinder.parser).parseCompilationUnit(unitElement,
                        true/* full parse */, monitor);
                problemFinder.resolve(unit, unitElement, true, // verify methods
                        true, // analyze code
                        true); // generate code
            } else if (problemFinder.parser instanceof AJSourceElementParser2) {
                unit = ((AJSourceElementParser2) problemFinder.parser).parseCompilationUnit(unitElement,
                        true/* full parse */, monitor);
                problemFinder.resolve(unit, unitElement, true, // verify methods
                        true, // analyze code
                        true); // generate code
            } else {
                unit = problemFinder.resolve(unitElement, true, // verify methods
                        true, // analyze code
                        true); // generate code
            }

        } catch (AbortCompilation e) {
            problemFinder.handleInternalException(e, unit);
        }

        // revert the compilation units that have ITDs in them
        ((ITDAwareLookupEnvironment) problemFinder.lookupEnvironment).revertCompilationUnits();
        // AspectJ Change end

        CompilationResult unitResult = unit.compilationResult;
        CategorizedProblem[] unitProblems = unitResult.getProblems();
        int length = unitProblems == null ? 0 : unitProblems.length;
        if (length > 0) {
            // AspectJ Change begin
            // filter out spurious problems
            CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
            System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
            categorizedProblems = removeAJNonProblems(categorizedProblems, unitElement, isJavaFileInAJEditor);
            if (categorizedProblems.length > 0) {
                problems.put(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, categorizedProblems);
            }
            // AspectJ Change end
        }
        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) {
            AJLog.log(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " //$NON-NLS-1$
                    + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$
            AJLog.log(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " //$NON-NLS-1$
                    + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$
        }
        return unit;
    } catch (OperationCanceledException e) {
        // catch this exception so as to not enter the
        // catch(RuntimeException e) below
        throw e;
    } catch (RuntimeException e) {
        String message = handleException(unitElement, environment, e);
        throw new JavaModelException(new RuntimeException(message, e),
                IJavaModelStatusConstants.COMPILER_FAILURE);

    } finally {
        if (environment != null)
            environment.setMonitor(null); // AJDT 3.6 // 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();
    }
}

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

License:Open Source License

public static void resolve(ICompilationUnit[] compilationUnits, String[] bindingKeys, ASTRequestor requestor,
        int apiLevel, Map options, IJavaProject javaProject, WorkingCopyOwner owner, int flags,
        IProgressMonitor monitor) {//w  w w.  jav a 2 s . c  o  m

    CancelableNameEnvironment environment = null;
    CancelableProblemFactory problemFactory = null;
    try {
        if (monitor != null) {
            int amountOfWork = (compilationUnits.length + bindingKeys.length) * 2; // 1 for beginToCompile, 1 for resolve
            monitor.beginTask("", amountOfWork); //$NON-NLS-1$
        }
        environment = new CancelableNameEnvironment(((JavaProject) javaProject), owner, monitor);
        problemFactory = new CancelableProblemFactory(monitor);
        CompilerOptions compilerOptions = getCompilerOptions(options,
                (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
        compilerOptions.ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
        // GROOVY start
        CompilerUtils.configureOptionsBasedOnNature(compilerOptions, javaProject);
        // GROOVY end
        CompilationUnitResolver resolver = new CompilationUnitResolver(environment, getHandlingPolicy(),
                compilerOptions, getRequestor(), problemFactory, monitor, javaProject != null);
        resolver.resolve(compilationUnits, bindingKeys, requestor, apiLevel, options, owner, flags);
        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 (JavaModelException e) {
        // project doesn't exist -> simple parse without resolving
        parse(compilationUnits, requestor, apiLevel, options, flags, monitor);
    } finally {
        if (monitor != null)
            monitor.done();
        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
        }
    }
}

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

License:Open Source License

public static void resolve(String[] sourceUnits, String[] encodings, String[] bindingKeys,
        FileASTRequestor requestor, int apiLevel, Map options, List classpaths, int flags,
        IProgressMonitor monitor) {// w  ww.j a v a 2  s.  c  o  m

    INameEnvironmentWithProgress environment = null;
    CancelableProblemFactory problemFactory = null;
    try {
        if (monitor != null) {
            int amountOfWork = (sourceUnits.length + bindingKeys.length) * 2; // 1 for beginToCompile, 1 for resolve
            monitor.beginTask("", amountOfWork); //$NON-NLS-1$
        }
        Classpath[] allEntries = new Classpath[classpaths.size()];
        classpaths.toArray(allEntries);
        environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
        problemFactory = new CancelableProblemFactory(monitor);
        CompilerOptions compilerOptions = getCompilerOptions(options,
                (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
        compilerOptions.ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
        CompilationUnitResolver resolver = new CompilationUnitResolver(environment, getHandlingPolicy(),
                compilerOptions, getRequestor(), problemFactory, monitor, false);
        resolver.resolve(sourceUnits, encodings, bindingKeys, requestor, apiLevel, options, flags);
        if (NameLookup.VERBOSE && (environment instanceof CancelableNameEnvironment)) {
            CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
            System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " //$NON-NLS-1$
                    + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$
            System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " //$NON-NLS-1$
                    + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$
        }
    } finally {
        if (monitor != null)
            monitor.done();
        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
        }
    }
}

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

License:Open Source License

public static CompilationUnitDeclaration resolve(
        org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, IJavaProject javaProject,
        List classpaths, NodeSearcher nodeSearcher, Map options, WorkingCopyOwner owner, int flags,
        IProgressMonitor monitor) throws JavaModelException {

    CompilationUnitDeclaration unit = null;
    INameEnvironmentWithProgress environment = null;
    CancelableProblemFactory problemFactory = null;
    CompilationUnitResolver resolver = null;
    try {/*from  w ww. j a  v a 2  s  .  c  o  m*/
        if (javaProject == null) {
            Classpath[] allEntries = new Classpath[classpaths.size()];
            classpaths.toArray(allEntries);
            environment = new NameEnvironmentWithProgress(allEntries, null, monitor);
        } else {
            environment = new CancelableNameEnvironment((JavaProject) javaProject, owner, monitor);
        }
        problemFactory = new CancelableProblemFactory(monitor);
        CompilerOptions compilerOptions = getCompilerOptions(options,
                (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
        boolean ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
        compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
        // GROOVY start   
        CompilerUtils.configureOptionsBasedOnNature(compilerOptions, javaProject);
        // GROOVY end
        resolver = new CompilationUnitResolver(environment, getHandlingPolicy(), compilerOptions,
                getRequestor(), problemFactory, monitor, javaProject != null);
        boolean analyzeAndGenerateCode = !ignoreMethodBodies;
        unit = resolver.resolve(null, // no existing compilation unit declaration
                sourceUnit, nodeSearcher, true, // method verification
                analyzeAndGenerateCode, // analyze code
                analyzeAndGenerateCode); // generate code
        if (resolver.hasCompilationAborted) {
            // the bindings could not be resolved due to missing types in name environment
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=86541
            CompilationUnitDeclaration unitDeclaration = parse(sourceUnit, nodeSearcher, options, flags);
            final int problemCount = unit.compilationResult.problemCount;
            if (problemCount != 0) {
                unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount];
                System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems,
                        0, problemCount);
                unitDeclaration.compilationResult.problemCount = problemCount;
            }
            return unitDeclaration;
        }
        if (NameLookup.VERBOSE && environment instanceof CancelableNameEnvironment) {
            CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
            System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " //$NON-NLS-1$
                    + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$
            System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " //$NON-NLS-1$
                    + cancelableNameEnvironment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$
        }
        return unit;
    } finally {
        if (environment != null) {
            // don't hold a reference to this external object
            environment.setMonitor(null);
        }
        if (problemFactory != null) {
            problemFactory.monitor = null; // don't hold a reference to this external object
        }
    }
}

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 {//  ww w  .  j  av  a  2  s  .c  o 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;
}

From source file:org.springframework.ide.vscode.boot.java.utils.CUResolver.java

License:Open Source License

static CompilationUnitDeclaration resolve(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit,
        List<Classpath> classpaths, Map<String, String> options, int flags,
        INameEnvironmentWithProgress environment) throws JavaModelException {
    try {// w w w.  j a v  a 2s . c o m

        CompilerOptions compilerOptions = (CompilerOptions) GET_COMPILER_OPTIONS_METHOD.get().invoke(null,
                options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
        CancelableProblemFactory problemFactory = new CancelableProblemFactory(new NullProgressMonitor());
        boolean ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
        compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
        Object resolver = COMPILATION_UNIT_RESOLVER_CONSTRUCTOR.get().newInstance(environment,
                GET_HANDLER_POLICY_METHOD.get().invoke(null), compilerOptions,
                GET_REQUESTOR_METHOD.get().invoke(null), problemFactory, new NullProgressMonitor(), false);
        boolean analyzeAndGenerateCode = !ignoreMethodBodies;
        // no existing compilation unit declaration
        CompilationUnitDeclaration unit = (CompilationUnitDeclaration) RESOLVE_METHOD.get().invoke(resolver,
                null, sourceUnit, null, true, // method verification
                analyzeAndGenerateCode, // analyze code
                analyzeAndGenerateCode); // generate code
        boolean hasCompilationAborted = HAS_COMPILATION_ABORTED_FIELD.get().getBoolean(resolver);
        if (hasCompilationAborted) {
            // the bindings could not be resolved due to missing types in name environment
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=86541
            CompilationUnitDeclaration unitDeclaration = parse(sourceUnit, options, flags);
            //         if (unit != null) {
            //            final int problemCount = unit.compilationResult.problemCount;
            //            if (problemCount != 0) {
            //               unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount];
            //               System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems, 0, problemCount);
            //               unitDeclaration.compilationResult.problemCount = problemCount;
            //            }
            //         } else if (resolver.abortProblem != null) {
            //            unitDeclaration.compilationResult.problemCount = 1;
            //            unitDeclaration.compilationResult.problems = new CategorizedProblem[] { resolver.abortProblem };
            //         }
            return unitDeclaration;
        }
        return unit;

    } catch (Exception e) {
        log.error("{}", e);
    }
    return null;
}