1 /*
   2  * Copyright (c) 2011, 2012, 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 javax.annotation.processing.ProcessingEnvironment;
  29 import javax.lang.model.element.Element;
  30 import javax.tools.JavaCompiler.CompilationTask;
  31 
  32 import com.sun.source.doctree.DocCommentTree;
  33 import com.sun.source.doctree.ReferenceTree;
  34 import javax.tools.Diagnostic;
  35 
  36 /**
  37  * Provides access to syntax trees for doc comments.
  38  *
  39  * @since 1.8
  40  */
  41 public abstract class DocTrees extends Trees {
  42     /**
  43      * Gets a DocTrees object for a given CompilationTask.
  44      * @param task the compilation task for which to get the Trees object
  45      * @throws IllegalArgumentException if the task does not support the Trees API.
  46      */
  47     public static DocTrees instance(CompilationTask task) {
  48         return (DocTrees) Trees.instance(task);
  49     }
  50 
  51     /**
  52      * Gets a DocTrees object for a given ProcessingEnvironment.
  53      * @param env the processing environment for which to get the Trees object
  54      * @throws IllegalArgumentException if the env does not support the Trees API.
  55      */
  56     public static DocTrees instance(ProcessingEnvironment env) {
  57         if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
  58             throw new IllegalArgumentException();
  59         return (DocTrees) getJavacTrees(ProcessingEnvironment.class, env);
  60     }
  61 
  62     /**
  63      * Gets the doc comment tree, if any, for the Tree node identified by a given TreePath.
  64      * Returns null if no doc comment was found.
  65      */
  66     public abstract DocCommentTree getDocCommentTree(TreePath path);
  67 
  68     /**
  69      * Gets the language model element referred to by a ReferenceTree that
  70      * appears on the declaration identified by the given path.
  71      */
  72     public abstract Element getElement(TreePath path, ReferenceTree reference);
  73 
  74     /**
  75      * Prints a message of the specified kind at the location of the
  76      * tree within the provided compilation unit
  77      *
  78      * @param kind the kind of message
  79      * @param msg  the message, or an empty string if none
  80      * @param t    the tree to use as a position hint
  81      * @param root the compilation unit that contains tree
  82      */
  83     public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
  84             com.sun.source.doctree.DocTree t,
  85             com.sun.source.doctree.DocCommentTree c,
  86             com.sun.source.tree.CompilationUnitTree root);
  87 }