src/share/classes/com/sun/source/util/DocTreePath.java

changeset 1726
a7ff36d06fa2
child 2083
379c04c090cf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/source/util/DocTreePath.java	Mon May 06 16:22:45 2013 +0200
     1.3 @@ -0,0 +1,171 @@
     1.4 +/*
     1.5 + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.source.util;
    1.30 +
    1.31 +import com.sun.source.doctree.DocCommentTree;
    1.32 +import com.sun.source.doctree.DocTree;
    1.33 +import java.util.Iterator;
    1.34 +
    1.35 +/**
    1.36 + * A path of tree nodes, typically used to represent the sequence of ancestor
    1.37 + * nodes of a tree node up to the top level DocCommentTree node.
    1.38 + *
    1.39 + * @since 1.8
    1.40 + */
    1.41 +@jdk.Supported
    1.42 +public class DocTreePath implements Iterable<DocTree> {
    1.43 +    /**
    1.44 +     * Gets a documentation tree path for a tree node within a compilation unit.
    1.45 +     * @return null if the node is not found
    1.46 +     */
    1.47 +    public static DocTreePath getPath(TreePath treePath, DocCommentTree doc, DocTree target) {
    1.48 +        return getPath(new DocTreePath(treePath, doc), target);
    1.49 +    }
    1.50 +
    1.51 +    /**
    1.52 +     * Gets a documentation tree path for a tree node within a subtree identified by a DocTreePath object.
    1.53 +     * @return null if the node is not found
    1.54 +     */
    1.55 +    public static DocTreePath getPath(DocTreePath path, DocTree target) {
    1.56 +        path.getClass();
    1.57 +        target.getClass();
    1.58 +
    1.59 +        class Result extends Error {
    1.60 +            static final long serialVersionUID = -5942088234594905625L;
    1.61 +            DocTreePath path;
    1.62 +            Result(DocTreePath path) {
    1.63 +                this.path = path;
    1.64 +            }
    1.65 +        }
    1.66 +
    1.67 +        class PathFinder extends DocTreePathScanner<DocTreePath,DocTree> {
    1.68 +            public DocTreePath scan(DocTree tree, DocTree target) {
    1.69 +                if (tree == target) {
    1.70 +                    throw new Result(new DocTreePath(getCurrentPath(), target));
    1.71 +                }
    1.72 +                return super.scan(tree, target);
    1.73 +            }
    1.74 +        }
    1.75 +
    1.76 +        if (path.getLeaf() == target) {
    1.77 +            return path;
    1.78 +        }
    1.79 +
    1.80 +        try {
    1.81 +            new PathFinder().scan(path, target);
    1.82 +        } catch (Result result) {
    1.83 +            return result.path;
    1.84 +        }
    1.85 +        return null;
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * Creates a DocTreePath for a root node.
    1.90 +     *
    1.91 +     * @param treePath the TreePath from which the root node was created.
    1.92 +     * @param t the DocCommentTree to create the path for.
    1.93 +     */
    1.94 +    public DocTreePath(TreePath treePath, DocCommentTree t) {
    1.95 +        treePath.getClass();
    1.96 +        t.getClass();
    1.97 +
    1.98 +        this.treePath = treePath;
    1.99 +        this.docComment = t;
   1.100 +        this.parent = null;
   1.101 +        this.leaf = t;
   1.102 +    }
   1.103 +
   1.104 +    /**
   1.105 +     * Creates a DocTreePath for a child node.
   1.106 +     */
   1.107 +    public DocTreePath(DocTreePath p, DocTree t) {
   1.108 +        if (t.getKind() == DocTree.Kind.DOC_COMMENT) {
   1.109 +            throw new IllegalArgumentException("Use DocTreePath(TreePath, DocCommentTree) to construct DocTreePath for a DocCommentTree.");
   1.110 +        } else {
   1.111 +            treePath = p.treePath;
   1.112 +            docComment = p.docComment;
   1.113 +            parent = p;
   1.114 +        }
   1.115 +        leaf = t;
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Get the TreePath associated with this path.
   1.120 +     * @return TreePath for this DocTreePath
   1.121 +     */
   1.122 +    public TreePath getTreePath() {
   1.123 +        return treePath;
   1.124 +    }
   1.125 +
   1.126 +    /**
   1.127 +     * Get the DocCommentTree associated with this path.
   1.128 +     * @return DocCommentTree for this DocTreePath
   1.129 +     */
   1.130 +    public DocCommentTree getDocComment() {
   1.131 +        return docComment;
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Get the leaf node for this path.
   1.136 +     * @return DocTree for this DocTreePath
   1.137 +     */
   1.138 +    public DocTree getLeaf() {
   1.139 +        return leaf;
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * Get the path for the enclosing node, or null if there is no enclosing node.
   1.144 +     * @return DocTreePath of parent
   1.145 +     */
   1.146 +    public DocTreePath getParentPath() {
   1.147 +        return parent;
   1.148 +    }
   1.149 +
   1.150 +    public Iterator<DocTree> iterator() {
   1.151 +        return new Iterator<DocTree>() {
   1.152 +            public boolean hasNext() {
   1.153 +                return next != null;
   1.154 +            }
   1.155 +
   1.156 +            public DocTree next() {
   1.157 +                DocTree t = next.leaf;
   1.158 +                next = next.parent;
   1.159 +                return t;
   1.160 +            }
   1.161 +
   1.162 +            public void remove() {
   1.163 +                throw new UnsupportedOperationException();
   1.164 +            }
   1.165 +
   1.166 +            private DocTreePath next = DocTreePath.this;
   1.167 +        };
   1.168 +    }
   1.169 +
   1.170 +    private final TreePath treePath;
   1.171 +    private final DocCommentTree docComment;
   1.172 +    private final DocTree leaf;
   1.173 +    private final DocTreePath parent;
   1.174 +}

mercurial