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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 785
65820d0d4a97
child 988
7ae6c0fd479b
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

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

mercurial