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