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