| 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. 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. 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; |
| 36 | import javax.lang.model.type.TypeMirror; |
| 37 | import javax.tools.JavaCompiler.CompilationTask; |
| 38 | |
| 39 | import com.sun.source.tree.ClassTree; |
| 40 | import com.sun.source.tree.CompilationUnitTree; |
| 41 | import com.sun.source.tree.MethodTree; |
| 42 | import com.sun.source.tree.Scope; |
| 43 | import com.sun.source.tree.Tree; |
| 44 | |
| 45 | /** |
| 46 | * Bridges JSR 199, JSR 269, and the Tree API. |
| 47 | * |
| 48 | * @author Peter von der Ahé |
| 49 | */ |
| 50 | public abstract class Trees { |
| 51 | /** |
| 52 | * Gets a Trees object for a given CompilationTask. |
| 53 | * @throws IllegalArgumentException if the task does not support the Trees API. |
| 54 | */ |
| 55 | public static Trees instance(CompilationTask task) { |
| 56 | if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl")) |
| 57 | throw new IllegalArgumentException(); |
| 58 | return getJavacTrees(CompilationTask.class, task); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets a Trees object for a given CompilationTask. |
| 63 | * @throws IllegalArgumentException if the env does not support the Trees API. |
| 64 | */ |
| 65 | public static Trees instance(ProcessingEnvironment env) { |
| 66 | if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) |
| 67 | throw new IllegalArgumentException(); |
| 68 | return getJavacTrees(ProcessingEnvironment.class, env); |
| 69 | } |
| 70 | |
| 71 | private static Trees getJavacTrees(Class<?> argType, Object arg) { |
| 72 | try { |
| 73 | ClassLoader cl = arg.getClass().getClassLoader(); |
| 74 | Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); |
| 75 | argType = Class.forName(argType.getName(), false, cl); |
| 76 | Method m = c.getMethod("instance", new Class[] { argType }); |
| 77 | return (Trees) m.invoke(null, new Object[] { arg }); |
| 78 | } catch (Throwable e) { |
| 79 | throw new AssertionError(e); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets a utility object for obtaining source positions. |
| 85 | */ |
| 86 | public abstract SourcePositions getSourcePositions(); |
| 87 | |
| 88 | /** |
| 89 | * Gets the Tree node for a given Element. |
| 90 | * Returns null if the node can not be found. |
| 91 | */ |
| 92 | public abstract Tree getTree(Element element); |
| 93 | |
| 94 | /** |
| 95 | * Gets the ClassTree node for a given TypeElement. |
| 96 | * Returns null if the node can not be found. |
| 97 | */ |
| 98 | public abstract ClassTree getTree(TypeElement element); |
| 99 | |
| 100 | /** |
| 101 | * Gets the MethodTree node for a given ExecutableElement. |
| 102 | * Returns null if the node can not be found. |
| 103 | */ |
| 104 | public abstract MethodTree getTree(ExecutableElement method); |
| 105 | |
| 106 | /** |
| 107 | * Gets the Tree node for an AnnotationMirror on a given Element. |
| 108 | * Returns null if the node can not be found. |
| 109 | */ |
| 110 | public abstract Tree getTree(Element e, AnnotationMirror a); |
| 111 | |
| 112 | /** |
| 113 | * Gets the Tree node for an AnnotationValue 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, AnnotationValue v); |
| 117 | |
| 118 | /** |
| 119 | * Gets the path to tree node within the specified compilation unit. |
| 120 | */ |
| 121 | public abstract TreePath getPath(CompilationUnitTree unit, Tree node); |
| 122 | |
| 123 | /** |
| 124 | * Gets the TreePath node for a given Element. |
| 125 | * Returns null if the node can not be found. |
| 126 | */ |
| 127 | public abstract TreePath getPath(Element e); |
| 128 | |
| 129 | /** |
| 130 | * Gets the TreePath node for an AnnotationMirror on a given Element. |
| 131 | * Returns null if the node can not be found. |
| 132 | */ |
| 133 | public abstract TreePath getPath(Element e, AnnotationMirror a); |
| 134 | |
| 135 | /** |
| 136 | * Gets the TreePath node for an AnnotationValue 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, AnnotationValue v); |
| 140 | |
| 141 | /** |
| 142 | * Gets the Element for the Tree node identified by a given TreePath. |
| 143 | * Returns null if the element is not available. |
| 144 | * @throws IllegalArgumentException is the TreePath does not identify |
| 145 | * a Tree node that might have an associated Element. |
| 146 | */ |
| 147 | public abstract Element getElement(TreePath path); |
| 148 | |
| 149 | /** |
| 150 | * Gets the TypeMirror for the Tree node identified by a given TreePath. |
| 151 | * Returns null if the TypeMirror is not available. |
| 152 | * @throws IllegalArgumentException is the TreePath does not identify |
| 153 | * a Tree node that might have an associated TypeMirror. |
| 154 | */ |
| 155 | public abstract TypeMirror getTypeMirror(TreePath path); |
| 156 | |
| 157 | /** |
| 158 | * Gets the Scope for the Tree node identified by a given TreePath. |
| 159 | * Returns null if the Scope is not available. |
| 160 | */ |
| 161 | public abstract Scope getScope(TreePath path); |
| 162 | |
| 163 | /** |
| 164 | * Checks whether a given type is accessible in a given scope. |
| 165 | * @param scope the scope to be checked |
| 166 | * @param type the type to be checked |
| 167 | * @return true if {@code type} is accessible |
| 168 | */ |
| 169 | public abstract boolean isAccessible(Scope scope, TypeElement type); |
| 170 | |
| 171 | /** |
| 172 | * Checks whether the given element is accessible as a member of the given |
| 173 | * type in a given scope. |
| 174 | * @param scope the scope to be checked |
| 175 | * @param member the member to be checked |
| 176 | * @param type the type for which to check if the member is accessible |
| 177 | * @return true if {@code member} is accessible in {@code type} |
| 178 | */ |
| 179 | public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type); |
| 180 | } |