1 /*
   2  * Copyright (c) 2005, 2014, 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.lang.reflect.Method;
  29 
  30 import javax.annotation.processing.ProcessingEnvironment;
  31 import javax.lang.model.element.AnnotationMirror;
  32 import javax.lang.model.element.AnnotationValue;
  33 import javax.lang.model.element.Element;
  34 import javax.lang.model.element.ExecutableElement;
  35 import javax.lang.model.element.TypeElement;
  36 import javax.lang.model.type.DeclaredType;
  37 import javax.lang.model.type.ErrorType;
  38 import javax.lang.model.type.TypeMirror;
  39 import javax.tools.Diagnostic;
  40 import javax.tools.JavaCompiler.CompilationTask;
  41 
  42 import com.sun.source.tree.CatchTree;
  43 import com.sun.source.tree.ClassTree;
  44 import com.sun.source.tree.CompilationUnitTree;
  45 import com.sun.source.tree.MethodTree;
  46 import com.sun.source.tree.Scope;
  47 import com.sun.source.tree.Tree;
  48 
  49 /**
  50  * Bridges JSR 199, JSR 269, and the Tree API.
  51  *
  52  * @author Peter von der Ahé
  53  */
  54 public abstract class Trees {
  55     /**
  56      * Constructor for subclasses to call.
  57      */
  58     public Trees() {}
  59 
  60     /**
  61      * Returns a Trees object for a given CompilationTask.
  62      * @param task the compilation task for which to get the Trees object
  63      * @throws IllegalArgumentException if the task does not support the Trees API.
  64      * @return the Trees object
  65      */
  66     public static Trees instance(CompilationTask task) {
  67         String taskClassName = task.getClass().getName();
  68         if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl")
  69                 && !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask"))
  70             throw new IllegalArgumentException();
  71         return getJavacTrees(CompilationTask.class, task);
  72     }
  73 
  74     /**
  75      * Returns a Trees object for a given ProcessingEnvironment.
  76      * @param env the processing environment for which to get the Trees object
  77      * @throws IllegalArgumentException if the env does not support the Trees API.
  78      * @return the Trees object
  79      */
  80     public static Trees instance(ProcessingEnvironment env) {
  81         if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
  82             throw new IllegalArgumentException();
  83         return getJavacTrees(ProcessingEnvironment.class, env);
  84     }
  85 
  86     static Trees getJavacTrees(Class<?> argType, Object arg) {
  87         try {
  88             ClassLoader cl = arg.getClass().getClassLoader();
  89             Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
  90             argType = Class.forName(argType.getName(), false, cl);
  91             Method m = c.getMethod("instance", argType);
  92             return (Trees) m.invoke(null, arg);
  93         } catch (ReflectiveOperationException e) {
  94             throw new AssertionError(e);
  95         }
  96     }
  97 
  98     /**
  99      * Returns a utility object for obtaining source positions.
 100      * @return the utility object for obtaining source positions
 101      */
 102     public abstract SourcePositions getSourcePositions();
 103 
 104     /**
 105      * Returns the Tree node for a given Element.
 106      * Returns {@code null} if the node can not be found.
 107      * @param element the element
 108      * @return the tree node
 109      */
 110     public abstract Tree getTree(Element element);
 111 
 112     /**
 113      * Returns the ClassTree node for a given TypeElement.
 114      * Returns {@code null} if the node can not be found.
 115      * @param element the element
 116      * @return the class tree node
 117      */
 118     public abstract ClassTree getTree(TypeElement element);
 119 
 120     /**
 121      * Returns the MethodTree node for a given ExecutableElement.
 122      * Returns {@code null} if the node can not be found.
 123      * @param method the executable element
 124      * @return the method tree node
 125      */
 126     public abstract MethodTree getTree(ExecutableElement method);
 127 
 128     /**
 129      * Returns the Tree node for an AnnotationMirror on a given Element.
 130      * Returns {@code null} if the node can not be found.
 131      * @param e the element
 132      * @param a the annotation mirror
 133      * @return the tree node
 134      */
 135     public abstract Tree getTree(Element e, AnnotationMirror a);
 136 
 137     /**
 138      * Returns the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
 139      * Returns {@code null} if the node can not be found.
 140      * @param e the element
 141      * @param a the annotation mirror
 142      * @param v the annotation value
 143      * @return the tree node
 144      */
 145     public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
 146 
 147     /**
 148      * Returns the path to tree node within the specified compilation unit.
 149      * @param unit the compilation unit
 150      * @param node the tree node
 151      * @return the tree path
 152      */
 153     public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
 154 
 155     /**
 156      * Returns the TreePath node for a given Element.
 157      * Returns {@code null} if the node can not be found.
 158      * @param e the element
 159      * @return the tree path
 160      */
 161     public abstract TreePath getPath(Element e);
 162 
 163     /**
 164      * Returns the TreePath node for an AnnotationMirror on a given Element.
 165      * Returns {@code null} if the node can not be found.
 166      * @param e the element
 167      * @param a the annotation mirror
 168      * @return the tree path
 169      */
 170     public abstract TreePath getPath(Element e, AnnotationMirror a);
 171 
 172     /**
 173      * Returns the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
 174      * Returns {@code null} if the node can not be found.
 175      * @param e the element
 176      * @param a the annotation mirror
 177      * @param v the annotation value
 178      * @return the tree path
 179      */
 180     public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
 181 
 182     /**
 183      * Returns the Element for the Tree node identified by a given TreePath.
 184      * Returns {@code null} if the element is not available.
 185      * @param path the tree path
 186      * @return the element
 187      * @throws IllegalArgumentException is the TreePath does not identify
 188      * a Tree node that might have an associated Element.
 189      */
 190     public abstract Element getElement(TreePath path);
 191 
 192     /**
 193      * Returns the TypeMirror for the Tree node identified by a given TreePath.
 194      * Returns {@code null} if the TypeMirror is not available.
 195      * @param path the tree path
 196      * @return the type mirror
 197      * @throws IllegalArgumentException is the TreePath does not identify
 198      * a Tree node that might have an associated TypeMirror.
 199      */
 200     public abstract TypeMirror getTypeMirror(TreePath path);
 201 
 202     /**
 203      * Returns the Scope for the Tree node identified by a given TreePath.
 204      * Returns {@code null} if the Scope is not available.
 205      * @param path the tree path
 206      * @return the scope
 207      */
 208     public abstract Scope getScope(TreePath path);
 209 
 210     /**
 211      * Returns the doc comment, if any, for the Tree node identified by a given TreePath.
 212      * Returns {@code null} if no doc comment was found.
 213      * @see DocTrees#getDocCommentTree(TreePath)
 214      * @param path the tree path
 215      * @return the doc comment
 216      */
 217     public abstract String getDocComment(TreePath path);
 218 
 219     /**
 220      * Checks whether a given type is accessible in a given scope.
 221      * @param scope the scope to be checked
 222      * @param type the type to be checked
 223      * @return true if {@code type} is accessible
 224      */
 225     public abstract boolean isAccessible(Scope scope, TypeElement type);
 226 
 227     /**
 228      * Checks whether the given element is accessible as a member of the given
 229      * type in a given scope.
 230      * @param scope the scope to be checked
 231      * @param member the member to be checked
 232      * @param type the type for which to check if the member is accessible
 233      * @return true if {@code member} is accessible in {@code type}
 234      */
 235     public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
 236 
 237     /**
 238       * Returns the original type from the ErrorType object.
 239       * @param errorType The errorType for which we want to get the original type.
 240       * @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
 241       */
 242     public abstract TypeMirror getOriginalType(ErrorType errorType);
 243 
 244     /**
 245      * Prints a message of the specified kind at the location of the
 246      * tree within the provided compilation unit
 247      *
 248      * @param kind the kind of message
 249      * @param msg  the message, or an empty string if none
 250      * @param t    the tree to use as a position hint
 251      * @param root the compilation unit that contains tree
 252      */
 253     public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
 254             com.sun.source.tree.Tree t,
 255             com.sun.source.tree.CompilationUnitTree root);
 256 
 257     /**
 258      * Returns the lub of an exception parameter declared in a catch clause.
 259      * @param tree the tree for the catch clause
 260      * @return The lub of the exception parameter
 261      */
 262     public abstract TypeMirror getLub(CatchTree tree);
 263 }