Tue, 28 Dec 2010 15:54:52 -0800
6962318: Update copyright year
Reviewed-by: xdono
1 /*
2 * Copyright (c) 2005, 2010, 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 */
26 package com.sun.source.util;
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;
36 import javax.lang.model.type.ErrorType;
37 import javax.lang.model.type.TypeMirror;
38 import javax.tools.Diagnostic;
39 import javax.tools.JavaCompiler.CompilationTask;
41 import com.sun.source.tree.ClassTree;
42 import com.sun.source.tree.CompilationUnitTree;
43 import com.sun.source.tree.MethodTree;
44 import com.sun.source.tree.Scope;
45 import com.sun.source.tree.Tree;
47 /**
48 * Bridges JSR 199, JSR 269, and the Tree API.
49 *
50 * @author Peter von der Ahé
51 */
52 public abstract class Trees {
53 /**
54 * Gets a Trees object for a given CompilationTask.
55 * @param task the compilation task for which to get the Trees object
56 * @throws IllegalArgumentException if the task does not support the Trees API.
57 */
58 public static Trees instance(CompilationTask task) {
59 if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl"))
60 throw new IllegalArgumentException();
61 return getJavacTrees(CompilationTask.class, task);
62 }
64 /**
65 * Gets a Trees object for a given ProcessingEnvironment.
66 * @param env the processing environment for which to get the Trees object
67 * @throws IllegalArgumentException if the env does not support the Trees API.
68 */
69 public static Trees instance(ProcessingEnvironment env) {
70 if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
71 throw new IllegalArgumentException();
72 return getJavacTrees(ProcessingEnvironment.class, env);
73 }
75 private static Trees getJavacTrees(Class<?> argType, Object arg) {
76 try {
77 ClassLoader cl = arg.getClass().getClassLoader();
78 Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
79 argType = Class.forName(argType.getName(), false, cl);
80 Method m = c.getMethod("instance", new Class<?>[] { argType });
81 return (Trees) m.invoke(null, new Object[] { arg });
82 } catch (Throwable e) {
83 throw new AssertionError(e);
84 }
85 }
87 /**
88 * Gets a utility object for obtaining source positions.
89 */
90 public abstract SourcePositions getSourcePositions();
92 /**
93 * Gets the Tree node for a given Element.
94 * Returns null if the node can not be found.
95 */
96 public abstract Tree getTree(Element element);
98 /**
99 * Gets the ClassTree node for a given TypeElement.
100 * Returns null if the node can not be found.
101 */
102 public abstract ClassTree getTree(TypeElement element);
104 /**
105 * Gets the MethodTree node for a given ExecutableElement.
106 * Returns null if the node can not be found.
107 */
108 public abstract MethodTree getTree(ExecutableElement method);
110 /**
111 * Gets the Tree node for an AnnotationMirror on a given Element.
112 * Returns null if the node can not be found.
113 */
114 public abstract Tree getTree(Element e, AnnotationMirror a);
116 /**
117 * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
118 * Returns null if the node can not be found.
119 */
120 public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
122 /**
123 * Gets the path to tree node within the specified compilation unit.
124 */
125 public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
127 /**
128 * Gets the TreePath node for a given Element.
129 * Returns null if the node can not be found.
130 */
131 public abstract TreePath getPath(Element e);
133 /**
134 * Gets the TreePath node for an AnnotationMirror on a given Element.
135 * Returns null if the node can not be found.
136 */
137 public abstract TreePath getPath(Element e, AnnotationMirror a);
139 /**
140 * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
141 * Returns null if the node can not be found.
142 */
143 public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
145 /**
146 * Gets the Element for the Tree node identified by a given TreePath.
147 * Returns null if the element is not available.
148 * @throws IllegalArgumentException is the TreePath does not identify
149 * a Tree node that might have an associated Element.
150 */
151 public abstract Element getElement(TreePath path);
153 /**
154 * Gets the TypeMirror for the Tree node identified by a given TreePath.
155 * Returns null if the TypeMirror is not available.
156 * @throws IllegalArgumentException is the TreePath does not identify
157 * a Tree node that might have an associated TypeMirror.
158 */
159 public abstract TypeMirror getTypeMirror(TreePath path);
161 /**
162 * Gets the Scope for the Tree node identified by a given TreePath.
163 * Returns null if the Scope is not available.
164 */
165 public abstract Scope getScope(TreePath path);
167 /**
168 * Gets the doc comment, if any, for the Tree node identified by a given TreePath.
169 * Returns null if no doc comment was found.
170 */
171 public abstract String getDocComment(TreePath path);
173 /**
174 * Checks whether a given type is accessible in a given scope.
175 * @param scope the scope to be checked
176 * @param type the type to be checked
177 * @return true if {@code type} is accessible
178 */
179 public abstract boolean isAccessible(Scope scope, TypeElement type);
181 /**
182 * Checks whether the given element is accessible as a member of the given
183 * type in a given scope.
184 * @param scope the scope to be checked
185 * @param member the member to be checked
186 * @param type the type for which to check if the member is accessible
187 * @return true if {@code member} is accessible in {@code type}
188 */
189 public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
191 /**
192 * Gets the original type from the ErrorType object.
193 * @param errorType The errorType for which we want to get the original type.
194 * @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
195 */
196 public abstract TypeMirror getOriginalType(ErrorType errorType);
198 /**
199 * Prints a message of the specified kind at the location of the
200 * tree within the provided compilation unit
201 *
202 * @param kind the kind of message
203 * @param msg the message, or an empty string if none
204 * @param t the tree to use as a position hint
205 * @param root the compilation unit that contains tree
206 */
207 public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
208 com.sun.source.tree.Tree t,
209 com.sun.source.tree.CompilationUnitTree root);
210 }