001 /*
002 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.source.util;
027
028 import java.lang.reflect.Method;
029 import javax.annotation.processing.ProcessingEnvironment;
030 import javax.lang.model.element.AnnotationMirror;
031 import javax.lang.model.element.AnnotationValue;
032 import javax.lang.model.element.Element;
033 import javax.lang.model.element.ExecutableElement;
034 import javax.lang.model.element.TypeElement;
035 import javax.lang.model.type.DeclaredType;
036 import javax.lang.model.type.ErrorType;
037 import javax.lang.model.type.TypeMirror;
038 import javax.tools.JavaCompiler.CompilationTask;
039
040 import com.sun.source.tree.ClassTree;
041 import com.sun.source.tree.CompilationUnitTree;
042 import com.sun.source.tree.MethodTree;
043 import com.sun.source.tree.Scope;
044 import com.sun.source.tree.Tree;
045
046 /**
047 * Bridges JSR 199, JSR 269, and the Tree API.
048 *
049 * @author Peter von der Ahé
050 */
051 public abstract class Trees {
052 /**
053 * Gets a Trees object for a given CompilationTask.
054 * @throws IllegalArgumentException if the task does not support the Trees API.
055 */
056 public static Trees instance(CompilationTask task) {
057 if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl"))
058 throw new IllegalArgumentException();
059 return getJavacTrees(CompilationTask.class, task);
060 }
061
062 /**
063 * Gets a Trees object for a given CompilationTask.
064 * @throws IllegalArgumentException if the env does not support the Trees API.
065 */
066 public static Trees instance(ProcessingEnvironment env) {
067 if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
068 throw new IllegalArgumentException();
069 return getJavacTrees(ProcessingEnvironment.class, env);
070 }
071
072 private static Trees getJavacTrees(Class<?> argType, Object arg) {
073 try {
074 ClassLoader cl = arg.getClass().getClassLoader();
075 Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
076 argType = Class.forName(argType.getName(), false, cl);
077 Method m = c.getMethod("instance", new Class<?>[] { argType });
078 return (Trees) m.invoke(null, new Object[] { arg });
079 } catch (Throwable e) {
080 throw new AssertionError(e);
081 }
082 }
083
084 /**
085 * Gets a utility object for obtaining source positions.
086 */
087 public abstract SourcePositions getSourcePositions();
088
089 /**
090 * Gets the Tree node for a given Element.
091 * Returns null if the node can not be found.
092 */
093 public abstract Tree getTree(Element element);
094
095 /**
096 * Gets the ClassTree node for a given TypeElement.
097 * Returns null if the node can not be found.
098 */
099 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);
181
182 /**
183 * Gets the original type from the ErrorType object.
184 * @param errorType The errorType for which we want to get the original type.
185 * @returns javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
186 */
187 public abstract TypeMirror getOriginalType(ErrorType errorType);
188 }