Path: blob/master/src/jdk.compiler/share/classes/com/sun/source/util/DocTrees.java
41175 views
/*1* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.source.util;2627import java.io.IOException;28import java.text.BreakIterator;29import java.util.List;3031import javax.annotation.processing.ProcessingEnvironment;32import javax.lang.model.element.Element;33import javax.lang.model.element.PackageElement;34import javax.lang.model.type.TypeMirror;35import javax.tools.Diagnostic;36import javax.tools.FileObject;37import javax.tools.JavaCompiler.CompilationTask;3839import com.sun.source.doctree.DocCommentTree;40import com.sun.source.doctree.DocTree;41import com.sun.source.doctree.EntityTree;42import com.sun.source.tree.CompilationUnitTree;4344/**45* Provides access to syntax trees for doc comments.46*47* @since 1.848*/49public abstract class DocTrees extends Trees {50/**51* Constructor for subclasses to call.52*/53public DocTrees() {}5455/**56* Returns a DocTrees object for a given CompilationTask.57* @param task the compilation task for which to get the Trees object58* @return the DocTrees object59* @throws IllegalArgumentException if the task does not support the Trees API.60*/61public static DocTrees instance(CompilationTask task) {62return (DocTrees) Trees.instance(task);63}6465/**66* Returns a DocTrees object for a given ProcessingEnvironment.67* @param env the processing environment for which to get the Trees object68* @return the DocTrees object69* @throws IllegalArgumentException if the env does not support the Trees API.70*/71public static DocTrees instance(ProcessingEnvironment env) {72if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))73throw new IllegalArgumentException();74return (DocTrees) getJavacTrees(ProcessingEnvironment.class, env);75}7677/**78* Returns the break iterator used to compute the first sentence of79* documentation comments.80* Returns {@code null} if none has been specified.81* @return the break iterator82*83* @since 984*/85public abstract BreakIterator getBreakIterator();8687/**88* Returns the doc comment tree, if any, for the Tree node identified by a given TreePath.89* Returns {@code null} if no doc comment was found.90* @param path the path for the tree node91* @return the doc comment tree92*/93public abstract DocCommentTree getDocCommentTree(TreePath path);9495/**96* Returns the doc comment tree of the given element.97* Returns {@code null} if no doc comment was found.98* @param e an element whose documentation is required99* @return the doc comment tree100*101* @since 9102*/103public abstract DocCommentTree getDocCommentTree(Element e);104105/**106* Returns the doc comment tree of the given file. The file must be107* an HTML file, in which case the doc comment tree represents the108* entire contents of the file.109* Returns {@code null} if no doc comment was found.110* Future releases may support additional file types.111*112* @param fileObject the content container113* @return the doc comment tree114* @since 9115*/116public abstract DocCommentTree getDocCommentTree(FileObject fileObject);117118/**119* Returns the doc comment tree of the given file whose path is120* specified relative to the given element. The file must be an HTML121* file, in which case the doc comment tree represents the contents122* of the <body> tag, and any enclosing tags are ignored.123* Returns {@code null} if no doc comment was found.124* Future releases may support additional file types.125*126* @param e an element whose path is used as a reference127* @param relativePath the relative path from the Element128* @return the doc comment tree129* @throws java.io.IOException if an exception occurs130*131* @since 9132*/133public abstract DocCommentTree getDocCommentTree(Element e, String relativePath) throws IOException;134135/**136* Returns a doc tree path containing the doc comment tree of the given file.137* The file must be an HTML file, in which case the doc comment tree represents138* the contents of the {@code <body>} tag, and any enclosing tags are ignored.139* Any references to source code elements contained in {@code @see} and140* {@code {@link}} tags in the doc comment tree will be evaluated in the141* context of the given package element.142* Returns {@code null} if no doc comment was found.143*144* @param fileObject a file object encapsulating the HTML content145* @param packageElement a package element to associate with the given file object146* representing a legacy package.html, null otherwise147* @return a doc tree path containing the doc comment parsed from the given file148* @throws IllegalArgumentException if the fileObject is not an HTML file149*150* @since 9151*/152public abstract DocTreePath getDocTreePath(FileObject fileObject, PackageElement packageElement);153154/**155* Returns the language model element referred to by the leaf node of the given156* {@link DocTreePath}, or {@code null} if unknown.157* @param path the path for the tree node158* @return the element159*/160public abstract Element getElement(DocTreePath path);161162/**163* Returns the language model type referred to by the leaf node of the given164* {@link DocTreePath}, or {@code null} if unknown. This method usually165* returns the same value as {@code getElement(path).asType()} for a166* {@code path} argument for which {@link #getElement(DocTreePath)} returns167* a non-null value, but may return a type that includes additional168* information, such as a parameterized generic type instead of a raw type.169*170* @param path the path for the tree node171* @return the referenced type, or null172*173* @since 15174*/175public abstract TypeMirror getType(DocTreePath path);176177/**178* Returns the list of {@link DocTree} representing the first sentence of179* a comment.180*181* @param list the DocTree list to interrogate182* @return the first sentence183*184* @since 9185*/186public abstract List<DocTree> getFirstSentence(List<? extends DocTree> list);187188/**189* Returns a utility object for accessing the source positions190* of documentation tree nodes.191* @return the utility object192*/193public abstract DocSourcePositions getSourcePositions();194195/**196* Prints a message of the specified kind at the location of the197* tree within the provided compilation unit198*199* @param kind the kind of message200* @param msg the message, or an empty string if none201* @param t the tree to use as a position hint202* @param c the doc comment tree to use as a position hint203* @param root the compilation unit that contains tree204*/205public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,206DocTree t, DocCommentTree c, CompilationUnitTree root);207208/**209* Sets the break iterator to compute the first sentence of210* documentation comments.211* @param breakIterator a break iterator or {@code null} to specify the default212* sentence breaker213*214* @since 9215*/216public abstract void setBreakIterator(BreakIterator breakIterator);217218/**219* Returns a utility object for creating {@code DocTree} objects.220* @return a utility object for creating {@code DocTree} objects221*222* @since 9223*/224public abstract DocTreeFactory getDocTreeFactory();225226/**227* Returns a string containing the characters for the entity in a given entity tree,228* or {@code null} if the tree does not represent a valid series of characters.229*230* <p>The interpretation of entities is based on section231* <a href="https://www.w3.org/TR/html52/syntax.html#character-references">8.1.4. Character references</a>232* in the HTML 5.2 specification.</p>233*234* @param tree the tree containing the entity235* @return a string containing the characters236*/237public abstract String getCharacters(EntityTree tree);238}239240241