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