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

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

Introduction

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

Prototype

public void setFocalPosition(int position) 

Source Link

Document

Requests an abridged abstract syntax tree.

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;//from   www .  j a  v a 2  s  . co 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.google.gdt.eclipse.appengine.rpc.util.CompilationUnitCreator.java

License:Open Source License

private CompilationUnit createASTForImports(ICompilationUnit cu) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(cu);//from w w  w.ja v a 2s. c o  m
    parser.setResolveBindings(false);
    parser.setFocalPosition(0);
    return (CompilationUnit) parser.createAST(null);
}

From source file:com.redhat.ceylon.eclipse.code.wizard.ClassPathDetector.java

License:Open Source License

private void visitCompilationUnit(IFile file) {
    ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
    if (cu != null) {
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setSource(cu);/*from  w ww  . ja v  a  2 s  .c o  m*/
        parser.setFocalPosition(0);
        CompilationUnit root = (CompilationUnit) parser.createAST(null);
        PackageDeclaration packDecl = root.getPackage();

        IPath packPath = file.getParent().getFullPath();
        String cuName = file.getName();
        if (packDecl == null) {
            addToMap(fSourceFolders, packPath, new Path(cuName));
        } else {
            IPath relPath = new Path(packDecl.getName().getFullyQualifiedName().replace('.', '/'));
            IPath folderPath = getFolderPath(packPath, relPath);
            if (folderPath != null) {
                addToMap(fSourceFolders, folderPath, relPath.append(cuName));
            }
        }
    }
}

From source file:org.dyno.visual.swing.parser.DefaultSourceParser.java

License:Open Source License

public static ImportRewrite createImportRewrite(ICompilationUnit unit) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(unit);//from w w w . java  2 s. c  om
    parser.setResolveBindings(false);
    parser.setFocalPosition(0);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    return CodeStyleConfiguration.createImportRewrite(cu, true);
}

From source file:org.eclipse.ajdt.internal.ui.wizards.NewTypeWizardPage.java

License:Open Source License

private CompilationUnit createASTForImports(ICompilationUnit cu) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setSource(cu);//from   w ww .  j a va 2  s  .  com
    parser.setResolveBindings(false);
    parser.setFocalPosition(0);
    return (CompilationUnit) parser.createAST(null);
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ASTHelper.java

License:Open Source License

private static CompilationUnit createASTForImports(ICompilationUnit cu) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(cu);//ww  w . j  av  a 2 s .c  o m
    parser.setResolveBindings(false);
    parser.setFocalPosition(0);
    return (CompilationUnit) parser.createAST(null);
}

From source file:org.eclipse.objectteams.otdt.internal.ui.wizards.typecreation.TypeCreator.java

License:Open Source License

private CompilationUnit createASTForImports(ICompilationUnit cu) {
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setSource(cu);/*from   w ww  .j a  v  a 2  s.c om*/
    parser.setResolveBindings(false);
    parser.setFocalPosition(0);
    return (CompilationUnit) parser.createAST(null);
}

From source file:org.eclipse.pde.api.tools.internal.builder.BaseApiAnalyzer.java

License:Open Source License

/**
 * Creates and AST for the given {@link ITypeRoot} at the given offset
 * //w  w  w. ja v a  2 s  . c  om
 * @param root
 * @param offset
 * @return
 */
private CompilationUnit createAST(ITypeRoot root, int offset) {
    if (fJavaProject == null) {
        return null;
    }
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setFocalPosition(offset);
    parser.setResolveBindings(false);
    parser.setSource(root);
    Map<String, String> options = fJavaProject.getOptions(true);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
    parser.setCompilerOptions(options);
    return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}

From source file:org.eclipse.pde.api.tools.ui.internal.markers.JavadocTagOperation.java

License:Open Source License

/**
 * Creates an {@link AST} with the given {@link ICompilationUnit} as source
 * and the {@link JavaCore#COMPILER_DOC_COMMENT_SUPPORT} options set.
 * /*from  w  ww .j av  a2 s.co  m*/
 * @param unit
 * @param focalposition
 * @param resolvebindings
 * @return a new {@link AST}
 */
protected CompilationUnit createAST(final ICompilationUnit unit, int focalposition, boolean resolvebindings) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setSource(unit);
    parser.setFocalPosition(focalposition);
    parser.setResolveBindings(resolvebindings);
    Map<String, String> options = unit.getJavaProject().getOptions(true);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
    parser.setCompilerOptions(options);
    return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}

From source file:org.eclipse.pde.api.tools.ui.internal.markers.RemoveUnsupportedAnnotationOperation.java

License:Open Source License

@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
    SubMonitor localMonitor = SubMonitor.convert(monitor,
            MarkerMessages.RemoveUnsupportedTagOperation_removeing_unsupported_tag, fMarkers.length + 6);
    HashMap<ICompilationUnit, Boolean> seen = new HashMap<ICompilationUnit, Boolean>();
    for (int i = 0; i < fMarkers.length; i++) {
        // retrieve the AST node compilation unit
        IResource resource = fMarkers[i].getResource();
        IJavaElement javaElement = JavaCore.create(resource);
        try {// w  w  w.  ja  v  a  2s  . c  o m
            if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
                ICompilationUnit compilationUnit = (ICompilationUnit) javaElement;
                if (!seen.containsKey(compilationUnit)) {
                    seen.put(compilationUnit, Boolean.valueOf(compilationUnit.hasUnsavedChanges()));
                }
                if (!compilationUnit.isWorkingCopy()) {
                    // open an editor of the corresponding unit to "show"
                    // the quick-fix change
                    JavaUI.openInEditor(compilationUnit);
                }
                if (!compilationUnit.isConsistent()) {
                    compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, null);
                    Util.updateMonitor(localMonitor, 1);
                }
                Util.updateMonitor(localMonitor, 1);
                ASTParser parser = ASTParser.newParser(AST.JLS8);
                parser.setSource(compilationUnit);
                Integer charStartAttribute = null;
                charStartAttribute = (Integer) fMarkers[i].getAttribute(IMarker.CHAR_START);
                int intValue = charStartAttribute.intValue();
                parser.setFocalPosition(intValue);
                final CompilationUnit unit = (CompilationUnit) parser.createAST(new NullProgressMonitor());
                AnnotationFinder finder = new AnnotationFinder(intValue);
                unit.accept(finder);
                Util.updateMonitor(localMonitor, 1);
                if (finder.fNode != null) {
                    unit.recordModifications();
                    AST ast = unit.getAST();
                    ASTRewrite rewrite = ASTRewrite.create(ast);
                    TextEditGroup group = new TextEditGroup("Removing API tools annotations"); //$NON-NLS-1$
                    rewrite.remove(finder.fNode, group);
                    Util.updateMonitor(localMonitor, 1);
                    TextEdit edit = rewrite.rewriteAST();
                    compilationUnit.applyTextEdit(edit, monitor);
                    Util.updateMonitor(localMonitor, 1);
                }
            }
        } catch (JavaModelException jme) {
        } catch (PartInitException e) {
        } catch (CoreException e) {
        }
    }
    // try saving the compilation units if they were in a saved state when
    // the quick-fix started
    for (Entry<ICompilationUnit, Boolean> entry : seen.entrySet()) {
        if (!entry.getValue().booleanValue()) {
            try {
                entry.getKey().commitWorkingCopy(true, null);
            } catch (JavaModelException jme) {
            }
        }
    }
    return Status.OK_STATUS;
}