src/share/classes/com/sun/source/util/Trees.java

Fri, 04 Oct 2013 10:00:28 -0700

author
darcy
date
Fri, 04 Oct 2013 10:00:28 -0700
changeset 2083
379c04c090cf
parent 1590
011cf7e0a148
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8025913: Rename jdk.Supported to jdk.Exported
Reviewed-by: psandoz, forax, lancea, alanb, mchung, jjg

duke@1 1 /*
darcy@1590 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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;
jjg@1409 29
duke@1 30 import javax.annotation.processing.ProcessingEnvironment;
duke@1 31 import javax.lang.model.element.AnnotationMirror;
duke@1 32 import javax.lang.model.element.AnnotationValue;
duke@1 33 import javax.lang.model.element.Element;
duke@1 34 import javax.lang.model.element.ExecutableElement;
duke@1 35 import javax.lang.model.element.TypeElement;
duke@1 36 import javax.lang.model.type.DeclaredType;
jjg@110 37 import javax.lang.model.type.ErrorType;
duke@1 38 import javax.lang.model.type.TypeMirror;
jjg@308 39 import javax.tools.Diagnostic;
duke@1 40 import javax.tools.JavaCompiler.CompilationTask;
duke@1 41
jjg@988 42 import com.sun.source.tree.CatchTree;
duke@1 43 import com.sun.source.tree.ClassTree;
duke@1 44 import com.sun.source.tree.CompilationUnitTree;
duke@1 45 import com.sun.source.tree.MethodTree;
duke@1 46 import com.sun.source.tree.Scope;
duke@1 47 import com.sun.source.tree.Tree;
duke@1 48
duke@1 49 /**
duke@1 50 * Bridges JSR 199, JSR 269, and the Tree API.
duke@1 51 *
duke@1 52 * @author Peter von der Ahé
duke@1 53 */
darcy@2083 54 @jdk.Exported
duke@1 55 public abstract class Trees {
duke@1 56 /**
duke@1 57 * Gets a Trees object for a given CompilationTask.
jjg@785 58 * @param task the compilation task for which to get the Trees object
duke@1 59 * @throws IllegalArgumentException if the task does not support the Trees API.
duke@1 60 */
duke@1 61 public static Trees instance(CompilationTask task) {
jjg@1416 62 String taskClassName = task.getClass().getName();
jjg@1416 63 if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl")
jjg@1416 64 && !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask"))
duke@1 65 throw new IllegalArgumentException();
duke@1 66 return getJavacTrees(CompilationTask.class, task);
duke@1 67 }
duke@1 68
duke@1 69 /**
jjg@785 70 * Gets a Trees object for a given ProcessingEnvironment.
jjg@785 71 * @param env the processing environment for which to get the Trees object
duke@1 72 * @throws IllegalArgumentException if the env does not support the Trees API.
duke@1 73 */
duke@1 74 public static Trees instance(ProcessingEnvironment env) {
duke@1 75 if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
duke@1 76 throw new IllegalArgumentException();
duke@1 77 return getJavacTrees(ProcessingEnvironment.class, env);
duke@1 78 }
duke@1 79
jjg@1409 80 static Trees getJavacTrees(Class<?> argType, Object arg) {
duke@1 81 try {
duke@1 82 ClassLoader cl = arg.getClass().getClassLoader();
duke@1 83 Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
duke@1 84 argType = Class.forName(argType.getName(), false, cl);
mcimadamore@184 85 Method m = c.getMethod("instance", new Class<?>[] { argType });
duke@1 86 return (Trees) m.invoke(null, new Object[] { arg });
duke@1 87 } catch (Throwable e) {
duke@1 88 throw new AssertionError(e);
duke@1 89 }
duke@1 90 }
duke@1 91
duke@1 92 /**
duke@1 93 * Gets a utility object for obtaining source positions.
duke@1 94 */
duke@1 95 public abstract SourcePositions getSourcePositions();
duke@1 96
duke@1 97 /**
duke@1 98 * Gets the Tree node for a given Element.
duke@1 99 * Returns null if the node can not be found.
duke@1 100 */
duke@1 101 public abstract Tree getTree(Element element);
duke@1 102
duke@1 103 /**
duke@1 104 * Gets the ClassTree node for a given TypeElement.
duke@1 105 * Returns null if the node can not be found.
duke@1 106 */
duke@1 107 public abstract ClassTree getTree(TypeElement element);
duke@1 108
duke@1 109 /**
duke@1 110 * Gets the MethodTree node for a given ExecutableElement.
duke@1 111 * Returns null if the node can not be found.
duke@1 112 */
duke@1 113 public abstract MethodTree getTree(ExecutableElement method);
duke@1 114
duke@1 115 /**
duke@1 116 * Gets the Tree node for an AnnotationMirror on a given Element.
duke@1 117 * Returns null if the node can not be found.
duke@1 118 */
duke@1 119 public abstract Tree getTree(Element e, AnnotationMirror a);
duke@1 120
duke@1 121 /**
duke@1 122 * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
duke@1 123 * Returns null if the node can not be found.
duke@1 124 */
duke@1 125 public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
duke@1 126
duke@1 127 /**
duke@1 128 * Gets the path to tree node within the specified compilation unit.
duke@1 129 */
duke@1 130 public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
duke@1 131
duke@1 132 /**
duke@1 133 * Gets the TreePath node for a given Element.
duke@1 134 * Returns null if the node can not be found.
duke@1 135 */
duke@1 136 public abstract TreePath getPath(Element e);
duke@1 137
duke@1 138 /**
duke@1 139 * Gets the TreePath node for an AnnotationMirror on a given Element.
duke@1 140 * Returns null if the node can not be found.
duke@1 141 */
duke@1 142 public abstract TreePath getPath(Element e, AnnotationMirror a);
duke@1 143
duke@1 144 /**
duke@1 145 * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
duke@1 146 * Returns null if the node can not be found.
duke@1 147 */
duke@1 148 public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
duke@1 149
duke@1 150 /**
duke@1 151 * Gets the Element for the Tree node identified by a given TreePath.
duke@1 152 * Returns null if the element 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 Element.
duke@1 155 */
duke@1 156 public abstract Element getElement(TreePath path);
duke@1 157
duke@1 158 /**
duke@1 159 * Gets the TypeMirror for the Tree node identified by a given TreePath.
duke@1 160 * Returns null if the TypeMirror is not available.
duke@1 161 * @throws IllegalArgumentException is the TreePath does not identify
duke@1 162 * a Tree node that might have an associated TypeMirror.
duke@1 163 */
duke@1 164 public abstract TypeMirror getTypeMirror(TreePath path);
duke@1 165
duke@1 166 /**
duke@1 167 * Gets the Scope for the Tree node identified by a given TreePath.
duke@1 168 * Returns null if the Scope is not available.
duke@1 169 */
duke@1 170 public abstract Scope getScope(TreePath path);
duke@1 171
duke@1 172 /**
jjg@783 173 * Gets the doc comment, if any, for the Tree node identified by a given TreePath.
jjg@783 174 * Returns null if no doc comment was found.
jjg@1409 175 * @see DocTrees#getDocCommentTree(TreePath)
jjg@783 176 */
jjg@783 177 public abstract String getDocComment(TreePath path);
jjg@783 178
jjg@783 179 /**
duke@1 180 * Checks whether a given type is accessible in a given scope.
duke@1 181 * @param scope the scope to be checked
duke@1 182 * @param type the type to be checked
duke@1 183 * @return true if {@code type} is accessible
duke@1 184 */
duke@1 185 public abstract boolean isAccessible(Scope scope, TypeElement type);
duke@1 186
duke@1 187 /**
duke@1 188 * Checks whether the given element is accessible as a member of the given
duke@1 189 * type in a given scope.
duke@1 190 * @param scope the scope to be checked
duke@1 191 * @param member the member to be checked
duke@1 192 * @param type the type for which to check if the member is accessible
duke@1 193 * @return true if {@code member} is accessible in {@code type}
duke@1 194 */
duke@1 195 public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
jjg@110 196
jjg@110 197 /**
jjg@110 198 * Gets the original type from the ErrorType object.
jjg@110 199 * @param errorType The errorType for which we want to get the original type.
jjg@308 200 * @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
jjg@110 201 */
jjg@110 202 public abstract TypeMirror getOriginalType(ErrorType errorType);
jjg@308 203
jjg@308 204 /**
jjg@308 205 * Prints a message of the specified kind at the location of the
jjg@308 206 * tree within the provided compilation unit
jjg@308 207 *
jjg@308 208 * @param kind the kind of message
jjg@308 209 * @param msg the message, or an empty string if none
jjg@308 210 * @param t the tree to use as a position hint
jjg@308 211 * @param root the compilation unit that contains tree
jjg@308 212 */
jjg@308 213 public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
jjg@308 214 com.sun.source.tree.Tree t,
jjg@308 215 com.sun.source.tree.CompilationUnitTree root);
jjg@988 216
jjg@988 217 /**
jjg@988 218 * Gets the lub of an exception parameter declared in a catch clause.
jjg@988 219 * @param tree the tree for the catch clause
jjg@988 220 * @return The lub of the exception parameter
jjg@988 221 */
jjg@988 222 public abstract TypeMirror getLub(CatchTree tree);
duke@1 223 }

mercurial