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

Mon, 19 Nov 2012 11:38:49 -0800

author
jjg
date
Mon, 19 Nov 2012 11:38:49 -0800
changeset 1416
c0f0c41cafa0
parent 1409
33abf479f202
child 1590
011cf7e0a148
permissions
-rw-r--r--

8001098: Provide a simple light-weight "plug-in" mechanism for javac
Reviewed-by: mcimadamore

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

mercurial