DocTrees.java revision 3060:23f76aadbb36
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.util.List;
29
30import javax.annotation.processing.ProcessingEnvironment;
31import javax.lang.model.element.Element;
32import javax.tools.Diagnostic;
33import javax.tools.JavaCompiler.CompilationTask;
34
35import com.sun.source.doctree.DocCommentTree;
36import com.sun.source.doctree.DocTree;
37
38/**
39 * Provides access to syntax trees for doc comments.
40 *
41 * @since 1.8
42 */
43@jdk.Exported
44public abstract class DocTrees extends Trees {
45    /**
46     * Returns a DocTrees object for a given CompilationTask.
47     * @param task the compilation task for which to get the Trees object
48     * @return the DocTrees object
49     * @throws IllegalArgumentException if the task does not support the Trees API.
50     */
51    public static DocTrees instance(CompilationTask task) {
52        return (DocTrees) Trees.instance(task);
53    }
54
55    /**
56     * Returns a DocTrees object for a given ProcessingEnvironment.
57     * @param env the processing environment for which to get the Trees object
58     * @return the DocTrees object
59     * @throws IllegalArgumentException if the env does not support the Trees API.
60     */
61    public static DocTrees instance(ProcessingEnvironment env) {
62        if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
63            throw new IllegalArgumentException();
64        return (DocTrees) getJavacTrees(ProcessingEnvironment.class, env);
65    }
66
67    /**
68     * Returns the doc comment tree, if any, for the Tree node identified by a given TreePath.
69     * Returns {@code null} if no doc comment was found.
70     * @param path the path for the tree node
71     * @return the doc comment tree
72     */
73    public abstract DocCommentTree getDocCommentTree(TreePath path);
74
75    /**
76     * Returns the language model element referred to by the leaf node of the given
77     * {@link DocTreePath}, or {@code null} if unknown.
78     * @param path the path for the tree node
79     * @return the element
80     */
81    public abstract Element getElement(DocTreePath path);
82
83    /**
84     * Returns the list of {@link DocTree} representing the first sentence of
85     * a comment.
86     *
87     * @param list the DocTree list to interrogate
88     * @return the first sentence
89     *
90     * @since 1.9
91     */
92    public abstract List<DocTree> getFirstSentence(List<? extends DocTree> list);
93
94    /**
95     * Returns a utility object for accessing the source positions
96     * of documentation tree nodes.
97     * @return the utility object
98     */
99    public abstract DocSourcePositions getSourcePositions();
100
101    /**
102     * Prints a message of the specified kind at the location of the
103     * tree within the provided compilation unit
104     *
105     * @param kind the kind of message
106     * @param msg  the message, or an empty string if none
107     * @param t    the tree to use as a position hint
108     * @param c    the doc comment tree to use as a position hint
109     * @param root the compilation unit that contains tree
110     */
111    public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
112            com.sun.source.doctree.DocTree t,
113            com.sun.source.doctree.DocCommentTree c,
114            com.sun.source.tree.CompilationUnitTree root);
115}
116