Thu, 02 Oct 2008 19:58:40 -0700
6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell
duke@1 | 1 | /* |
xdono@117 | 2 | * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.source.util; |
duke@1 | 27 | |
duke@1 | 28 | import java.lang.reflect.Method; |
duke@1 | 29 | import javax.annotation.processing.ProcessingEnvironment; |
duke@1 | 30 | import javax.lang.model.element.AnnotationMirror; |
duke@1 | 31 | import javax.lang.model.element.AnnotationValue; |
duke@1 | 32 | import javax.lang.model.element.Element; |
duke@1 | 33 | import javax.lang.model.element.ExecutableElement; |
duke@1 | 34 | import javax.lang.model.element.TypeElement; |
duke@1 | 35 | import javax.lang.model.type.DeclaredType; |
jjg@110 | 36 | import javax.lang.model.type.ErrorType; |
duke@1 | 37 | import javax.lang.model.type.TypeMirror; |
duke@1 | 38 | import javax.tools.JavaCompiler.CompilationTask; |
duke@1 | 39 | |
duke@1 | 40 | import com.sun.source.tree.ClassTree; |
duke@1 | 41 | import com.sun.source.tree.CompilationUnitTree; |
duke@1 | 42 | import com.sun.source.tree.MethodTree; |
duke@1 | 43 | import com.sun.source.tree.Scope; |
duke@1 | 44 | import com.sun.source.tree.Tree; |
duke@1 | 45 | |
duke@1 | 46 | /** |
duke@1 | 47 | * Bridges JSR 199, JSR 269, and the Tree API. |
duke@1 | 48 | * |
duke@1 | 49 | * @author Peter von der Ahé |
duke@1 | 50 | */ |
duke@1 | 51 | public abstract class Trees { |
duke@1 | 52 | /** |
duke@1 | 53 | * Gets a Trees object for a given CompilationTask. |
duke@1 | 54 | * @throws IllegalArgumentException if the task does not support the Trees API. |
duke@1 | 55 | */ |
duke@1 | 56 | public static Trees instance(CompilationTask task) { |
duke@1 | 57 | if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl")) |
duke@1 | 58 | throw new IllegalArgumentException(); |
duke@1 | 59 | return getJavacTrees(CompilationTask.class, task); |
duke@1 | 60 | } |
duke@1 | 61 | |
duke@1 | 62 | /** |
duke@1 | 63 | * Gets a Trees object for a given CompilationTask. |
duke@1 | 64 | * @throws IllegalArgumentException if the env does not support the Trees API. |
duke@1 | 65 | */ |
duke@1 | 66 | public static Trees instance(ProcessingEnvironment env) { |
duke@1 | 67 | if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) |
duke@1 | 68 | throw new IllegalArgumentException(); |
duke@1 | 69 | return getJavacTrees(ProcessingEnvironment.class, env); |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | private static Trees getJavacTrees(Class<?> argType, Object arg) { |
duke@1 | 73 | try { |
duke@1 | 74 | ClassLoader cl = arg.getClass().getClassLoader(); |
duke@1 | 75 | Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); |
duke@1 | 76 | argType = Class.forName(argType.getName(), false, cl); |
duke@1 | 77 | Method m = c.getMethod("instance", new Class[] { argType }); |
duke@1 | 78 | return (Trees) m.invoke(null, new Object[] { arg }); |
duke@1 | 79 | } catch (Throwable e) { |
duke@1 | 80 | throw new AssertionError(e); |
duke@1 | 81 | } |
duke@1 | 82 | } |
duke@1 | 83 | |
duke@1 | 84 | /** |
duke@1 | 85 | * Gets a utility object for obtaining source positions. |
duke@1 | 86 | */ |
duke@1 | 87 | public abstract SourcePositions getSourcePositions(); |
duke@1 | 88 | |
duke@1 | 89 | /** |
duke@1 | 90 | * Gets the Tree node for a given Element. |
duke@1 | 91 | * Returns null if the node can not be found. |
duke@1 | 92 | */ |
duke@1 | 93 | public abstract Tree getTree(Element element); |
duke@1 | 94 | |
duke@1 | 95 | /** |
duke@1 | 96 | * Gets the ClassTree node for a given TypeElement. |
duke@1 | 97 | * Returns null if the node can not be found. |
duke@1 | 98 | */ |
duke@1 | 99 | public abstract ClassTree getTree(TypeElement element); |
duke@1 | 100 | |
duke@1 | 101 | /** |
duke@1 | 102 | * Gets the MethodTree node for a given ExecutableElement. |
duke@1 | 103 | * Returns null if the node can not be found. |
duke@1 | 104 | */ |
duke@1 | 105 | public abstract MethodTree getTree(ExecutableElement method); |
duke@1 | 106 | |
duke@1 | 107 | /** |
duke@1 | 108 | * Gets the Tree node for an AnnotationMirror on a given Element. |
duke@1 | 109 | * Returns null if the node can not be found. |
duke@1 | 110 | */ |
duke@1 | 111 | public abstract Tree getTree(Element e, AnnotationMirror a); |
duke@1 | 112 | |
duke@1 | 113 | /** |
duke@1 | 114 | * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element. |
duke@1 | 115 | * Returns null if the node can not be found. |
duke@1 | 116 | */ |
duke@1 | 117 | public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); |
duke@1 | 118 | |
duke@1 | 119 | /** |
duke@1 | 120 | * Gets the path to tree node within the specified compilation unit. |
duke@1 | 121 | */ |
duke@1 | 122 | public abstract TreePath getPath(CompilationUnitTree unit, Tree node); |
duke@1 | 123 | |
duke@1 | 124 | /** |
duke@1 | 125 | * Gets the TreePath node for a given Element. |
duke@1 | 126 | * Returns null if the node can not be found. |
duke@1 | 127 | */ |
duke@1 | 128 | public abstract TreePath getPath(Element e); |
duke@1 | 129 | |
duke@1 | 130 | /** |
duke@1 | 131 | * Gets the TreePath node for an AnnotationMirror on a given Element. |
duke@1 | 132 | * Returns null if the node can not be found. |
duke@1 | 133 | */ |
duke@1 | 134 | public abstract TreePath getPath(Element e, AnnotationMirror a); |
duke@1 | 135 | |
duke@1 | 136 | /** |
duke@1 | 137 | * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element. |
duke@1 | 138 | * Returns null if the node can not be found. |
duke@1 | 139 | */ |
duke@1 | 140 | public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); |
duke@1 | 141 | |
duke@1 | 142 | /** |
duke@1 | 143 | * Gets the Element for the Tree node identified by a given TreePath. |
duke@1 | 144 | * Returns null if the element is not available. |
duke@1 | 145 | * @throws IllegalArgumentException is the TreePath does not identify |
duke@1 | 146 | * a Tree node that might have an associated Element. |
duke@1 | 147 | */ |
duke@1 | 148 | public abstract Element getElement(TreePath path); |
duke@1 | 149 | |
duke@1 | 150 | /** |
duke@1 | 151 | * Gets the TypeMirror for the Tree node identified by a given TreePath. |
duke@1 | 152 | * Returns null if the TypeMirror is not available. |
duke@1 | 153 | * @throws IllegalArgumentException is the TreePath does not identify |
duke@1 | 154 | * a Tree node that might have an associated TypeMirror. |
duke@1 | 155 | */ |
duke@1 | 156 | public abstract TypeMirror getTypeMirror(TreePath path); |
duke@1 | 157 | |
duke@1 | 158 | /** |
duke@1 | 159 | * Gets the Scope for the Tree node identified by a given TreePath. |
duke@1 | 160 | * Returns null if the Scope is not available. |
duke@1 | 161 | */ |
duke@1 | 162 | public abstract Scope getScope(TreePath path); |
duke@1 | 163 | |
duke@1 | 164 | /** |
duke@1 | 165 | * Checks whether a given type is accessible in a given scope. |
duke@1 | 166 | * @param scope the scope to be checked |
duke@1 | 167 | * @param type the type to be checked |
duke@1 | 168 | * @return true if {@code type} is accessible |
duke@1 | 169 | */ |
duke@1 | 170 | public abstract boolean isAccessible(Scope scope, TypeElement type); |
duke@1 | 171 | |
duke@1 | 172 | /** |
duke@1 | 173 | * Checks whether the given element is accessible as a member of the given |
duke@1 | 174 | * type in a given scope. |
duke@1 | 175 | * @param scope the scope to be checked |
duke@1 | 176 | * @param member the member to be checked |
duke@1 | 177 | * @param type the type for which to check if the member is accessible |
duke@1 | 178 | * @return true if {@code member} is accessible in {@code type} |
duke@1 | 179 | */ |
duke@1 | 180 | public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type); |
jjg@110 | 181 | |
jjg@110 | 182 | /** |
jjg@110 | 183 | * Gets the original type from the ErrorType object. |
jjg@110 | 184 | * @param errorType The errorType for which we want to get the original type. |
jjg@110 | 185 | * @returns javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType. |
jjg@110 | 186 | */ |
jjg@110 | 187 | public abstract TypeMirror getOriginalType(ErrorType errorType); |
duke@1 | 188 | } |