Example usage for org.eclipse.jdt.core.dom ASTNode accept

List of usage examples for org.eclipse.jdt.core.dom ASTNode accept

Introduction

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

Prototype

public final void accept(ASTVisitor visitor) 

Source Link

Document

Accepts the given visitor on a visit of the current node.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

public static String asString(ASTNode node) {
    Assert.isTrue(node.getAST().apiLevel() == ASTProvider.SHARED_AST_LEVEL);

    ASTFlattener flattener = new ASTFlattener();
    node.accept(flattener);
    return flattener.getResult();
}

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

/**
 * Appends the text representation of the given modifier flags, followed by a single space.
 * Used for 3.0 modifiers and annotations.
 *
 * @param ext the list of modifier and annotation nodes
 * (element type: <code>IExtendedModifier</code>)
 *///w  ww .  j  a v a 2 s. c o  m
private void printModifiers(List<IExtendedModifier> ext) {
    for (Iterator<IExtendedModifier> it = ext.iterator(); it.hasNext();) {
        ASTNode p = (ASTNode) it.next();
        p.accept(this);
        this.fBuffer.append(" ");//$NON-NLS-1$
    }
}

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(Javadoc node) {
    this.fBuffer.append("/** ");//$NON-NLS-1$
    for (Iterator<TagElement> it = node.tags().iterator(); it.hasNext();) {
        ASTNode e = it.next();
        e.accept(this);
    }/*from   ww w .j  a  v a  2  s  .  c om*/
    this.fBuffer.append("\n */");//$NON-NLS-1$
    return false;
}

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }//from  w w  w. j  a va  2s .  co  m
    if (node.getAST().apiLevel() >= JLS3) {
        printModifiers(node.modifiers());
        if (!node.typeParameters().isEmpty()) {
            this.fBuffer.append("<");//$NON-NLS-1$
            for (Iterator<TypeParameter> it = node.typeParameters().iterator(); it.hasNext();) {
                TypeParameter t = it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.fBuffer.append(", ");//$NON-NLS-1$
                }
            }
            this.fBuffer.append("> ");//$NON-NLS-1$
        }
    }
    if (!node.isConstructor()) {
        if (node.getReturnType2() != null) {
            node.getReturnType2().accept(this);
        } else {
            // methods really ought to have a return type
            this.fBuffer.append("void");//$NON-NLS-1$
        }
        this.fBuffer.append(" ");//$NON-NLS-1$
    }
    node.getName().accept(this);
    this.fBuffer.append("(");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= AST.JLS8) {
        Type receiverType = node.getReceiverType();
        if (receiverType != null) {
            receiverType.accept(this);
            this.fBuffer.append(' ');
            SimpleName qualifier = node.getReceiverQualifier();
            if (qualifier != null) {
                qualifier.accept(this);
                this.fBuffer.append('.');
            }
            this.fBuffer.append("this"); //$NON-NLS-1$
            if (node.parameters().size() > 0) {
                this.fBuffer.append(',');
            }
        }
    }
    for (Iterator<SingleVariableDeclaration> it = node.parameters().iterator(); it.hasNext();) {
        SingleVariableDeclaration v = it.next();
        v.accept(this);
        if (it.hasNext()) {
            this.fBuffer.append(", ");//$NON-NLS-1$
        }
    }
    this.fBuffer.append(")");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= AST.JLS8) {
        List<Dimension> dimensions = node.extraDimensions();
        for (Iterator<Dimension> it = dimensions.iterator(); it.hasNext();) {
            Dimension e = it.next();
            e.accept(this);
        }
    } else {
        for (int i = 0; i < node.getExtraDimensions(); i++) {
            this.fBuffer.append("[]"); //$NON-NLS-1$
        }
    }
    List<? extends ASTNode> thrownExceptions = node.getAST().apiLevel() >= AST.JLS8
            ? node.thrownExceptionTypes()
            : getThrownExceptions(node);
    if (!thrownExceptions.isEmpty()) {
        this.fBuffer.append(" throws ");//$NON-NLS-1$
        for (Iterator<? extends ASTNode> it = thrownExceptions.iterator(); it.hasNext();) {
            ASTNode n = it.next();
            n.accept(this);
            if (it.hasNext()) {
                this.fBuffer.append(", ");//$NON-NLS-1$
            }
        }
        this.fBuffer.append(" ");//$NON-NLS-1$
    }
    if (node.getBody() == null) {
        this.fBuffer.append(";");//$NON-NLS-1$
    } else {
        node.getBody().accept(this);
    }
    return false;
}

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(TagElement node) {
    if (node.isNested()) {
        // nested tags are always enclosed in braces
        this.fBuffer.append("{");//$NON-NLS-1$
    } else {/*from w  w w  .j  a v  a2 s. c  o  m*/
        // top-level tags always begin on a new line
        this.fBuffer.append("\n * ");//$NON-NLS-1$
    }
    boolean previousRequiresWhiteSpace = false;
    if (node.getTagName() != null) {
        this.fBuffer.append(node.getTagName());
        previousRequiresWhiteSpace = true;
    }
    boolean previousRequiresNewLine = false;
    for (Iterator<? extends ASTNode> it = node.fragments().iterator(); it.hasNext();) {
        ASTNode e = it.next();
        // assume text elements include necessary leading and trailing whitespace
        // but Name, MemberRef, MethodRef, and nested TagElement do not include white space
        boolean currentIncludesWhiteSpace = (e instanceof TextElement);
        if (previousRequiresNewLine && currentIncludesWhiteSpace) {
            this.fBuffer.append("\n * ");//$NON-NLS-1$
        }
        previousRequiresNewLine = currentIncludesWhiteSpace;
        // add space if required to separate
        if (previousRequiresWhiteSpace && !currentIncludesWhiteSpace) {
            this.fBuffer.append(" "); //$NON-NLS-1$
        }
        e.accept(this);
        previousRequiresWhiteSpace = !currentIncludesWhiteSpace && !(e instanceof TagElement);
    }
    if (node.isNested()) {
        this.fBuffer.append("}");//$NON-NLS-1$
    }
    return false;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.actions.PPAOnCuAction.java

License:Open Source License

public void run(IAction action) {
    if (icu == null) {
        return;/*from  w ww  .  ja  v  a2  s. c o m*/
    }

    try {
        MessageConsole mConsole = new MessageConsole("PPA Console", null);
        final PrintStream printer = new PrintStream(mConsole.newMessageStream());
        IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
        manager.addConsoles(new IConsole[] { mConsole });
        manager.showConsoleView(mConsole);

        ProgressMonitorDialog dialog = new ProgressMonitorDialog(
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
        dialog.run(true, false, new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    monitor.beginTask("Visiting AST", 100);
                    ASTNode node = PPAUtil.getCU((IFile) icu.getResource(), new PPAOptions());
                    monitor.worked(50);
                    node.accept(new NameBindingVisitor(printer, monitor));
                    printer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    monitor.done();
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.ASTUTils.java

License:Open Source License

public static MethodDeclaration findMethodDeclaration(ASTNode rootNode, IMethod method) {
    if (rootNode != null) {
        ISourceRange range;//from   ww  w.  ja v a 2s. co  m
        try {
            range = method.getSourceRange();
            NodeFinder finder = new NodeFinder(range.getOffset(), range.getLength());
            rootNode.accept(finder);
            return (MethodDeclaration) finder.getCoveredNode();
        } catch (JavaModelException e) {
        }
    }
    return null;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.NodeFinder.java

License:Open Source License

/**
 * A visitor that maps a selection to a given ASTNode. The result node is
 * determined as follows://ww w . j a  v a 2s  .  c  om
 * <ul>
 *   <li>first the visitor tries to find a node with the exact start and length</li>
 *     <li>if no such node exists than the node that encloses the range defined by
 *       start and end is returned.</li>
 *   <li>if the length is zero than also nodes are considered where the node's
 *       start or end position matches <code>start</code>.</li>
 *   <li>otherwise <code>null</code> is returned.</li>
 * </ul>
 * 
 * @param root the root node from which the search starts
 * @param start the start offset
 * @param length the length
 * 
 * @return the result node
 * 
 * @since      2.1
 */
public static ASTNode perform(ASTNode root, int start, int length) {
    NodeFinder finder = new NodeFinder(start, length);
    root.accept(finder);
    ASTNode result = finder.getCoveredNode();
    if (result == null || result.getStartPosition() != start || result.getLength() != length) {
        return finder.getCoveringNode();
    }
    return result;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.NodeFinder.java

License:Open Source License

/**
 * A visitor that maps a selection to a given ASTNode. The result node is
 * determined as follows:/*from  w  w  w.j a  v a 2 s  . c o m*/
 * <ul>
 *   <li>first the visitor tries to find a node that is covered by <code>start</code> and
 *       <code>length</code> where either <code>start</code> and <code>length</code> exactly
 *       matches the node or where the text covered before and after the node only consists
 *       of white spaces or comments.</li>
 *     <li>if no such node exists than the node that encloses the range defined by
 *       start and end is returned.</li>
 *   <li>if the length is zero than also nodes are considered where the node's
 *       start or end position matches <code>start</code>.</li>
 *   <li>otherwise <code>null</code> is returned.</li>
 * </ul>
 * 
 * @param root the root node from which the search starts
 * @param start the start offset
 * @param length the length
 * @param source the source of the compilation unit
 * 
 * @return the result node
 * @throws JavaModelException if an error occurs in the Java model
 * 
 * @since      3.0
 */
public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException {
    NodeFinder finder = new NodeFinder(start, length);
    root.accept(finder);
    ASTNode result = finder.getCoveredNode();
    if (result == null)
        return null;
    Selection selection = Selection.createFromStartLength(start, length);
    if (selection.covers(result)) {
        IBuffer buffer = source.getBuffer();
        if (buffer != null) {
            IScanner scanner = ToolFactory.createScanner(false, false, false, false);
            scanner.setSource(buffer.getText(start, length).toCharArray());
            try {
                int token = scanner.getNextToken();
                if (token != ITerminalSymbols.TokenNameEOF) {
                    int tStart = scanner.getCurrentTokenStartPosition();
                    if (tStart == result.getStartPosition() - start) {
                        scanner.resetTo(tStart + result.getLength(), length - 1);
                        token = scanner.getNextToken();
                        if (token == ITerminalSymbols.TokenNameEOF)
                            return result;
                    }
                }
            } catch (InvalidInputException e) {
            }
        }
    }
    return finder.getCoveringNode();
}

From source file:ca.uvic.chisel.javasketch.internal.ast.groups.ASTLoopGroupCalculator.java

License:Open Source License

/**
 * Clears the current grouping for the block node, and all of its children.
 * @param blockNode/*from w  ww  .j  a v  a  2s.c om*/
 * @param child
 * @param i
 * @param groupings
 * @return
 */
private static ASTMessageGroupingTree resetGrouping(ASTNode blockNode) {
    ASTNode loop = findLoopingParent(blockNode);
    if (loop != null) {
        ASTMessageGroupingTree loopGroup = (ASTMessageGroupingTree) loop.getProperty(CURRENT_GROUPING);
        int currentIteration = loopGroup.getIteration();
        //clear the children
        loop.accept(new GenericVisitor() {
            /* (non-Javadoc)
             * @see ca.uvic.chisel.javasketch.internal.ast.groups.GenericVisitor#visitNode(org.eclipse.jdt.core.dom.ASTNode)
             */
            @Override
            protected boolean visitNode(ASTNode node) {
                node.setProperty(CURRENT_GROUPING, null);
                return true;
            }
        });
        //create a new group for the loop
        ASTMessageGroupingTree newLoop = getGrouping(loop);
        newLoop.setIteration(currentIteration + 1);
        return getGrouping(blockNode);
    }
    return (ASTMessageGroupingTree) blockNode.getProperty(CURRENT_GROUPING);
}