author | mcimadamore |
Tue, 13 Jan 2009 13:27:14 +0000 | |
changeset 1789 | 7ac8c0815000 |
parent 1264 | 076a3cde30d5 |
child 3149 | 0cd06d598d6f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
1264 | 2 |
* Copyright 2005-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.source.util; |
|
27 |
||
28 |
import java.lang.reflect.Method; |
|
29 |
import javax.annotation.processing.ProcessingEnvironment; |
|
30 |
import javax.lang.model.element.AnnotationMirror; |
|
31 |
import javax.lang.model.element.AnnotationValue; |
|
32 |
import javax.lang.model.element.Element; |
|
33 |
import javax.lang.model.element.ExecutableElement; |
|
34 |
import javax.lang.model.element.TypeElement; |
|
35 |
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
|
36 |
import javax.lang.model.type.ErrorType; |
10 | 37 |
import javax.lang.model.type.TypeMirror; |
38 |
import javax.tools.JavaCompiler.CompilationTask; |
|
39 |
||
40 |
import com.sun.source.tree.ClassTree; |
|
41 |
import com.sun.source.tree.CompilationUnitTree; |
|
42 |
import com.sun.source.tree.MethodTree; |
|
43 |
import com.sun.source.tree.Scope; |
|
44 |
import com.sun.source.tree.Tree; |
|
45 |
||
46 |
/** |
|
47 |
* Bridges JSR 199, JSR 269, and the Tree API. |
|
48 |
* |
|
49 |
* @author Peter von der Ahé |
|
50 |
*/ |
|
51 |
public abstract class Trees { |
|
52 |
/** |
|
53 |
* Gets a Trees object for a given CompilationTask. |
|
54 |
* @throws IllegalArgumentException if the task does not support the Trees API. |
|
55 |
*/ |
|
56 |
public static Trees instance(CompilationTask task) { |
|
57 |
if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl")) |
|
58 |
throw new IllegalArgumentException(); |
|
59 |
return getJavacTrees(CompilationTask.class, task); |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* Gets a Trees object for a given CompilationTask. |
|
64 |
* @throws IllegalArgumentException if the env does not support the Trees API. |
|
65 |
*/ |
|
66 |
public static Trees instance(ProcessingEnvironment env) { |
|
67 |
if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) |
|
68 |
throw new IllegalArgumentException(); |
|
69 |
return getJavacTrees(ProcessingEnvironment.class, env); |
|
70 |
} |
|
71 |
||
72 |
private static Trees getJavacTrees(Class<?> argType, Object arg) { |
|
73 |
try { |
|
74 |
ClassLoader cl = arg.getClass().getClassLoader(); |
|
75 |
Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); |
|
76 |
argType = Class.forName(argType.getName(), false, cl); |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1264
diff
changeset
|
77 |
Method m = c.getMethod("instance", new Class<?>[] { argType }); |
10 | 78 |
return (Trees) m.invoke(null, new Object[] { arg }); |
79 |
} catch (Throwable e) { |
|
80 |
throw new AssertionError(e); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Gets a utility object for obtaining source positions. |
|
86 |
*/ |
|
87 |
public abstract SourcePositions getSourcePositions(); |
|
88 |
||
89 |
/** |
|
90 |
* Gets the Tree node for a given Element. |
|
91 |
* Returns null if the node can not be found. |
|
92 |
*/ |
|
93 |
public abstract Tree getTree(Element element); |
|
94 |
||
95 |
/** |
|
96 |
* Gets the ClassTree node for a given TypeElement. |
|
97 |
* Returns null if the node can not be found. |
|
98 |
*/ |
|
99 |
public abstract ClassTree getTree(TypeElement element); |
|
100 |
||
101 |
/** |
|
102 |
* Gets the MethodTree node for a given ExecutableElement. |
|
103 |
* Returns null if the node can not be found. |
|
104 |
*/ |
|
105 |
public abstract MethodTree getTree(ExecutableElement method); |
|
106 |
||
107 |
/** |
|
108 |
* Gets the Tree node for an AnnotationMirror on a given Element. |
|
109 |
* Returns null if the node can not be found. |
|
110 |
*/ |
|
111 |
public abstract Tree getTree(Element e, AnnotationMirror a); |
|
112 |
||
113 |
/** |
|
114 |
* Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element. |
|
115 |
* Returns null if the node can not be found. |
|
116 |
*/ |
|
117 |
public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); |
|
118 |
||
119 |
/** |
|
120 |
* Gets the path to tree node within the specified compilation unit. |
|
121 |
*/ |
|
122 |
public abstract TreePath getPath(CompilationUnitTree unit, Tree node); |
|
123 |
||
124 |
/** |
|
125 |
* Gets the TreePath node for a given Element. |
|
126 |
* Returns null if the node can not be found. |
|
127 |
*/ |
|
128 |
public abstract TreePath getPath(Element e); |
|
129 |
||
130 |
/** |
|
131 |
* Gets the TreePath node for an AnnotationMirror on a given Element. |
|
132 |
* Returns null if the node can not be found. |
|
133 |
*/ |
|
134 |
public abstract TreePath getPath(Element e, AnnotationMirror a); |
|
135 |
||
136 |
/** |
|
137 |
* Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element. |
|
138 |
* Returns null if the node can not be found. |
|
139 |
*/ |
|
140 |
public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); |
|
141 |
||
142 |
/** |
|
143 |
* Gets the Element for the Tree node identified by a given TreePath. |
|
144 |
* Returns null if the element is not available. |
|
145 |
* @throws IllegalArgumentException is the TreePath does not identify |
|
146 |
* a Tree node that might have an associated Element. |
|
147 |
*/ |
|
148 |
public abstract Element getElement(TreePath path); |
|
149 |
||
150 |
/** |
|
151 |
* Gets the TypeMirror for the Tree node identified by a given TreePath. |
|
152 |
* Returns null if the TypeMirror is not available. |
|
153 |
* @throws IllegalArgumentException is the TreePath does not identify |
|
154 |
* a Tree node that might have an associated TypeMirror. |
|
155 |
*/ |
|
156 |
public abstract TypeMirror getTypeMirror(TreePath path); |
|
157 |
||
158 |
/** |
|
159 |
* Gets the Scope for the Tree node identified by a given TreePath. |
|
160 |
* Returns null if the Scope is not available. |
|
161 |
*/ |
|
162 |
public abstract Scope getScope(TreePath path); |
|
163 |
||
164 |
/** |
|
165 |
* Checks whether a given type is accessible in a given scope. |
|
166 |
* @param scope the scope to be checked |
|
167 |
* @param type the type to be checked |
|
168 |
* @return true if {@code type} is accessible |
|
169 |
*/ |
|
170 |
public abstract boolean isAccessible(Scope scope, TypeElement type); |
|
171 |
||
172 |
/** |
|
173 |
* Checks whether the given element is accessible as a member of the given |
|
174 |
* type in a given scope. |
|
175 |
* @param scope the scope to be checked |
|
176 |
* @param member the member to be checked |
|
177 |
* @param type the type for which to check if the member is accessible |
|
178 |
* @return true if {@code member} is accessible in {@code type} |
|
179 |
*/ |
|
180 |
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
|
181 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
182 |
/** |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
183 |
* 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
|
184 |
* @param errorType The errorType for which we want to get the original type. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
185 |
* @returns javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
186 |
*/ |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
187 |
public abstract TypeMirror getOriginalType(ErrorType errorType); |
10 | 188 |
} |