langtools/src/share/classes/com/sun/source/util/TreePath.java
author jjg
Tue, 19 Mar 2013 15:13:44 -0700
changeset 16559 e927a4e65b0c
parent 16303 b5dca0b42963
child 18664 5c35de99f6df
permissions -rw-r--r--
8010315: doclint errors in javac public API Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
16303
b5dca0b42963 8008267: Add @Supported annotation to com.sun.source types
darcy
parents: 14952
diff changeset
     2
 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.source.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.Iterator;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * A path of tree nodes, typically used to represent the sequence of ancestor
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * nodes of a tree node up to the top level CompilationUnitTree node.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * @author Jonathan Gibbons
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * @since 1.6
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 */
16303
b5dca0b42963 8008267: Add @Supported annotation to com.sun.source types
darcy
parents: 14952
diff changeset
    39
@jdk.Supported
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class TreePath implements Iterable<Tree> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
     * Gets a tree path for a tree node within a compilation unit.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
     * @return null if the node is not found
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    public static TreePath getPath(CompilationUnitTree unit, Tree target) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        return getPath(new TreePath(unit), target);
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
     * Gets a tree path for a tree node within a subtree identified by a TreePath object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
     * @return null if the node is not found
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public static TreePath getPath(TreePath path, Tree target) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        path.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        target.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        class Result extends Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            static final long serialVersionUID = -5942088234594905625L;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            TreePath path;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
            Result(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
                this.path = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        }
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    64
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        class PathFinder extends TreePathScanner<TreePath,Tree> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            public TreePath scan(Tree tree, Tree target) {
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    67
                if (tree == target) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                    throw new Result(new TreePath(getCurrentPath(), target));
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    69
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                return super.scan(tree, target);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    74
        if (path.getLeaf() == target) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    75
            return path;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    76
        }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14258
diff changeset
    77
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            new PathFinder().scan(path, target);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        } catch (Result result) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            return result.path;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * Creates a TreePath for a root node.
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    public TreePath(CompilationUnitTree t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        this(null, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * Creates a TreePath for a child node.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public TreePath(TreePath p, Tree t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        if (t.getKind() == Tree.Kind.COMPILATION_UNIT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            compilationUnit = (CompilationUnitTree) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            parent = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            compilationUnit = p.compilationUnit;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            parent = p;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        leaf = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Get the compilation unit associated with this path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public CompilationUnitTree getCompilationUnit() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        return compilationUnit;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * Get the leaf node for this path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    public Tree getLeaf() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        return leaf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     * Get the path for the enclosing node, or null if there is no enclosing node.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    public TreePath getParentPath() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        return parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    public Iterator<Tree> iterator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        return new Iterator<Tree>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            public boolean hasNext() {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 10
diff changeset
   131
                return next != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            public Tree next() {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 10
diff changeset
   135
                Tree t = next.leaf;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 10
diff changeset
   136
                next = next.parent;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 10
diff changeset
   137
                return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            public void remove() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 10
diff changeset
   144
            private TreePath next = TreePath.this;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    private CompilationUnitTree compilationUnit;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    private Tree leaf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    private TreePath parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
}