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

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

Introduction

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

Prototype

public CompilationUnitResolver(INameEnvironment environment, IErrorHandlingPolicy policy,
        CompilerOptions compilerOptions, ICompilerRequestor requestor, IProblemFactory problemFactory,
        IProgressMonitor monitor, boolean fromJavaProject) 

Source Link

Document

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

Usage

From source file:org.codehaus.groovy.eclipse.core.compiler.GroovySnippetCompiler.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
private GroovyCompilationUnitDeclaration internalCompile(String source, String sourcePath) {
    if (sourcePath == null) {
        sourcePath = "Nothing.groovy";
    } else if (!ContentTypeUtils.isGroovyLikeFileName(sourcePath)) {
        sourcePath = sourcePath.concat(".groovy");
    }/*from   w  w w .  jav  a 2 s .  c  om*/

    Map options = JavaCore.getOptions();
    options.put(CompilerOptions.OPTIONG_BuildGroovyFiles, CompilerOptions.ENABLED);
    Compiler compiler = new CompilationUnitResolver(nameEnvironment,
            DefaultErrorHandlingPolicies.proceedWithAllProblems(), new CompilerOptions(options),
            new Requestor(), new DefaultProblemFactory(), null, true);
    GroovyCompilationUnitDeclaration decl = (GroovyCompilationUnitDeclaration) compiler.resolve(
            new MockCompilationUnit(source.toCharArray(), sourcePath.toCharArray()), true, false, false);
    return decl;
}

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) {/*from  ww w  . j av a2  s .c om*/

    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) {//from   w w w.  j av 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 {// ww  w  .  ja v  a 2 s. c om
        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
        }
    }
}