Example usage for org.eclipse.jdt.core.dom NodeFinder getCoveringNode

List of usage examples for org.eclipse.jdt.core.dom NodeFinder getCoveringNode

Introduction

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

Prototype

public ASTNode getCoveringNode() 

Source Link

Document

Returns the innermost node that fully contains the selection.

Usage

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

License:Open Source License

private void doLinkWithEditor(ISelection selection) {
    ITextSelection textSelection = (ITextSelection) selection;
    int offset = textSelection.getOffset();
    int length = textSelection.getLength();

    NodeFinder finder = new NodeFinder(offset, length);
    fRoot.accept(finder);/*from ww  w . j  ava2 s.  c o m*/
    ASTNode covering = finder.getCoveringNode();
    if (covering != null) {
        fViewer.reveal(covering);
        fViewer.setSelection(new StructuredSelection(covering));
    }
}

From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

/**
 * Adds any applicable suppress lint fix resolutions into the given list
 *
 * @param marker the marker to create fixes for
 * @param id the issue id/*from   w ww .  j ava 2  s  .  c o m*/
 * @param resolutions a list to add the created resolutions into, if any
 */
public static void createFixes(IMarker marker, String id, List<IMarkerResolution> resolutions) {
    ITextEditor textEditor = AdtUtils.getActiveTextEditor();
    IDocumentProvider provider = textEditor.getDocumentProvider();
    IEditorInput editorInput = textEditor.getEditorInput();
    IDocument document = provider.getDocument(editorInput);
    if (document == null) {
        return;
    }

    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    ICompilationUnit compilationUnit = manager.getWorkingCopy(editorInput);
    int offset = 0;
    int length = 0;
    int start = marker.getAttribute(IMarker.CHAR_START, -1);
    int end = marker.getAttribute(IMarker.CHAR_END, -1);
    offset = start;
    length = end - start;
    CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
    if (root == null) {
        return;
    }

    int api = -1;
    if (id.equals(ApiDetector.UNSUPPORTED.getId()) || id.equals(ApiDetector.INLINED.getId())) {
        String message = marker.getAttribute(IMarker.MESSAGE, null);
        if (message != null) {
            Pattern pattern = Pattern.compile("\\s(\\d+)\\s"); //$NON-NLS-1$
            Matcher matcher = pattern.matcher(message);
            if (matcher.find()) {
                api = Integer.parseInt(matcher.group(1));
            }
        }
    }

    Issue issue = EclipseLintClient.getRegistry().getIssue(id);
    boolean isClassDetector = issue != null && issue.getImplementation().getScope().contains(Scope.CLASS_FILE);

    // Don't offer to suppress (with an annotation) the annotation checks
    if (issue == AnnotationDetector.ISSUE) {
        return;
    }

    NodeFinder nodeFinder = new NodeFinder(root, offset, length);
    ASTNode coveringNode;
    if (offset <= 0) {
        // Error added on the first line of a Java class: typically from a class-based
        // detector which lacks line information. Map this to the top level class
        // in the file instead.
        coveringNode = root;
        if (root.types() != null && root.types().size() > 0) {
            Object type = root.types().get(0);
            if (type instanceof ASTNode) {
                coveringNode = (ASTNode) type;
            }
        }
    } else {
        coveringNode = nodeFinder.getCoveringNode();
    }
    for (ASTNode body = coveringNode; body != null; body = body.getParent()) {
        if (body instanceof BodyDeclaration) {
            BodyDeclaration declaration = (BodyDeclaration) body;

            String target = null;
            if (body instanceof MethodDeclaration) {
                target = ((MethodDeclaration) body).getName().toString() + "()"; //$NON-NLS-1$
            } else if (body instanceof FieldDeclaration) {
                target = "field";
                FieldDeclaration field = (FieldDeclaration) body;
                if (field.fragments() != null && field.fragments().size() > 0) {
                    ASTNode first = (ASTNode) field.fragments().get(0);
                    if (first instanceof VariableDeclarationFragment) {
                        VariableDeclarationFragment decl = (VariableDeclarationFragment) first;
                        target = decl.getName().toString();
                    }
                }
            } else if (body instanceof AnonymousClassDeclaration) {
                target = "anonymous class";
            } else if (body instanceof TypeDeclaration) {
                target = ((TypeDeclaration) body).getName().toString();
            } else {
                target = body.getClass().getSimpleName();
            }

            // In class files, detectors can only find annotations on methods
            // and on classes, not on variable declarations
            if (isClassDetector && !(body instanceof MethodDeclaration || body instanceof TypeDeclaration
                    || body instanceof AnonymousClassDeclaration || body instanceof FieldDeclaration)) {
                continue;
            }

            String desc = String.format("Add @SuppressLint '%1$s\' to '%2$s'", id, target);
            resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, null));

            if (api != -1
                    // @TargetApi is only valid on methods and classes, not fields etc
                    && (body instanceof MethodDeclaration || body instanceof TypeDeclaration)) {
                String apiString = SdkVersionInfo.getBuildCode(api);
                if (apiString == null) {
                    apiString = Integer.toString(api);
                }
                desc = String.format("Add @TargetApi(%1$s) to '%2$s'", apiString, target);
                resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, apiString));
            }
        }
    }
}

From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

/**
 * Adds any applicable suppress lint fix resolutions into the given list
 *
 * @param marker the marker to create fixes for
 * @param id the issue id//from   w  w  w .  j  a v a 2s .  c  om
 * @param resolutions a list to add the created resolutions into, if any
 */
public static void createFixes(IMarker marker, String id, List<IMarkerResolution> resolutions) {
    ITextEditor textEditor = AdtUtils.getActiveTextEditor();
    IDocumentProvider provider = textEditor.getDocumentProvider();
    IEditorInput editorInput = textEditor.getEditorInput();
    IDocument document = provider.getDocument(editorInput);
    if (document == null) {
        return;
    }
    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    ICompilationUnit compilationUnit = manager.getWorkingCopy(editorInput);
    int offset = 0;
    int length = 0;
    int start = marker.getAttribute(IMarker.CHAR_START, -1);
    int end = marker.getAttribute(IMarker.CHAR_END, -1);
    offset = start;
    length = end - start;
    CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
    if (root == null) {
        return;
    }

    int api = -1;
    if (id.equals(ApiDetector.UNSUPPORTED.getId())) {
        String message = marker.getAttribute(IMarker.MESSAGE, null);
        if (message != null) {
            Pattern pattern = Pattern.compile("\\s(\\d+)\\s"); //$NON-NLS-1$
            Matcher matcher = pattern.matcher(message);
            if (matcher.find()) {
                api = Integer.parseInt(matcher.group(1));
            }
        }
    }

    Issue issue = EclipseLintClient.getRegistry().getIssue(id);
    boolean isClassDetector = issue != null && issue.getScope().contains(Scope.CLASS_FILE);

    NodeFinder nodeFinder = new NodeFinder(root, offset, length);
    ASTNode coveringNode;
    if (offset <= 0) {
        // Error added on the first line of a Java class: typically from a class-based
        // detector which lacks line information. Map this to the top level class
        // in the file instead.
        coveringNode = root;
        if (root.types() != null && root.types().size() > 0) {
            Object type = root.types().get(0);
            if (type instanceof ASTNode) {
                coveringNode = (ASTNode) type;
            }
        }
    } else {
        coveringNode = nodeFinder.getCoveringNode();
    }
    for (ASTNode body = coveringNode; body != null; body = body.getParent()) {
        if (body instanceof BodyDeclaration) {
            BodyDeclaration declaration = (BodyDeclaration) body;

            String target = null;
            if (body instanceof MethodDeclaration) {
                target = ((MethodDeclaration) body).getName().toString() + "()"; //$NON-NLS-1$
            } else if (body instanceof FieldDeclaration) {
                target = "field";
                FieldDeclaration field = (FieldDeclaration) body;
                if (field.fragments() != null && field.fragments().size() > 0) {
                    ASTNode first = (ASTNode) field.fragments().get(0);
                    if (first instanceof VariableDeclarationFragment) {
                        VariableDeclarationFragment decl = (VariableDeclarationFragment) first;
                        target = decl.getName().toString();
                    }
                }
            } else if (body instanceof AnonymousClassDeclaration) {
                target = "anonymous class";
            } else if (body instanceof TypeDeclaration) {
                target = ((TypeDeclaration) body).getName().toString();
            } else {
                target = body.getClass().getSimpleName();
            }

            // In class files, detectors can only find annotations on methods
            // and on classes, not on variable declarations
            if (isClassDetector && !(body instanceof MethodDeclaration || body instanceof TypeDeclaration
                    || body instanceof AnonymousClassDeclaration || body instanceof FieldDeclaration)) {
                continue;
            }

            String desc = String.format("Add @SuppressLint '%1$s\' to '%2$s'", id, target);
            resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, -1));

            if (api != -1
                    // @TargetApi is only valid on methods and classes, not fields etc
                    && (body instanceof MethodDeclaration || body instanceof TypeDeclaration)) {
                desc = String.format("Add @TargetApi(%1$d) to '%2$s'", api, target);
                resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, api));
            }
        }
    }
}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureRefactoring.java

License:Open Source License

private MethodDeclaration locateSelectedMethod() {
    NodeFinder nodeFinder = new NodeFinder(fRoot, fSelectionStart, fSelectionLength);
    ASTNode coveringNode = nodeFinder.getCoveringNode();
    MethodDeclaration methodDeclaration = (MethodDeclaration) ASTNodes.getParent(coveringNode,
            MethodDeclaration.class);
    return methodDeclaration;
}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureRefactoring.java

License:Open Source License

private Statement locateEnclosingLoopStatement() {
    NodeFinder nodeFinder = new NodeFinder(fRoot, fSelectionStart, fSelectionLength);
    ASTNode node = nodeFinder.getCoveringNode();
    do {/*from   ww  w  . j  av  a  2 s  . com*/
        node = node.getParent();
    } while (node != null && !(node instanceof EnhancedForStatement || node instanceof ForStatement));
    return (Statement) node;
}

From source file:org.autorefactor.refactoring.rules.CommentsRefactoring.java

License:Open Source License

private ASTNode getNextNode(Comment node) {
    final int nodeEndPosition = node.getStartPosition() + node.getLength();
    final ASTNode coveringNode = getCoveringNode(node);
    final int parentNodeEndPosition = coveringNode.getStartPosition() + coveringNode.getLength();
    final NodeFinder finder = new NodeFinder(coveringNode, nodeEndPosition,
            parentNodeEndPosition - nodeEndPosition);
    if (node instanceof Javadoc) {
        return finder.getCoveringNode();
    }//from   w ww . ja v  a2 s.co m
    return finder.getCoveredNode();
}

From source file:org.autorefactor.refactoring.rules.CommentsRefactoring.java

License:Open Source License

private ASTNode getCoveringNode(int start, int length) {
    final NodeFinder finder = new NodeFinder(this.astRoot, start, length);
    return finder.getCoveringNode();
}

From source file:org.eclim.plugin.jdt.command.correct.ProblemLocation.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww w . j  av  a2  s  . c o m*/
 */
public ASTNode getCoveringNode(CompilationUnit astRoot) {
    NodeFinder finder = new NodeFinder(astRoot, offset, length);
    return finder.getCoveringNode();
}

From source file:org.eclim.plugin.jdt.util.ASTUtils.java

License:Open Source License

/**
 * Finds the node at the specified offset.
 *
 * @param cu The CompilationUnit.//  w ww.  jav a 2  s .  c o m
 * @param offset The node offset in the compilation unit.
 * @return The node at the specified offset.
 */
public static ASTNode findNode(CompilationUnit cu, int offset) throws Exception {
    NodeFinder finder = new NodeFinder(cu, offset, 1);
    //return finder.getCoveredNode();
    return finder.getCoveringNode();
}

From source file:org.eclipse.andmore.internal.lint.AddSuppressAnnotation.java

License:Open Source License

/**
 * Adds any applicable suppress lint fix resolutions into the given list
 *
 * @param marker the marker to create fixes for
 * @param id the issue id/*from   ww w  . j  a  v  a2  s  .c o m*/
 * @param resolutions a list to add the created resolutions into, if any
 */
public static void createFixes(IMarker marker, String id, List<IMarkerResolution> resolutions) {
    ITextEditor textEditor = AdtUtils.getActiveTextEditor();
    IDocumentProvider provider = textEditor.getDocumentProvider();
    IEditorInput editorInput = textEditor.getEditorInput();
    IDocument document = provider.getDocument(editorInput);
    if (document == null) {
        return;
    }

    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    ICompilationUnit compilationUnit = manager.getWorkingCopy(editorInput);
    int offset = 0;
    int length = 0;
    int start = marker.getAttribute(IMarker.CHAR_START, -1);
    int end = marker.getAttribute(IMarker.CHAR_END, -1);
    offset = start;
    length = end - start;
    CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
    if (root == null) {
        return;
    }

    int api = -1;
    if (id.equals(ApiDetector.UNSUPPORTED.getId()) || id.equals(ApiDetector.INLINED.getId())) {
        String message = marker.getAttribute(IMarker.MESSAGE, null);
        if (message != null) {
            Pattern pattern = Pattern.compile("\\s(\\d+)\\s"); //$NON-NLS-1$
            Matcher matcher = pattern.matcher(message);
            if (matcher.find()) {
                api = Integer.parseInt(matcher.group(1));
            }
        }
    }

    Issue issue = EclipseLintClient.getRegistry().getIssue(id);
    boolean isClassDetector = issue != null && issue.getImplementation().getScope().contains(Scope.CLASS_FILE);

    // Don't offer to suppress (with an annotation) the annotation checks
    if (issue == AnnotationDetector.INSIDE_METHOD) {
        return;
    }

    NodeFinder nodeFinder = new NodeFinder(root, offset, length);
    ASTNode coveringNode;
    if (offset <= 0) {
        // Error added on the first line of a Java class: typically from a class-based
        // detector which lacks line information. Map this to the top level class
        // in the file instead.
        coveringNode = root;
        if (root.types() != null && root.types().size() > 0) {
            Object type = root.types().get(0);
            if (type instanceof ASTNode) {
                coveringNode = (ASTNode) type;
            }
        }
    } else {
        coveringNode = nodeFinder.getCoveringNode();
    }
    for (ASTNode body = coveringNode; body != null; body = body.getParent()) {
        if (body instanceof BodyDeclaration) {
            BodyDeclaration declaration = (BodyDeclaration) body;

            String target = null;
            if (body instanceof MethodDeclaration) {
                target = ((MethodDeclaration) body).getName().toString() + "()"; //$NON-NLS-1$
            } else if (body instanceof FieldDeclaration) {
                target = "field";
                FieldDeclaration field = (FieldDeclaration) body;
                if (field.fragments() != null && field.fragments().size() > 0) {
                    ASTNode first = (ASTNode) field.fragments().get(0);
                    if (first instanceof VariableDeclarationFragment) {
                        VariableDeclarationFragment decl = (VariableDeclarationFragment) first;
                        target = decl.getName().toString();
                    }
                }
            } else if (body instanceof AnonymousClassDeclaration) {
                target = "anonymous class";
            } else if (body instanceof TypeDeclaration) {
                target = ((TypeDeclaration) body).getName().toString();
            } else {
                target = body.getClass().getSimpleName();
            }

            // In class files, detectors can only find annotations on methods
            // and on classes, not on variable declarations
            if (isClassDetector && !(body instanceof MethodDeclaration || body instanceof TypeDeclaration
            //                            || body instanceof AnonymousClassDeclaration
                    || body instanceof FieldDeclaration)) {
                continue;
            }

            String desc = String.format("Add @SuppressLint '%1$s\' to '%2$s'", id, target);
            resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, null));

            if (api != -1
                    // @TargetApi is only valid on methods and classes, not fields etc
                    && (body instanceof MethodDeclaration || body instanceof TypeDeclaration)) {
                String apiString = SdkVersionInfo.getBuildCode(api);
                if (apiString == null) {
                    apiString = Integer.toString(api);
                }
                desc = String.format("Add @TargetApi(%1$s) to '%2$s'", apiString, target);
                resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, apiString));
            }
        }
    }
}