Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.source.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Method; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import javax.annotation.processing.ProcessingEnvironment; |
aoqi@0 | 31 | import javax.lang.model.element.AnnotationMirror; |
aoqi@0 | 32 | import javax.lang.model.element.AnnotationValue; |
aoqi@0 | 33 | import javax.lang.model.element.Element; |
aoqi@0 | 34 | import javax.lang.model.element.ExecutableElement; |
aoqi@0 | 35 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 36 | import javax.lang.model.type.DeclaredType; |
aoqi@0 | 37 | import javax.lang.model.type.ErrorType; |
aoqi@0 | 38 | import javax.lang.model.type.TypeMirror; |
aoqi@0 | 39 | import javax.tools.Diagnostic; |
aoqi@0 | 40 | import javax.tools.JavaCompiler.CompilationTask; |
aoqi@0 | 41 | |
aoqi@0 | 42 | import com.sun.source.tree.CatchTree; |
aoqi@0 | 43 | import com.sun.source.tree.ClassTree; |
aoqi@0 | 44 | import com.sun.source.tree.CompilationUnitTree; |
aoqi@0 | 45 | import com.sun.source.tree.MethodTree; |
aoqi@0 | 46 | import com.sun.source.tree.Scope; |
aoqi@0 | 47 | import com.sun.source.tree.Tree; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * Bridges JSR 199, JSR 269, and the Tree API. |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * @author Peter von der Ahé |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | @jdk.Exported |
aoqi@0 | 55 | public abstract class Trees { |
aoqi@0 | 56 | /** |
aoqi@0 | 57 | * Gets a Trees object for a given CompilationTask. |
aoqi@0 | 58 | * @param task the compilation task for which to get the Trees object |
aoqi@0 | 59 | * @throws IllegalArgumentException if the task does not support the Trees API. |
aoqi@0 | 60 | */ |
aoqi@0 | 61 | public static Trees instance(CompilationTask task) { |
aoqi@0 | 62 | String taskClassName = task.getClass().getName(); |
aoqi@0 | 63 | if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl") |
aoqi@0 | 64 | && !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask")) |
aoqi@0 | 65 | throw new IllegalArgumentException(); |
aoqi@0 | 66 | return getJavacTrees(CompilationTask.class, task); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | /** |
aoqi@0 | 70 | * Gets a Trees object for a given ProcessingEnvironment. |
aoqi@0 | 71 | * @param env the processing environment for which to get the Trees object |
aoqi@0 | 72 | * @throws IllegalArgumentException if the env does not support the Trees API. |
aoqi@0 | 73 | */ |
aoqi@0 | 74 | public static Trees instance(ProcessingEnvironment env) { |
aoqi@0 | 75 | if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) |
aoqi@0 | 76 | throw new IllegalArgumentException(); |
aoqi@0 | 77 | return getJavacTrees(ProcessingEnvironment.class, env); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | static Trees getJavacTrees(Class<?> argType, Object arg) { |
aoqi@0 | 81 | try { |
aoqi@0 | 82 | ClassLoader cl = arg.getClass().getClassLoader(); |
aoqi@0 | 83 | Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); |
aoqi@0 | 84 | argType = Class.forName(argType.getName(), false, cl); |
aoqi@0 | 85 | Method m = c.getMethod("instance", new Class<?>[] { argType }); |
aoqi@0 | 86 | return (Trees) m.invoke(null, new Object[] { arg }); |
aoqi@0 | 87 | } catch (Throwable e) { |
aoqi@0 | 88 | throw new AssertionError(e); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | /** |
aoqi@0 | 93 | * Gets a utility object for obtaining source positions. |
aoqi@0 | 94 | */ |
aoqi@0 | 95 | public abstract SourcePositions getSourcePositions(); |
aoqi@0 | 96 | |
aoqi@0 | 97 | /** |
aoqi@0 | 98 | * Gets the Tree node for a given Element. |
aoqi@0 | 99 | * Returns null if the node can not be found. |
aoqi@0 | 100 | */ |
aoqi@0 | 101 | public abstract Tree getTree(Element element); |
aoqi@0 | 102 | |
aoqi@0 | 103 | /** |
aoqi@0 | 104 | * Gets the ClassTree node for a given TypeElement. |
aoqi@0 | 105 | * Returns null if the node can not be found. |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | public abstract ClassTree getTree(TypeElement element); |
aoqi@0 | 108 | |
aoqi@0 | 109 | /** |
aoqi@0 | 110 | * Gets the MethodTree node for a given ExecutableElement. |
aoqi@0 | 111 | * Returns null if the node can not be found. |
aoqi@0 | 112 | */ |
aoqi@0 | 113 | public abstract MethodTree getTree(ExecutableElement method); |
aoqi@0 | 114 | |
aoqi@0 | 115 | /** |
aoqi@0 | 116 | * Gets the Tree node for an AnnotationMirror on a given Element. |
aoqi@0 | 117 | * Returns null if the node can not be found. |
aoqi@0 | 118 | */ |
aoqi@0 | 119 | public abstract Tree getTree(Element e, AnnotationMirror a); |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element. |
aoqi@0 | 123 | * Returns null if the node can not be found. |
aoqi@0 | 124 | */ |
aoqi@0 | 125 | public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); |
aoqi@0 | 126 | |
aoqi@0 | 127 | /** |
aoqi@0 | 128 | * Gets the path to tree node within the specified compilation unit. |
aoqi@0 | 129 | */ |
aoqi@0 | 130 | public abstract TreePath getPath(CompilationUnitTree unit, Tree node); |
aoqi@0 | 131 | |
aoqi@0 | 132 | /** |
aoqi@0 | 133 | * Gets the TreePath node for a given Element. |
aoqi@0 | 134 | * Returns null if the node can not be found. |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | public abstract TreePath getPath(Element e); |
aoqi@0 | 137 | |
aoqi@0 | 138 | /** |
aoqi@0 | 139 | * Gets the TreePath node for an AnnotationMirror on a given Element. |
aoqi@0 | 140 | * Returns null if the node can not be found. |
aoqi@0 | 141 | */ |
aoqi@0 | 142 | public abstract TreePath getPath(Element e, AnnotationMirror a); |
aoqi@0 | 143 | |
aoqi@0 | 144 | /** |
aoqi@0 | 145 | * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element. |
aoqi@0 | 146 | * Returns null if the node can not be found. |
aoqi@0 | 147 | */ |
aoqi@0 | 148 | public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); |
aoqi@0 | 149 | |
aoqi@0 | 150 | /** |
aoqi@0 | 151 | * Gets the Element for the Tree node identified by a given TreePath. |
aoqi@0 | 152 | * Returns null if the element is not available. |
aoqi@0 | 153 | * @throws IllegalArgumentException is the TreePath does not identify |
aoqi@0 | 154 | * a Tree node that might have an associated Element. |
aoqi@0 | 155 | */ |
aoqi@0 | 156 | public abstract Element getElement(TreePath path); |
aoqi@0 | 157 | |
aoqi@0 | 158 | /** |
aoqi@0 | 159 | * Gets the TypeMirror for the Tree node identified by a given TreePath. |
aoqi@0 | 160 | * Returns null if the TypeMirror is not available. |
aoqi@0 | 161 | * @throws IllegalArgumentException is the TreePath does not identify |
aoqi@0 | 162 | * a Tree node that might have an associated TypeMirror. |
aoqi@0 | 163 | */ |
aoqi@0 | 164 | public abstract TypeMirror getTypeMirror(TreePath path); |
aoqi@0 | 165 | |
aoqi@0 | 166 | /** |
aoqi@0 | 167 | * Gets the Scope for the Tree node identified by a given TreePath. |
aoqi@0 | 168 | * Returns null if the Scope is not available. |
aoqi@0 | 169 | */ |
aoqi@0 | 170 | public abstract Scope getScope(TreePath path); |
aoqi@0 | 171 | |
aoqi@0 | 172 | /** |
aoqi@0 | 173 | * Gets the doc comment, if any, for the Tree node identified by a given TreePath. |
aoqi@0 | 174 | * Returns null if no doc comment was found. |
aoqi@0 | 175 | * @see DocTrees#getDocCommentTree(TreePath) |
aoqi@0 | 176 | */ |
aoqi@0 | 177 | public abstract String getDocComment(TreePath path); |
aoqi@0 | 178 | |
aoqi@0 | 179 | /** |
aoqi@0 | 180 | * Checks whether a given type is accessible in a given scope. |
aoqi@0 | 181 | * @param scope the scope to be checked |
aoqi@0 | 182 | * @param type the type to be checked |
aoqi@0 | 183 | * @return true if {@code type} is accessible |
aoqi@0 | 184 | */ |
aoqi@0 | 185 | public abstract boolean isAccessible(Scope scope, TypeElement type); |
aoqi@0 | 186 | |
aoqi@0 | 187 | /** |
aoqi@0 | 188 | * Checks whether the given element is accessible as a member of the given |
aoqi@0 | 189 | * type in a given scope. |
aoqi@0 | 190 | * @param scope the scope to be checked |
aoqi@0 | 191 | * @param member the member to be checked |
aoqi@0 | 192 | * @param type the type for which to check if the member is accessible |
aoqi@0 | 193 | * @return true if {@code member} is accessible in {@code type} |
aoqi@0 | 194 | */ |
aoqi@0 | 195 | public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type); |
aoqi@0 | 196 | |
aoqi@0 | 197 | /** |
aoqi@0 | 198 | * Gets the original type from the ErrorType object. |
aoqi@0 | 199 | * @param errorType The errorType for which we want to get the original type. |
aoqi@0 | 200 | * @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType. |
aoqi@0 | 201 | */ |
aoqi@0 | 202 | public abstract TypeMirror getOriginalType(ErrorType errorType); |
aoqi@0 | 203 | |
aoqi@0 | 204 | /** |
aoqi@0 | 205 | * Prints a message of the specified kind at the location of the |
aoqi@0 | 206 | * tree within the provided compilation unit |
aoqi@0 | 207 | * |
aoqi@0 | 208 | * @param kind the kind of message |
aoqi@0 | 209 | * @param msg the message, or an empty string if none |
aoqi@0 | 210 | * @param t the tree to use as a position hint |
aoqi@0 | 211 | * @param root the compilation unit that contains tree |
aoqi@0 | 212 | */ |
aoqi@0 | 213 | public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg, |
aoqi@0 | 214 | com.sun.source.tree.Tree t, |
aoqi@0 | 215 | com.sun.source.tree.CompilationUnitTree root); |
aoqi@0 | 216 | |
aoqi@0 | 217 | /** |
aoqi@0 | 218 | * Gets the lub of an exception parameter declared in a catch clause. |
aoqi@0 | 219 | * @param tree the tree for the catch clause |
aoqi@0 | 220 | * @return The lub of the exception parameter |
aoqi@0 | 221 | */ |
aoqi@0 | 222 | public abstract TypeMirror getLub(CatchTree tree); |
aoqi@0 | 223 | } |