Example usage for org.eclipse.jdt.core ICompilationUnit ENABLE_STATEMENTS_RECOVERY

List of usage examples for org.eclipse.jdt.core ICompilationUnit ENABLE_STATEMENTS_RECOVERY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ICompilationUnit ENABLE_STATEMENTS_RECOVERY.

Prototype

int ENABLE_STATEMENTS_RECOVERY

To view the source code for org.eclipse.jdt.core ICompilationUnit ENABLE_STATEMENTS_RECOVERY.

Click Source Link

Document

Constant indicating that a reconcile operation should enable the statements recovery.

Usage

From source file:cideplus.ui.astview.ASTView.java

License:Open Source License

private CompilationUnit createAST(ITypeRoot input, int astLevel, int offset)
        throws JavaModelException, CoreException {
    long startTime;
    long endTime;
    CompilationUnit root;/*ww  w  .j  a  va 2 s  . c  o m*/

    if ((getCurrentInputKind() == ASTInputKindAction.USE_RECONCILE)) {
        final IProblemRequestor problemRequestor = new IProblemRequestor() { //strange: don't get bindings when supplying null as problemRequestor
            public void acceptProblem(IProblem problem) {
                /*not interested*/}

            public void beginReporting() {
                /*not interested*/}

            public void endReporting() {
                /*not interested*/}

            public boolean isActive() {
                return true;
            }
        };
        WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {
            @Override
            public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
                return problemRequestor;
            }
        };
        ICompilationUnit wc = input.getWorkingCopy(workingCopyOwner, null);
        try {
            int reconcileFlags = ICompilationUnit.FORCE_PROBLEM_DETECTION;
            if (fStatementsRecovery)
                reconcileFlags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
            if (fBindingsRecovery)
                reconcileFlags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
            if (fIgnoreMethodBodies)
                reconcileFlags |= ICompilationUnit.IGNORE_METHOD_BODIES;
            startTime = System.currentTimeMillis();
            root = wc.reconcile(getCurrentASTLevel(), reconcileFlags, null, null);
            endTime = System.currentTimeMillis();
        } finally {
            wc.discardWorkingCopy();
        }

    } else if (input instanceof ICompilationUnit && (getCurrentInputKind() == ASTInputKindAction.USE_CACHE)) {
        ICompilationUnit cu = (ICompilationUnit) input;
        startTime = System.currentTimeMillis();
        root = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_NO, null);
        endTime = System.currentTimeMillis();

    } else {
        ASTParser parser = ASTParser.newParser(astLevel);
        parser.setResolveBindings(fCreateBindings);
        if (input instanceof ICompilationUnit) {
            parser.setSource((ICompilationUnit) input);
        } else {
            parser.setSource((IClassFile) input);
        }
        parser.setStatementsRecovery(fStatementsRecovery);
        parser.setBindingsRecovery(fBindingsRecovery);
        parser.setIgnoreMethodBodies(fIgnoreMethodBodies);
        if (getCurrentInputKind() == ASTInputKindAction.USE_FOCAL) {
            parser.setFocalPosition(offset);
        }
        startTime = System.currentTimeMillis();
        root = (CompilationUnit) parser.createAST(null);
        endTime = System.currentTimeMillis();
    }
    if (root != null) {
        updateContentDescription(input, root, endTime - startTime);
    }
    return root;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

protected boolean buildStructure(OpenableElementInfo info, final IProgressMonitor pm, Map newElements,
        File underlyingResource) throws JavaModelException {
    CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info;

    // ensure buffer is opened
    IBuffer buffer = getBufferManager().getBuffer(CompilationUnit.this);
    if (buffer == null) {
        openBuffer(pm, unitInfo); // open buffer independently from the info, since we are building the info
    }//from w  ww.  j  av  a  2s  . co  m

    // generate structure and compute syntax problems if needed
    CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo,
            newElements, manager);
    JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo();
    IJavaProject project = getJavaProject();

    boolean createAST;
    boolean resolveBindings;
    int reconcileFlags;
    HashMap problems;
    if (info instanceof ASTHolderCUInfo) {
        ASTHolderCUInfo astHolder = (ASTHolderCUInfo) info;
        createAST = astHolder.astLevel != NO_AST;
        resolveBindings = astHolder.resolveBindings;
        reconcileFlags = astHolder.reconcileFlags;
        problems = astHolder.problems;
    } else {
        createAST = false;
        resolveBindings = true;
        reconcileFlags = 0;
        problems = null;
    }
    boolean computeProblems = false;
    //   boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo.isActive() && project != null && JavaProject
    //         .hasJavaNature(project.getProject());
    IProblemFactory problemFactory = new DefaultProblemFactory();
    Map options = project == null ? JavaCore.getOptions() : project.getOptions(true);
    if (!computeProblems) {
        // disable task tags checking to speed up parsing
        options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
    }
    CompilerOptions compilerOptions = new CompilerOptions(options);
    compilerOptions.ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
    SourceElementParser parser = new SourceElementParser(requestor, problemFactory, compilerOptions,
            true/*report local declarations*/,
            !createAST /*optimize string literals only if not creating a DOM AST*/);
    parser.reportOnlyOneSyntaxError = !computeProblems;
    parser.setMethodsFullRecovery(true);
    parser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);

    if (!computeProblems && !resolveBindings && !createAST) // disable javadoc parsing if not computing problems, not resolving and not creating ast
        parser.javadocParser.checkDocComment = false;
    requestor.parser = parser;

    // update timestamp (might be IResource.NULL_STAMP if original does not exist)
    if (underlyingResource == null) {
        underlyingResource = resource();
    }
    // underlying resource is null in the case of a working copy on a class file in a jar
    if (underlyingResource != null)
        unitInfo.timestamp = (underlyingResource).lastModified();

    // compute other problems if needed
    CompilationUnitDeclaration compilationUnitDeclaration = null;
    CompilationUnit source = cloneCachingContents();
    try {
        if (computeProblems) {
            //         if (problems == null) {
            //            // report problems to the problem requestor
            //            problems = new HashMap();
            //            compilationUnitDeclaration = CompilationUnitProblemFinder
            //                  .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm);
            //            try {
            //               perWorkingCopyInfo.beginReporting();
            //               for (Iterator iteraror = problems.values().iterator(); iteraror.hasNext();) {
            //                  CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror.next();
            //                  if (categorizedProblems == null) continue;
            //                  for (int i = 0, length = categorizedProblems.length; i < length; i++) {
            //                     perWorkingCopyInfo.acceptProblem(categorizedProblems[i]);
            //                  }
            //               }
            //            } finally {
            //               perWorkingCopyInfo.endReporting();
            //            }
            //         } else {
            //            // collect problems
            //            compilationUnitDeclaration = CompilationUnitProblemFinder
            //                  .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm);
            //         }
        } else {
            compilationUnitDeclaration = parser.parseCompilationUnit(source,
                    true /*full parse to find local elements*/, pm);
        }

        if (createAST) {
            //         int astLevel = ((ASTHolderCUInfo) info).astLevel;
            //         org.eclipse.jdt.core.dom.CompilationUnit cu = AST
            //               .convertCompilationUnit(astLevel, compilationUnitDeclaration, options, computeProblems, source, reconcileFlags, pm);
            //         ((ASTHolderCUInfo) info).ast = cu;
        }
    } finally {
        if (compilationUnitDeclaration != null) {
            unitInfo.hasFunctionalTypes = compilationUnitDeclaration.hasFunctionalTypes();
            compilationUnitDeclaration.cleanUp();
        }
    }

    return unitInfo.isStructureKnown();
}

From source file:org.codehaus.jdt.groovy.model.GroovyCompilationUnit.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes", "nls", "restriction" })
@Override// w ww  .  ja  v a 2s .com
protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements,
        IResource underlyingResource) throws JavaModelException {
    try {
        depth.set(depth.get() + 1);

        // if (!isOnBuildPath()) {
        // return false;
        // }

        if (GroovyLogManager.manager.hasLoggers()) {
            GroovyLogManager.manager.log(TraceCategory.COMPILER, "Build Structure starting for " + this.name);
            GroovyLogManager.manager
                    .logStart("Build structure: " + name + " : " + Thread.currentThread().getName());
        }

        CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info;

        // ensure buffer is opened
        IBuffer buffer = getBufferManager().getBuffer(this);
        if (buffer == null) {
            openBuffer(pm, unitInfo); // open buffer independently from the
            // info, since we are building the info
        }

        // generate structure and compute syntax problems if needed
        GroovyCompilationUnitStructureRequestor requestor = new GroovyCompilationUnitStructureRequestor(this,
                unitInfo, newElements);
        JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo();
        JavaProject project = (JavaProject) getJavaProject();

        // determine what kind of buildStructure we are doing
        boolean createAST;
        int reconcileFlags;
        boolean resolveBindings;
        HashMap problems;
        if (info instanceof ASTHolderCUInfo) {
            ASTHolderCUInfo astHolder = (ASTHolderCUInfo) info;
            createAST = ((Integer) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "astLevel", //$NON-NLS-1$
                    astHolder)).intValue() != NO_AST;
            resolveBindings = ((Boolean) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class,
                    "resolveBindings", astHolder)).booleanValue();
            reconcileFlags = ((Integer) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "reconcileFlags", //$NON-NLS-1$
                    astHolder)).intValue();
            problems = (HashMap) ReflectionUtils.getPrivateField(ASTHolderCUInfo.class, "problems", astHolder);
        } else {
            createAST = false;
            resolveBindings = false;
            reconcileFlags = 0;
            problems = null;
        }

        boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo.isActive() && project != null
                && JavaProject.hasJavaNature(project.getProject());
        IProblemFactory problemFactory = new DefaultProblemFactory();

        // compiler options
        Map<String, String> options = (project == null ? JavaCore.getOptions() : project.getOptions(true));
        if (!computeProblems) {
            // disable task tags checking to speed up parsing
            options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
        }

        // FIXASC deal with the case of project==null to reduce duplication in this next line and call to setGroovyClasspath
        // Required for Groovy, but not for Java
        options.put(CompilerOptions.OPTIONG_BuildGroovyFiles, CompilerOptions.ENABLED);

        CompilerOptions compilerOptions = new CompilerOptions(options);

        if (project != null) {
            CompilerUtils.setGroovyClasspath(compilerOptions, project);
        }

        // Required for Groovy, but not for Java
        ProblemReporter reporter = new ProblemReporter(new GroovyErrorHandlingPolicy(!computeProblems),
                compilerOptions, new DefaultProblemFactory());

        SourceElementParser parser = new MultiplexingSourceElementRequestorParser(reporter, requestor, /*
                                                                                                       * not needed if
                                                                                                       * computing groovy only
                                                                                                       */
                problemFactory, compilerOptions, true/* report local declarations */, !createAST /*
                                                                                                 * optimize string literals only if not
                                                                                                 * creating a DOM AST
                                                                                                 */);
        parser.reportOnlyOneSyntaxError = !computeProblems;
        // maybe not needed for groovy, but I don't want to find out.
        parser.setMethodsFullRecovery(true);
        parser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);

        if (!computeProblems && !resolveBindings && !createAST) // disable javadoc parsing if not computing problems, not
            // resolving
            // and not creating ast
            parser.javadocParser.checkDocComment = false;
        requestor.setParser(parser);

        // update timestamp (might be IResource.NULL_STAMP if original does not
        // exist)
        if (underlyingResource == null) {
            underlyingResource = getResource();
        }
        // underlying resource is null in the case of a working copy on a class
        // file in a jar
        if (underlyingResource != null) {
            ReflectionUtils.setPrivateField(CompilationUnitElementInfo.class, "timestamp", unitInfo, //$NON-NLS-1$
                    underlyingResource.getModificationStamp());
        }

        GroovyCompilationUnitDeclaration compilationUnitDeclaration = null;
        CompilationUnit source = cloneCachingContents();
        try {
            // GROOVY
            // note that this is a slightly different approach than taken by super.buildStructure
            // in super.buildStructure, there is a test here to see if computeProblems is true.
            // if false, then parser.parserCompilationUnit is called.
            // this will not work for Groovy because we need to ensure bindings are resolved
            // for many operations (content assist and code select) to work.
            // So, for groovy, always use CompilationUnitProblemFinder.process and then process problems
            // separately only if necessary
            // addendum (GRECLIPSE-942). The testcase for that bug includes a package with 200
            // types in that refer to each other in a chain, through field references. If a reconcile
            // references the top of the chain we can go through a massive number of recursive calls into
            // this buildStructure for each one. The 'full' parse (with bindings) is only required for
            // the top most (regardless of the computeProblems setting) and so we track how many recursive
            // calls we have made - if we are at depth 2 we do what JDT was going to do (the quick thing).
            if (computeProblems || depth.get() < 2) {
                if (problems == null) {
                    // report problems to the problem requestor
                    problems = new HashMap();
                    compilationUnitDeclaration = (GroovyCompilationUnitDeclaration) CompilationUnitProblemFinder
                            .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm);
                    if (computeProblems) {
                        try {
                            perWorkingCopyInfo.beginReporting();
                            for (Iterator iteraror = problems.values().iterator(); iteraror.hasNext();) {
                                CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror
                                        .next();
                                if (categorizedProblems == null)
                                    continue;
                                for (int i = 0, length = categorizedProblems.length; i < length; i++) {
                                    perWorkingCopyInfo.acceptProblem(categorizedProblems[i]);
                                }
                            }
                        } finally {
                            perWorkingCopyInfo.endReporting();
                        }
                    }
                } else {
                    // collect problems
                    compilationUnitDeclaration = (GroovyCompilationUnitDeclaration) CompilationUnitProblemFinder
                            .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm);
                }
            } else {
                compilationUnitDeclaration = (GroovyCompilationUnitDeclaration) parser
                        .parseCompilationUnit(source, true /* full parse to find local elements */, pm);
            }

            // GROOVY
            // if this is a working copy, then we have more work to do
            maybeCacheModuleNode(perWorkingCopyInfo, compilationUnitDeclaration);

            // create the DOM AST from the compiler AST
            if (createAST) {
                org.eclipse.jdt.core.dom.CompilationUnit ast;
                try {
                    ast = AST.convertCompilationUnit(AST.JLS3, compilationUnitDeclaration, options,
                            computeProblems, source, reconcileFlags, pm);
                    ReflectionUtils.setPrivateField(ASTHolderCUInfo.class, "ast", info, ast); //$NON-NLS-1$
                } catch (OperationCanceledException e) {
                    // catch this exception so as to not enter the catch(RuntimeException e) below
                    // might need to do the same for AbortCompilation
                    throw e;
                } catch (IllegalArgumentException e) {
                    // if necessary, we can do some better reporting here.
                    Util.log(e, "Problem with build structure: Offset for AST node is incorrect in " //$NON-NLS-1$
                            + this.getParent().getElementName() + "." + getElementName()); //$NON-NLS-1$
                } catch (Exception e) {
                    Util.log(e, "Problem with build structure for " + this.getElementName()); //$NON-NLS-1$
                }
            }
        } catch (OperationCanceledException e) {
            // catch this exception so as to not enter the catch(RuntimeException e) below
            // might need to do the same for AbortCompilation
            throw e;
        } catch (JavaModelException e) {
            // GRECLIPSE-1480 don't log element does not exist exceptions. since this could occur when element is in a non-java
            // project
            if (e.getStatus().getCode() != IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST
                    || this.getJavaProject().exists()) {
                Util.log(e, "Problem with build structure for " + this.getElementName()); //$NON-NLS-1$
            }
        } catch (Exception e) {
            // GROOVY: The groovy compiler does not handle broken code well in many situations
            // use this general catch clause so that exceptions thrown by broken code
            // do not bubble up the stack.
            Util.log(e, "Problem with build structure for " + this.getElementName()); //$NON-NLS-1$
        } finally {
            if (compilationUnitDeclaration != null) {
                compilationUnitDeclaration.cleanUp();
            }
        }
        return unitInfo.isStructureKnown();
    } finally {
        depth.set(depth.get() - 1);
        if (GroovyLogManager.manager.hasLoggers()) {
            GroovyLogManager.manager.logEnd(
                    "Build structure: " + name + " : " + Thread.currentThread().getName(),
                    TraceCategory.COMPILER);
        }
    }
}

From source file:org.eclipse.ajdt.core.javaelements.AJCompilationUnit.java

License:Open Source License

/**
 * builds the structure of this Compilation unit.  We need to use an aspect-aware parser for this (in the org.aspectj.org.eclipse... world, which
 * makes things a little messy//from   ww  w.  j  av  a  2s.co m
 */
protected boolean buildStructure(OpenableElementInfo info, final IProgressMonitor pm, Map newElements,
        IResource underlyingResource) throws JavaModelException {
    AJCompilationUnitInfo unitInfo = (AJCompilationUnitInfo) info;

    if (ajFile == null) {
        return false;
    }

    // ensure buffer is opened
    IBuffer buffer = getBufferManager().getBuffer(this);
    if (buffer == null) {
        openBuffer(pm, unitInfo); // open buffer independently from the info, since we are building the info
    }

    // generate structure and compute syntax problems if needed
    AJCompilationUnitStructureRequestor requestor = new AJCompilationUnitStructureRequestor(this, unitInfo,
            newElements);
    JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo();
    IJavaProject project = getJavaProject();

    boolean createAST;
    boolean resolveBindings;
    int reconcileFlags;
    //        HashMap problems;
    AJCompilationUnitInfo astHolder = (AJCompilationUnitInfo) info;
    createAST = astHolder.getASTLevel() != NO_AST;
    resolveBindings = astHolder.doResolveBindings();
    reconcileFlags = astHolder.getReconcileFlags();
    //        problems = astHolder.getProblems();

    boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo.isActive() && project != null
            && AspectJPlugin.isAJProject(project.getProject());
    org.aspectj.org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory problemFactory = new org.aspectj.org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory();
    Map options = project == null ? JavaCore.getOptions() : project.getOptions(true);
    if (!computeProblems) {
        // disable task tags checking to speed up parsing
        options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
    }

    // ensure parser sees the real contents (not the fake java buffer)
    this.requestOriginalContentMode();

    // use an aspectj aware source parser
    AJSourceElementParser ajdtParser = new AJSourceElementParser(requestor, problemFactory,
            new org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions(options),
            true/*report local declarations*/,
            !createAST /*optimize string literals only if not creating a DOM AST*/);

    ajdtParser.reportOnlyOneSyntaxError = !computeProblems;
    ajdtParser.setMethodsFullRecovery(true);
    ajdtParser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);

    if (!computeProblems && !resolveBindings && !createAST) {
        // disable javadoc parsing if not computing problems, not resolving and not creating ast
        ajdtParser.javadocParser.checkDocComment = false;
    }
    requestor.setParser(ajdtParser);

    // update timestamp (might be IResource.NULL_STAMP if original does not exist)
    if (underlyingResource == null) {
        underlyingResource = getResource();
    }
    // underlying resource is null in the case of a working copy on a class file in a jar
    if (underlyingResource != null)
        unitInfo.setTimestamp(((IFile) underlyingResource).getModificationStamp());

    // compute other problems if needed
    CompilationUnitDeclaration compilationUnitDeclaration = null;
    final AJCompilationUnit source = ajCloneCachingContents();
    requestor.setSource(source.getContents());
    try {
        if (false) {
            // for now, don't go here
            // the problem is that we can't find problems and build structure at the same time
            // they require difference kinds of parsers.
            //            if (computeProblems) {
            //                if (problems == null) {
            //                    // report problems to the problem requestor
            //                    problems = new HashMap();
            //                    compilationUnitDeclaration = AJCompilationUnitProblemFinder.processAJ(
            //                            source, ajdtParser, this.owner, problems, createAST, reconcileFlags, pm);
            //                    try {
            //                        perWorkingCopyInfo.beginReporting();
            //                        for (Iterator iteraror = problems.values().iterator(); iteraror.hasNext();) {
            //                            CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror.next();
            //                            if (categorizedProblems == null) continue;
            //                            for (int i = 0, length = categorizedProblems.length; i < length; i++) {
            //                                perWorkingCopyInfo.acceptProblem(categorizedProblems[i]);
            //                            }
            //                        }
            //                    } finally {
            //                        perWorkingCopyInfo.endReporting();
            //                    }
            //                } else {
            //                    // collect problems
            //                    compilationUnitDeclaration = AJCompilationUnitProblemFinder.processAJ(source, ajdtParser, this.owner, problems, createAST, reconcileFlags, pm);
            //                }
        } else {
            // since we are doing n aspectj aware parse with the AJ parser
            // need to wrap the results in a JDT CompilationUnitDeclaration
            org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration ajDeclaration = ajdtParser
                    .parseCompilationUnit(
                            new org.aspectj.org.eclipse.jdt.internal.compiler.env.ICompilationUnit() {
                                public char[] getContents() {
                                    return source.getContents();
                                }

                                public char[] getMainTypeName() {
                                    return source.getMainTypeName();
                                }

                                public char[][] getPackageName() {
                                    return source.getPackageName();
                                }

                                public char[] getFileName() {
                                    return source.getFileName();
                                }

                                public boolean ignoreOptionalProblems() {
                                    return false;
                                }
                            }, true /*full parse to find local elements*/);
            compilationUnitDeclaration = new AJCompilationUnitDeclarationWrapper(ajDeclaration, source);
        }

        if (createAST) {
            // XXX hmmmm...may not work
            int astLevel = unitInfo.getASTLevel();
            org.eclipse.jdt.core.dom.CompilationUnit cu = AST.convertCompilationUnit(astLevel,
                    compilationUnitDeclaration, options, computeProblems, source, reconcileFlags, pm);
            unitInfo.setAST(cu);
        }
    } finally {
        discardOriginalContentMode();
        if (compilationUnitDeclaration != null) {
            compilationUnitDeclaration.cleanUp();
        }
    }

    return unitInfo.isStructureKnown();
}

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 {//ww  w .jav  a2  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.ajdt.core.tests.problemfinding.AbstractProblemFindingTests.java

License:Apache License

/**
 * @throws JavaModelException/*from   w ww.  j  a  v a  2 s.c  o  m*/
 */
private void assertNoProblems(CompilationUnit unit) throws JavaModelException {
    HashMap<String, CategorizedProblem[]> problems = new HashMap<String, CategorizedProblem[]>();
    AJCompilationUnitProblemFinder.processAJ(
            unit, AJWorkingCopyOwner.INSTANCE, problems, true, ICompilationUnit.ENABLE_BINDINGS_RECOVERY
                    | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.FORCE_PROBLEM_DETECTION,
            null);

    MockProblemRequestor.filterAllWarningProblems(problems);
    assertEquals("Should not have any problems in " + unit + " but found:\n" //$NON-NLS-1$
            + MockProblemRequestor.printProblems(problems), 0, problems.size());
}

From source file:org.eclipse.ajdt.core.tests.problemfinding.Bug273691Reconciling.java

License:Open Source License

private HashMap doFind(ICompilationUnit unit) throws JavaModelException {
    HashMap problems = new HashMap();
    if (unit instanceof AJCompilationUnit) {
        AJCompilationUnitProblemFinder.processAJ((AJCompilationUnit) unit, AJWorkingCopyOwner.INSTANCE,
                problems, true,//from ww  w.j  a  v  a2s  .c om
                ICompilationUnit.ENABLE_BINDINGS_RECOVERY | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY
                        | ICompilationUnit.FORCE_PROBLEM_DETECTION,
                null);
    } else {
        // Requires JDT Weaving
        CompilationUnitProblemFinder.process((CompilationUnit) unit, null, DefaultWorkingCopyOwner.PRIMARY,
                problems, true,
                ICompilationUnit.ENABLE_BINDINGS_RECOVERY | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY
                        | ICompilationUnit.FORCE_PROBLEM_DETECTION,
                null);
    }
    return problems;
}

From source file:org.eclipse.ajdt.core.tests.problemfinding.ProblemFinderTests.java

License:Open Source License

/**
 * project should have no problems at first
 * @throws Exception/*from   www.java2s  .c  o  m*/
 */
public void testNoProblemsMyAspect() throws Exception {
    HashMap problems = new HashMap();
    AJCompilationUnitProblemFinder.processAJ(
            myAspectCU, AJWorkingCopyOwner.INSTANCE, problems, true, ICompilationUnit.ENABLE_BINDINGS_RECOVERY
                    | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.FORCE_PROBLEM_DETECTION,
            null);

    MockProblemRequestor.filterAllWarningProblems(problems);
    assertEquals("Should not have any problems in " + myAspectCU + " but found:\n" //$NON-NLS-1$
            + MockProblemRequestor.printProblems(problems), 0, problems.size());
}

From source file:org.eclipse.ajdt.core.tests.problemfinding.ProblemFinderTests.java

License:Open Source License

public void testNoProblemsOtherClass() throws Exception {
    HashMap problems = new HashMap();
    AJCompilationUnitProblemFinder.processAJ(
            otherClassCU, AJWorkingCopyOwner.INSTANCE, problems, true, ICompilationUnit.ENABLE_BINDINGS_RECOVERY
                    | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.FORCE_PROBLEM_DETECTION,
            null);/*from  ww  w  . j a v a2s .c  o  m*/

    MockProblemRequestor.filterAllWarningProblems(problems);
    assertEquals("Should not have any problems in " + otherClassCU + " but found:\n" //$NON-NLS-1$
            + MockProblemRequestor.printProblems(problems), 0, problems.size());
}

From source file:org.eclipse.ajdt.core.tests.problemfinding.ProblemFinderTests.java

License:Open Source License

public void testNoProblemsDemo() throws Exception {
    HashMap problems = new HashMap();
    AJCompilationUnitProblemFinder.processAJ(
            demoCU, AJWorkingCopyOwner.INSTANCE, problems, true, ICompilationUnit.ENABLE_BINDINGS_RECOVERY
                    | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.FORCE_PROBLEM_DETECTION,
            null);/*  www. j  a  va 2s.c  om*/

    MockProblemRequestor.filterAllWarningProblems(problems);
    assertEquals("Should not have any problems in " + demoCU + " but found:\n" //$NON-NLS-1$
            + MockProblemRequestor.printProblems(problems), 0, problems.size());
}