author | jjg |
Wed, 14 Nov 2012 17:23:10 -0800 | |
changeset 14541 | 36f9d11fc9aa |
parent 9599 | 0996df19ea87 |
child 14548 | aa687b312c97 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
9599
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, 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 |
/** |
|
56 |
* Gets 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. |
59 |
*/ |
|
60 |
public static Trees instance(CompilationTask task) { |
|
61 |
if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl")) |
|
62 |
throw new IllegalArgumentException(); |
|
63 |
return getJavacTrees(CompilationTask.class, task); |
|
64 |
} |
|
65 |
||
66 |
/** |
|
7633
52b637b3b246
6986242: cut-n-paste error in javadoc for Trees.instance(ProcessingEnvironment)
jjg
parents:
7631
diff
changeset
|
67 |
* Gets a Trees object for a given ProcessingEnvironment. |
52b637b3b246
6986242: cut-n-paste error in javadoc for Trees.instance(ProcessingEnvironment)
jjg
parents:
7631
diff
changeset
|
68 |
* @param env the processing environment for which to get the Trees object |
10 | 69 |
* @throws IllegalArgumentException if the env does not support the Trees API. |
70 |
*/ |
|
71 |
public static Trees instance(ProcessingEnvironment env) { |
|
72 |
if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) |
|
73 |
throw new IllegalArgumentException(); |
|
74 |
return getJavacTrees(ProcessingEnvironment.class, env); |
|
75 |
} |
|
76 |
||
14541
36f9d11fc9aa
7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
9599
diff
changeset
|
77 |
static Trees getJavacTrees(Class<?> argType, Object arg) { |
10 | 78 |
try { |
79 |
ClassLoader cl = arg.getClass().getClassLoader(); |
|
80 |
Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); |
|
81 |
argType = Class.forName(argType.getName(), false, cl); |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
82 |
Method m = c.getMethod("instance", new Class<?>[] { argType }); |
10 | 83 |
return (Trees) m.invoke(null, new Object[] { arg }); |
84 |
} catch (Throwable e) { |
|
85 |
throw new AssertionError(e); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Gets a utility object for obtaining source positions. |
|
91 |
*/ |
|
92 |
public abstract SourcePositions getSourcePositions(); |
|
93 |
||
94 |
/** |
|
95 |
* Gets the Tree node for a given Element. |
|
96 |
* Returns null if the node can not be found. |
|
97 |
*/ |
|
98 |
public abstract Tree getTree(Element element); |
|
99 |
||
100 |
/** |
|
101 |
* Gets the ClassTree node for a given TypeElement. |
|
102 |
* Returns null if the node can not be found. |
|
103 |
*/ |
|
104 |
public abstract ClassTree getTree(TypeElement element); |
|
105 |
||
106 |
/** |
|
107 |
* Gets the MethodTree node for a given ExecutableElement. |
|
108 |
* Returns null if the node can not be found. |
|
109 |
*/ |
|
110 |
public abstract MethodTree getTree(ExecutableElement method); |
|
111 |
||
112 |
/** |
|
113 |
* Gets the Tree node for an AnnotationMirror on a given Element. |
|
114 |
* Returns null if the node can not be found. |
|
115 |
*/ |
|
116 |
public abstract Tree getTree(Element e, AnnotationMirror a); |
|
117 |
||
118 |
/** |
|
119 |
* Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element. |
|
120 |
* Returns null if the node can not be found. |
|
121 |
*/ |
|
122 |
public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); |
|
123 |
||
124 |
/** |
|
125 |
* Gets the path to tree node within the specified compilation unit. |
|
126 |
*/ |
|
127 |
public abstract TreePath getPath(CompilationUnitTree unit, Tree node); |
|
128 |
||
129 |
/** |
|
130 |
* Gets the TreePath node for a given Element. |
|
131 |
* Returns null if the node can not be found. |
|
132 |
*/ |
|
133 |
public abstract TreePath getPath(Element e); |
|
134 |
||
135 |
/** |
|
136 |
* Gets the TreePath node for an AnnotationMirror on a given Element. |
|
137 |
* Returns null if the node can not be found. |
|
138 |
*/ |
|
139 |
public abstract TreePath getPath(Element e, AnnotationMirror a); |
|
140 |
||
141 |
/** |
|
142 |
* Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element. |
|
143 |
* Returns null if the node can not be found. |
|
144 |
*/ |
|
145 |
public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); |
|
146 |
||
147 |
/** |
|
148 |
* Gets the Element for the Tree node identified by a given TreePath. |
|
149 |
* Returns null if the element is not available. |
|
150 |
* @throws IllegalArgumentException is the TreePath does not identify |
|
151 |
* a Tree node that might have an associated Element. |
|
152 |
*/ |
|
153 |
public abstract Element getElement(TreePath path); |
|
154 |
||
155 |
/** |
|
156 |
* Gets the TypeMirror for the Tree node identified by a given TreePath. |
|
157 |
* Returns null if the TypeMirror is not available. |
|
158 |
* @throws IllegalArgumentException is the TreePath does not identify |
|
159 |
* a Tree node that might have an associated TypeMirror. |
|
160 |
*/ |
|
161 |
public abstract TypeMirror getTypeMirror(TreePath path); |
|
162 |
||
163 |
/** |
|
164 |
* Gets the Scope for the Tree node identified by a given TreePath. |
|
165 |
* Returns null if the Scope is not available. |
|
166 |
*/ |
|
167 |
public abstract Scope getScope(TreePath path); |
|
168 |
||
169 |
/** |
|
7631 | 170 |
* Gets the doc comment, if any, for the Tree node identified by a given TreePath. |
171 |
* Returns 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
|
172 |
* @see DocTrees#getDocCommentTree(TreePath) |
7631 | 173 |
*/ |
174 |
public abstract String getDocComment(TreePath path); |
|
175 |
||
176 |
/** |
|
10 | 177 |
* Checks whether a given type is accessible in a given scope. |
178 |
* @param scope the scope to be checked |
|
179 |
* @param type the type to be checked |
|
180 |
* @return true if {@code type} is accessible |
|
181 |
*/ |
|
182 |
public abstract boolean isAccessible(Scope scope, TypeElement type); |
|
183 |
||
184 |
/** |
|
185 |
* Checks whether the given element is accessible as a member of the given |
|
186 |
* type in a given scope. |
|
187 |
* @param scope the scope to be checked |
|
188 |
* @param member the member to be checked |
|
189 |
* @param type the type for which to check if the member is accessible |
|
190 |
* @return true if {@code member} is accessible in {@code type} |
|
191 |
*/ |
|
192 |
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
|
193 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
194 |
/** |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
195 |
* Gets the original type from the ErrorType object. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
196 |
* @param errorType The errorType for which we want to get the original type. |
3149 | 197 |
* @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
|
198 |
*/ |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
199 |
public abstract TypeMirror getOriginalType(ErrorType errorType); |
3149 | 200 |
|
201 |
/** |
|
202 |
* Prints a message of the specified kind at the location of the |
|
203 |
* tree within the provided compilation unit |
|
204 |
* |
|
205 |
* @param kind the kind of message |
|
206 |
* @param msg the message, or an empty string if none |
|
207 |
* @param t the tree to use as a position hint |
|
208 |
* @param root the compilation unit that contains tree |
|
209 |
*/ |
|
210 |
public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg, |
|
211 |
com.sun.source.tree.Tree t, |
|
212 |
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
|
213 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
214 |
/** |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
215 |
* Gets the lub of an exception parameter declared in a catch clause. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
216 |
* @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
|
217 |
* @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
|
218 |
*/ |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7681
diff
changeset
|
219 |
public abstract TypeMirror getLub(CatchTree tree); |
10 | 220 |
} |