1 /* 2 * Copyright (c) 2011, 2020, 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 26 package com.sun.source.util; 27 28 import java.io.IOException; 29 import java.text.BreakIterator; 30 import java.util.List; 31 32 import javax.annotation.processing.ProcessingEnvironment; 33 import javax.lang.model.element.Element; 34 import javax.lang.model.element.PackageElement; 35 import javax.lang.model.type.TypeMirror; 36 import javax.tools.Diagnostic; 37 import javax.tools.FileObject; 38 import javax.tools.JavaCompiler.CompilationTask; 39 40 import com.sun.source.doctree.DocCommentTree; 41 import com.sun.source.doctree.DocTree; 42 43 /** 44 * Provides access to syntax trees for doc comments. 45 * 46 * @since 1.8 47 */ 48 public abstract class DocTrees extends Trees { 49 /** 50 * Constructor for subclasses to call. 51 */ 52 public DocTrees() {} 53 54 /** 55 * Returns a DocTrees object for a given CompilationTask. 56 * @param task the compilation task for which to get the Trees object 57 * @return the DocTrees object 58 * @throws IllegalArgumentException if the task does not support the Trees API. 59 */ 60 public static DocTrees instance(CompilationTask task) { 61 return (DocTrees) Trees.instance(task); 62 } 63 64 /** 65 * Returns a DocTrees object for a given ProcessingEnvironment. 66 * @param env the processing environment for which to get the Trees object 67 * @return the DocTrees object 68 * @throws IllegalArgumentException if the env does not support the Trees API. 69 */ 70 public static DocTrees instance(ProcessingEnvironment env) { 71 if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) 72 throw new IllegalArgumentException(); 73 return (DocTrees) getJavacTrees(ProcessingEnvironment.class, env); 74 } 75 76 /** 77 * Returns the break iterator used to compute the first sentence of 78 * documentation comments. 79 * Returns {@code null} if none has been specified. 80 * @return the break iterator 81 * 82 * @since 9 83 */ 84 public abstract BreakIterator getBreakIterator(); 85 86 /** 87 * Returns the doc comment tree, if any, for the Tree node identified by a given TreePath. 88 * Returns {@code null} if no doc comment was found. 89 * @param path the path for the tree node 90 * @return the doc comment tree 91 */ 92 public abstract DocCommentTree getDocCommentTree(TreePath path); 93 94 /** 95 * Returns the doc comment tree of the given element. 96 * Returns {@code null} if no doc comment was found. 97 * @param e an element whose documentation is required 98 * @return the doc comment tree 99 * 100 * @since 9 101 */ 102 public abstract DocCommentTree getDocCommentTree(Element e); 103 104 /** 105 * Returns the doc comment tree of the given file. The file must be 106 * an HTML file, in which case the doc comment tree represents the 107 * entire contents of the file. 108 * Returns {@code null} if no doc comment was found. 109 * Future releases may support additional file types. 110 * 111 * @param fileObject the content container 112 * @return the doc comment tree 113 * @since 9 114 */ 115 public abstract DocCommentTree getDocCommentTree(FileObject fileObject); 116 117 /** 118 * Returns the doc comment tree of the given file whose path is 119 * specified relative to the given element. The file must be an HTML 120 * file, in which case the doc comment tree represents the contents 121 * of the <body> tag, and any enclosing tags are ignored. 122 * Returns {@code null} if no doc comment was found. 123 * Future releases may support additional file types. 124 * 125 * @param e an element whose path is used as a reference 126 * @param relativePath the relative path from the Element 127 * @return the doc comment tree 128 * @throws java.io.IOException if an exception occurs 129 * 130 * @since 9 131 */ 132 public abstract DocCommentTree getDocCommentTree(Element e, String relativePath) throws IOException; 133 134 /** 135 * Returns a doc tree path containing the doc comment tree of the given file. 136 * The file must be an HTML file, in which case the doc comment tree represents 137 * the contents of the {@code <body>} tag, and any enclosing tags are ignored. 138 * Any references to source code elements contained in {@code @see} and 139 * {@code {@link}} tags in the doc comment tree will be evaluated in the 140 * context of the given package element. 141 * Returns {@code null} if no doc comment was found. 142 * 143 * @param fileObject a file object encapsulating the HTML content 144 * @param packageElement a package element to associate with the given file object 145 * representing a legacy package.html, null otherwise 146 * @return a doc tree path containing the doc comment parsed from the given file 147 * @throws IllegalArgumentException if the fileObject is not an HTML file 148 * 149 * @since 9 150 */ 151 public abstract DocTreePath getDocTreePath(FileObject fileObject, PackageElement packageElement); 152 153 /** 154 * Returns the language model element referred to by the leaf node of the given 155 * {@link DocTreePath}, or {@code null} if unknown. 156 * @param path the path for the tree node 157 * @return the element 158 */ 159 public abstract Element getElement(DocTreePath path); 160 161 /** 162 * Returns the language model type referred to by the leaf node of the given 163 * {@link DocTreePath}, or {@code null} if unknown. This method usually 164 * returns the same value as {@code getElement(path).asType()} for a 165 * {@code path} argument for which {@link #getElement(DocTreePath)} returns 166 * a non-null value, but may return a type that includes additional 167 * information, such as a parameterized generic type instead of a raw type. 168 * 169 * @param path the path for the tree node 170 * @return the referenced type, or null 171 * 172 * @since 15 173 */ 174 public abstract TypeMirror getType(DocTreePath path); 175 176 /** 177 * Returns the list of {@link DocTree} representing the first sentence of 178 * a comment. 179 * 180 * @param list the DocTree list to interrogate 181 * @return the first sentence 182 * 183 * @since 9 184 */ 185 public abstract List<DocTree> getFirstSentence(List<? extends DocTree> list); 186 187 /** 188 * Returns a utility object for accessing the source positions 189 * of documentation tree nodes. 190 * @return the utility object 191 */ 192 public abstract DocSourcePositions getSourcePositions(); 193 194 /** 195 * Prints a message of the specified kind at the location of the 196 * tree within the provided compilation unit 197 * 198 * @param kind the kind of message 199 * @param msg the message, or an empty string if none 200 * @param t the tree to use as a position hint 201 * @param c the doc comment tree to use as a position hint 202 * @param root the compilation unit that contains tree 203 */ 204 public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg, 205 com.sun.source.doctree.DocTree t, 206 com.sun.source.doctree.DocCommentTree c, 207 com.sun.source.tree.CompilationUnitTree root); 208 209 /** 210 * Sets the break iterator to compute the first sentence of 211 * documentation comments. 212 * @param breakiterator a break iterator or {@code null} to specify the default 213 * sentence breaker 214 * 215 * @since 9 216 */ 217 public abstract void setBreakIterator(BreakIterator breakiterator); 218 219 /** 220 * Returns a utility object for creating {@code DocTree} objects. 221 * @return a utility object for creating {@code DocTree} objects 222 * 223 * @since 9 224 */ 225 public abstract DocTreeFactory getDocTreeFactory(); 226 }