Example usage for org.eclipse.jdt.core.dom ASTParser setBindingsRecovery

List of usage examples for org.eclipse.jdt.core.dom ASTParser setBindingsRecovery

Introduction

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

Prototype

public void setBindingsRecovery(boolean enabled) 

Source Link

Document

Requests that the compiler should perform bindings recovery.

Usage

From source file:br.uff.ic.mergeguider.javaparser.JavaParser.java

private Storage generateASTs(String path) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);

    parser.setResolveBindings(true);//from  w ww . j  av  a2 s .  com
    parser.setBindingsRecovery(true);

    Map options = JavaCore.getOptions();

    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
    parser.setCompilerOptions(options);

    String[] srcDirs = FileUtils.getAllDirs(path);
    String[] javaFiles = FileUtils.getAllJavaFiles(path);

    parser.setEnvironment(null, srcDirs, null, true);

    Storage storage = new Storage();
    parser.createASTs(javaFiles, null, new String[0], storage, null);
    return storage;
}

From source file:ca.ecliptical.pde.internal.ds.DSAnnotationCompilationParticipant.java

License:Open Source License

private void processAnnotations(IJavaProject javaProject, Map<ICompilationUnit, BuildContext> fileMap) {
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setResolveBindings(true);/*from   w ww.jav  a2s .c o  m*/
    parser.setBindingsRecovery(true);
    parser.setProject(javaProject);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    ProjectContext projectContext = processingContext.get(javaProject);
    ProjectState state = projectContext.getState();

    parser.setIgnoreMethodBodies(state.getErrorLevel() == ValidationErrorLevel.none);

    ICompilationUnit[] cuArr = fileMap.keySet().toArray(new ICompilationUnit[fileMap.size()]);
    Map<ICompilationUnit, Collection<IDSModel>> models = new HashMap<ICompilationUnit, Collection<IDSModel>>();
    parser.createASTs(cuArr, new String[0], new AnnotationProcessor(models, fileMap, state.getErrorLevel()),
            null);

    Map<String, Collection<String>> cuMap = state.getMappings();
    Collection<String> unprocessed = projectContext.getUnprocessed();
    Collection<String> abandoned = projectContext.getAbandoned();

    IPath outputPath = new Path(state.getPath()).addTrailingSeparator();

    // save each model to a file; track changes to mappings
    for (Map.Entry<ICompilationUnit, Collection<IDSModel>> entry : models.entrySet()) {
        ICompilationUnit cu = entry.getKey();
        IType cuType = cu.findPrimaryType();
        if (cuType == null) {
            if (debug.isDebugging())
                debug.trace(String.format("CU %s has no primary type!", cu.getElementName())); //$NON-NLS-1$

            continue; // should never happen
        }

        String cuKey = cuType.getFullyQualifiedName();

        unprocessed.remove(cuKey);
        Collection<String> oldDSKeys = cuMap.remove(cuKey);
        Collection<String> dsKeys = new HashSet<String>();
        cuMap.put(cuKey, dsKeys);

        for (IDSModel model : entry.getValue()) {
            String compName = model.getDSComponent().getAttributeName();
            IPath filePath = outputPath.append(compName).addFileExtension("xml"); //$NON-NLS-1$
            String dsKey = filePath.toPortableString();

            // exclude file from garbage collection
            if (oldDSKeys != null)
                oldDSKeys.remove(dsKey);

            // add file to CU mapping
            dsKeys.add(dsKey);

            // actually save the file
            IFile compFile = PDEProject.getBundleRelativeFile(javaProject.getProject(), filePath);
            model.setUnderlyingResource(compFile);

            try {
                ensureDSProject(compFile.getProject());
            } catch (CoreException e) {
                Activator.getDefault().getLog().log(e.getStatus());
            }

            IPath parentPath = compFile.getParent().getProjectRelativePath();
            if (!parentPath.isEmpty()) {
                IFolder folder = javaProject.getProject().getFolder(parentPath);
                try {
                    ensureExists(folder);
                } catch (CoreException e) {
                    Activator.getDefault().getLog().log(e.getStatus());
                    model.dispose();
                    continue;
                }
            }

            if (debug.isDebugging())
                debug.trace(String.format("Saving model: %s", compFile.getFullPath())); //$NON-NLS-1$

            model.save();
            model.dispose();
        }

        // track abandoned files (may be garbage)
        if (oldDSKeys != null)
            abandoned.addAll(oldDSKeys);
    }
}

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;//from ww  w .j  av a2  s.  com

    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.facebook.nuclide.debugger.EvaluationManager.java

License:Open Source License

private CompilationUnit compileSource(String source, String sourceFilePath, String unitName,
        StringBuilder errors) throws Exception {

    if (!unitName.endsWith(".java")) {
        // The AST compiler is surprisingly insistent about this.
        unitName += ".java";
    }//  www  .j a va2  s  . c om

    final File sourceFile = new File(sourceFilePath);
    final String directoryPath = sourceFile.getAbsoluteFile().getParent();
    Utils.logVerbose("compiling source for: " + directoryPath);
    final String[] sources = _contextManager.getSourceLocator().getSourceSearchPaths();
    final String[] classpath = _contextManager.getSourceLocator().getBinaryJarPaths();

    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    final Map<String, String> options = JavaCore.getOptions();

    JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
    parser.setCompilerOptions(options);
    parser.setUnitName(unitName);
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    parser.setBindingsRecovery(true);
    parser.setEnvironment(classpath, sources, null, true);
    parser.setSource(source.toCharArray());

    CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    String errorMsg = checkUnitForProblems(unit);
    if (errorMsg != null) {
        errors.append(errorMsg);
    }

    return unit;
}

From source file:com.flamefire.importsmalinames.utils.Util.java

License:Open Source License

public static CompilationUnit createCU(ICompilationUnit cu, boolean resolveBindings) {
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setProject(cu.getJavaProject());
    parser.setSource(cu);//from w  w w .  ja va  2 s .  c om
    parser.setResolveBindings(resolveBindings);
    parser.setStatementsRecovery(true);
    parser.setBindingsRecovery(true);
    CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    return unit;
}

From source file:com.github.parzonka.ccms.sorter.callgraph.ASTUtils.java

License:Open Source License

public static ASTNode getAST(ICompilationUnit compilationUnit, IJavaProject project) {
    final ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(compilationUnit);/*from   w  w  w .ja  va  2s .c  o  m*/
    parser.setResolveBindings(true);
    parser.setBindingsRecovery(true);
    parser.setProject(project);
    return parser.createAST(null);
}

From source file:com.github.parzonka.ccms.sorter.callgraph.ASTUtils.java

License:Open Source License

/**
 * Returns an ASTNode for a ICompilationUnit usually representing the
 * Java-AST of a complete java source-code file.
 *
 * @param compilationUnit/*from   w ww.  j a  v a  2  s .com*/
 * @return
 */
public static ASTNode getAST(ICompilationUnit compilationUnit) {
    final ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(compilationUnit);
    parser.setResolveBindings(true);
    parser.setBindingsRecovery(false);
    return parser.createAST(null);
}

From source file:com.kodebeagle.javaparser.JavaASTParser.java

License:Apache License

/**
 * Return an ASTNode given the content//from   w w w .ja  v  a 2 s  . com
 *
 * @param content
 * @return
 */
public final ASTNode getASTNode(final char[] content, final ParseType parseType) {
    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    final int astKind;
    switch (parseType) {
    case CLASS_BODY:
    case METHOD:
        astKind = ASTParser.K_CLASS_BODY_DECLARATIONS;
        break;
    case COMPILATION_UNIT:
        astKind = ASTParser.K_COMPILATION_UNIT;
        break;
    case EXPRESSION:
        astKind = ASTParser.K_EXPRESSION;
        break;
    case STATEMENTS:
        astKind = ASTParser.K_STATEMENTS;
        break;
    default:
        astKind = ASTParser.K_COMPILATION_UNIT;
    }
    parser.setKind(astKind);

    final Map<String, String> options = new Hashtable<String, String>();
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
    if (useJavadocs) {
        options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
    }
    parser.setCompilerOptions(options);
    parser.setSource(content);
    parser.setResolveBindings(useBindings);
    parser.setBindingsRecovery(useBindings);
    parser.setStatementsRecovery(true);

    if (parseType != ParseType.METHOD) {
        return parser.createAST(null);
    } else {
        final ASTNode cu = parser.createAST(null);
        return getFirstMethodDeclaration(cu);
    }
}

From source file:com.liferay.blade.eclipse.provider.CUCache.java

License:Open Source License

@SuppressWarnings("unchecked")
private static CompilationUnit createCompilationUnit(String unitName, char[] javaSource) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);

    Map<String, String> options = JavaCore.getOptions();

    JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options);

    parser.setCompilerOptions(options);//  ww w . j  a  v  a  2s .co  m

    //setUnitName for resolve bindings
    parser.setUnitName(unitName);

    String[] sources = { "" };
    String[] classpath = { "" };
    //setEnvironment for resolve bindings even if the args is empty
    parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true);

    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    parser.setBindingsRecovery(true);
    parser.setSource(javaSource);
    parser.setIgnoreMethodBodies(false);

    return (CompilationUnit) parser.createAST(null);
}

From source file:com.liferay.blade.eclipse.provider.CUCacheJDT.java

License:Open Source License

@SuppressWarnings("unchecked")
private CompilationUnit createCompilationUnit(String unitName, char[] javaSource) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);

    Map<String, String> options = JavaCore.getOptions();

    JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options);

    parser.setCompilerOptions(options);// www.  j a  v a  2s .c  om

    //setUnitName for resolve bindings
    parser.setUnitName(unitName);

    String[] sources = { "" };
    String[] classpath = { "" };
    //setEnvironment for resolve bindings even if the args is empty
    parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true);

    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    parser.setBindingsRecovery(true);
    parser.setSource(javaSource);
    parser.setIgnoreMethodBodies(false);

    return (CompilationUnit) parser.createAST(null);
}