DocTrees.java revision 3193:3b3bea483542
1/*
2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.source.util;
27
28import java.io.IOException;
29import java.text.BreakIterator;
30import java.util.List;
31
32import javax.annotation.processing.ProcessingEnvironment;
33import javax.lang.model.element.Element;
34import javax.tools.Diagnostic;
35import javax.tools.FileObject;
36import javax.tools.JavaCompiler.CompilationTask;
37
38import com.sun.source.doctree.DocCommentTree;
39import com.sun.source.doctree.DocTree;
40
41/**
42 * Provides access to syntax trees for doc comments.
43 *
44 * @since 1.8
45 */
46public abstract class DocTrees extends Trees {
47    /**
48     * Returns a DocTrees object for a given CompilationTask.
49     * @param task the compilation task for which to get the Trees object
50     * @return the DocTrees object
51     * @throws IllegalArgumentException if the task does not support the Trees API.
52     */
53    public static DocTrees instance(CompilationTask task) {
54        return (DocTrees) Trees.instance(task);
55    }
56
57    /**
58     * Returns a DocTrees object for a given ProcessingEnvironment.
59     * @param env the processing environment for which to get the Trees object
60     * @return the DocTrees object
61     * @throws IllegalArgumentException if the env does not support the Trees API.
62     */
63    public static DocTrees instance(ProcessingEnvironment env) {
64        if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
65            throw new IllegalArgumentException();
66        return (DocTrees) getJavacTrees(ProcessingEnvironment.class, env);
67    }
68
69    /**
70     * Returns the break iterator used to compute the first sentence of
71     * documentation comments.
72     * Returns {@code null} if none has been specified.
73     * @return the break iterator
74     *
75     * @since 9
76     */
77    public abstract BreakIterator getBreakIterator();
78
79    /**
80     * Returns the doc comment tree, if any, for the Tree node identified by a given TreePath.
81     * Returns {@code null} if no doc comment was found.
82     * @param path the path for the tree node
83     * @return the doc comment tree
84     */
85    public abstract DocCommentTree getDocCommentTree(TreePath path);
86
87    /**
88     * Returns the doc comment tree of the given element.
89     * Returns {@code null} if no doc comment was found.
90     * @param e an element whose documentation is required
91     * @return the doc comment tree
92     *
93     * @since 9
94     */
95    public abstract DocCommentTree getDocCommentTree(Element e);
96
97    /**
98     * Returns the doc comment tree of the given file. The file must be
99     * an HTML file, in which case the doc comment tree represents the
100     * contents of the <body> tag, and any enclosing tags are ignored.
101     * Returns {@code null} if no doc comment was found.
102     * Future releases may support additional file types.
103     *
104     * @param fileObject the content container
105     * @return the doc comment tree
106     *
107     * @since 9
108     */
109    public abstract DocCommentTree getDocCommentTree(FileObject fileObject);
110
111    /**
112     * Returns the doc comment tree of the given file whose path is
113     * specified relative to the given element. The file must be an HTML
114     * file, in which case the doc comment tree represents the contents
115     * of the <body> tag, and any enclosing tags are ignored.
116     * Returns {@code null} if no doc comment was found.
117     * Future releases may support additional file types.
118     *
119     * @param e an element whose path is used as a reference
120     * @param relativePath the relative path from the Element
121     * @return the doc comment tree
122     * @throws java.io.IOException if an exception occurs
123     *
124     * @since 9
125     */
126    public abstract DocCommentTree getDocCommentTree(Element e, String relativePath) throws IOException;
127
128    /**
129     * Returns the language model element referred to by the leaf node of the given
130     * {@link DocTreePath}, or {@code null} if unknown.
131     * @param path the path for the tree node
132     * @return the element
133     */
134    public abstract Element getElement(DocTreePath path);
135
136    /**
137     * Returns the list of {@link DocTree} representing the first sentence of
138     * a comment.
139     *
140     * @param list the DocTree list to interrogate
141     * @return the first sentence
142     *
143     * @since 9
144     */
145    public abstract List<DocTree> getFirstSentence(List<? extends DocTree> list);
146
147    /**
148     * Returns a utility object for accessing the source positions
149     * of documentation tree nodes.
150     * @return the utility object
151     */
152    public abstract DocSourcePositions getSourcePositions();
153
154    /**
155     * Prints a message of the specified kind at the location of the
156     * tree within the provided compilation unit
157     *
158     * @param kind the kind of message
159     * @param msg  the message, or an empty string if none
160     * @param t    the tree to use as a position hint
161     * @param c    the doc comment tree to use as a position hint
162     * @param root the compilation unit that contains tree
163     */
164    public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
165            com.sun.source.doctree.DocTree t,
166            com.sun.source.doctree.DocCommentTree c,
167            com.sun.source.tree.CompilationUnitTree root);
168
169    /**
170     * Sets the break iterator to compute the first sentence of
171     * documentation comments.
172     * @param breakiterator a break iterator or {@code null} to specify the default
173     *                      sentence breaker
174     *
175     * @since 9
176     */
177    public abstract void setBreakIterator(BreakIterator breakiterator);
178}
179