author | alanb |
Fri, 08 Jan 2016 11:47:12 +0000 | |
changeset 34916 | 16043fc1d90b |
parent 25874 | 83c19f00452c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
25287 | 2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.source.util; |
|
27 |
||
28 |
import java.lang.reflect.Method; |
|
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
9599
diff
changeset
|
29 |
|
10 | 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; |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
37 |
import javax.lang.model.type.ErrorType; |
10 | 38 |
import javax.lang.model.type.TypeMirror; |
3149 | 39 |
import javax.tools.Diagnostic; |
10 | 40 |
import javax.tools.JavaCompiler.CompilationTask; |
41 |
||
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
42 |
import com.sun.source.tree.CatchTree; |
10 | 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 |
/** |
|
25287 | 56 |
* Returns a Trees object for a given CompilationTask. |
7633
52b637b3b246
6986242: cut-n-paste error in javadoc for Trees.instance(ProcessingEnvironment)
jjg
parents:
7631
diff
changeset
|
57 |
* @param task the compilation task for which to get the Trees object |
10 | 58 |
* @throws IllegalArgumentException if the task does not support the Trees API. |
25287 | 59 |
* @return the Trees object |
10 | 60 |
*/ |
61 |
public static Trees instance(CompilationTask task) { |
|
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14541
diff
changeset
|
62 |
String taskClassName = task.getClass().getName(); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14541
diff
changeset
|
63 |
if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl") |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14541
diff
changeset
|
64 |
&& !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask")) |
10 | 65 |
throw new IllegalArgumentException(); |
66 |
return getJavacTrees(CompilationTask.class, task); |
|
67 |
} |
|
68 |
||
69 |
/** |
|
25287 | 70 |
* Returns a Trees object for a given ProcessingEnvironment. |
7633
52b637b3b246
6986242: cut-n-paste error in javadoc for Trees.instance(ProcessingEnvironment)
jjg
parents:
7631
diff
changeset
|
71 |
* @param env the processing environment for which to get the Trees object |
10 | 72 |
* @throws IllegalArgumentException if the env does not support the Trees API. |
25287 | 73 |
* @return the Trees object |
10 | 74 |
*/ |
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 |
||
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
9599
diff
changeset
|
81 |
static Trees getJavacTrees(Class<?> argType, Object arg) { |
10 | 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); |
|
22163 | 86 |
Method m = c.getMethod("instance", argType); |
87 |
return (Trees) m.invoke(null, arg); |
|
25288
5234a53b53a8
8048162: Restrict catch type from Throwable to ReflectiveOperationException
jjg
parents:
25287
diff
changeset
|
88 |
} catch (ReflectiveOperationException e) { |
10 | 89 |
throw new AssertionError(e); |
90 |
} |
|
91 |
} |
|
92 |
||
93 |
/** |
|
25287 | 94 |
* Returns a utility object for obtaining source positions. |
95 |
* @return the utility object for obtaining source positions |
|
10 | 96 |
*/ |
97 |
public abstract SourcePositions getSourcePositions(); |
|
98 |
||
99 |
/** |
|
25287 | 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 |
|
10 | 104 |
*/ |
105 |
public abstract Tree getTree(Element element); |
|
106 |
||
107 |
/** |
|
25287 | 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 |
|
10 | 112 |
*/ |
113 |
public abstract ClassTree getTree(TypeElement element); |
|
114 |
||
115 |
/** |
|
25287 | 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 |
|
10 | 120 |
*/ |
121 |
public abstract MethodTree getTree(ExecutableElement method); |
|
122 |
||
123 |
/** |
|
25287 | 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 |
|
10 | 129 |
*/ |
130 |
public abstract Tree getTree(Element e, AnnotationMirror a); |
|
131 |
||
132 |
/** |
|
25287 | 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 |
|
10 | 139 |
*/ |
140 |
public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); |
|
141 |
||
142 |
/** |
|
25287 | 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 |
|
10 | 147 |
*/ |
148 |
public abstract TreePath getPath(CompilationUnitTree unit, Tree node); |
|
149 |
||
150 |
/** |
|
25287 | 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 |
|
10 | 155 |
*/ |
156 |
public abstract TreePath getPath(Element e); |
|
157 |
||
158 |
/** |
|
25287 | 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 |
|
10 | 164 |
*/ |
165 |
public abstract TreePath getPath(Element e, AnnotationMirror a); |
|
166 |
||
167 |
/** |
|
25287 | 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 |
|
10 | 174 |
*/ |
175 |
public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); |
|
176 |
||
177 |
/** |
|
25287 | 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 |
|
10 | 182 |
* @throws IllegalArgumentException is the TreePath does not identify |
183 |
* a Tree node that might have an associated Element. |
|
184 |
*/ |
|
185 |
public abstract Element getElement(TreePath path); |
|
186 |
||
187 |
/** |
|
25287 | 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 |
|
10 | 192 |
* @throws IllegalArgumentException is the TreePath does not identify |
193 |
* a Tree node that might have an associated TypeMirror. |
|
194 |
*/ |
|
195 |
public abstract TypeMirror getTypeMirror(TreePath path); |
|
196 |
||
197 |
/** |
|
25287 | 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 |
|
10 | 202 |
*/ |
203 |
public abstract Scope getScope(TreePath path); |
|
204 |
||
205 |
/** |
|
25287 | 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. |
|
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
9599
diff
changeset
|
208 |
* @see DocTrees#getDocCommentTree(TreePath) |
25287 | 209 |
* @param path the tree path |
210 |
* @return the doc comment |
|
7631 | 211 |
*/ |
212 |
public abstract String getDocComment(TreePath path); |
|
213 |
||
214 |
/** |
|
10 | 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 |
*/ |
|
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 |
*/ |
|
230 |
public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
231 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
232 |
/** |
25287 | 233 |
* Returns the original type from the ErrorType object. |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
234 |
* @param errorType The errorType for which we want to get the original type. |
3149 | 235 |
* @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType. |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
236 |
*/ |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
237 |
public abstract TypeMirror getOriginalType(ErrorType errorType); |
3149 | 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 |
*/ |
|
248 |
public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg, |
|
249 |
com.sun.source.tree.Tree t, |
|
250 |
com.sun.source.tree.CompilationUnitTree root); |
|
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
251 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
252 |
/** |
25287 | 253 |
* Returns the lub of an exception parameter declared in a catch clause. |
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
254 |
* @param tree the tree for the catch clause |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
255 |
* @return The lub of the exception parameter |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
256 |
*/ |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
257 |
public abstract TypeMirror getLub(CatchTree tree); |
10 | 258 |
} |