Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/source/util/Trees.java
38899 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.source.util;2627import java.lang.reflect.Method;2829import javax.annotation.processing.ProcessingEnvironment;30import javax.lang.model.element.AnnotationMirror;31import javax.lang.model.element.AnnotationValue;32import javax.lang.model.element.Element;33import javax.lang.model.element.ExecutableElement;34import javax.lang.model.element.TypeElement;35import javax.lang.model.type.DeclaredType;36import javax.lang.model.type.ErrorType;37import javax.lang.model.type.TypeMirror;38import javax.tools.Diagnostic;39import javax.tools.JavaCompiler.CompilationTask;4041import com.sun.source.tree.CatchTree;42import com.sun.source.tree.ClassTree;43import com.sun.source.tree.CompilationUnitTree;44import com.sun.source.tree.MethodTree;45import com.sun.source.tree.Scope;46import com.sun.source.tree.Tree;4748/**49* Bridges JSR 199, JSR 269, and the Tree API.50*51* @author Peter von der Ahé52*/53@jdk.Exported54public abstract class Trees {55/**56* Gets a Trees object for a given CompilationTask.57* @param task the compilation task for which to get the Trees object58* @throws IllegalArgumentException if the task does not support the Trees API.59*/60public static Trees instance(CompilationTask task) {61String taskClassName = task.getClass().getName();62if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl")63&& !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask"))64throw new IllegalArgumentException();65return getJavacTrees(CompilationTask.class, task);66}6768/**69* Gets a Trees object for a given ProcessingEnvironment.70* @param env the processing environment for which to get the Trees object71* @throws IllegalArgumentException if the env does not support the Trees API.72*/73public static Trees instance(ProcessingEnvironment env) {74if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))75throw new IllegalArgumentException();76return getJavacTrees(ProcessingEnvironment.class, env);77}7879static Trees getJavacTrees(Class<?> argType, Object arg) {80try {81ClassLoader cl = arg.getClass().getClassLoader();82Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);83argType = Class.forName(argType.getName(), false, cl);84Method m = c.getMethod("instance", new Class<?>[] { argType });85return (Trees) m.invoke(null, new Object[] { arg });86} catch (Throwable e) {87throw new AssertionError(e);88}89}9091/**92* Gets a utility object for obtaining source positions.93*/94public abstract SourcePositions getSourcePositions();9596/**97* Gets the Tree node for a given Element.98* Returns null if the node can not be found.99*/100public abstract Tree getTree(Element element);101102/**103* Gets the ClassTree node for a given TypeElement.104* Returns null if the node can not be found.105*/106public abstract ClassTree getTree(TypeElement element);107108/**109* Gets the MethodTree node for a given ExecutableElement.110* Returns null if the node can not be found.111*/112public abstract MethodTree getTree(ExecutableElement method);113114/**115* Gets the Tree node for an AnnotationMirror on a given Element.116* Returns null if the node can not be found.117*/118public abstract Tree getTree(Element e, AnnotationMirror a);119120/**121* Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.122* Returns null if the node can not be found.123*/124public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);125126/**127* Gets the path to tree node within the specified compilation unit.128*/129public abstract TreePath getPath(CompilationUnitTree unit, Tree node);130131/**132* Gets the TreePath node for a given Element.133* Returns null if the node can not be found.134*/135public abstract TreePath getPath(Element e);136137/**138* Gets the TreePath node for an AnnotationMirror on a given Element.139* Returns null if the node can not be found.140*/141public abstract TreePath getPath(Element e, AnnotationMirror a);142143/**144* Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.145* Returns null if the node can not be found.146*/147public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);148149/**150* Gets the Element for the Tree node identified by a given TreePath.151* Returns null if the element is not available.152* @throws IllegalArgumentException is the TreePath does not identify153* a Tree node that might have an associated Element.154*/155public abstract Element getElement(TreePath path);156157/**158* Gets the TypeMirror for the Tree node identified by a given TreePath.159* Returns null if the TypeMirror is not available.160* @throws IllegalArgumentException is the TreePath does not identify161* a Tree node that might have an associated TypeMirror.162*/163public abstract TypeMirror getTypeMirror(TreePath path);164165/**166* Gets the Scope for the Tree node identified by a given TreePath.167* Returns null if the Scope is not available.168*/169public abstract Scope getScope(TreePath path);170171/**172* Gets the doc comment, if any, for the Tree node identified by a given TreePath.173* Returns null if no doc comment was found.174* @see DocTrees#getDocCommentTree(TreePath)175*/176public abstract String getDocComment(TreePath path);177178/**179* Checks whether a given type is accessible in a given scope.180* @param scope the scope to be checked181* @param type the type to be checked182* @return true if {@code type} is accessible183*/184public abstract boolean isAccessible(Scope scope, TypeElement type);185186/**187* Checks whether the given element is accessible as a member of the given188* type in a given scope.189* @param scope the scope to be checked190* @param member the member to be checked191* @param type the type for which to check if the member is accessible192* @return true if {@code member} is accessible in {@code type}193*/194public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);195196/**197* Gets the original type from the ErrorType object.198* @param errorType The errorType for which we want to get the original type.199* @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.200*/201public abstract TypeMirror getOriginalType(ErrorType errorType);202203/**204* Prints a message of the specified kind at the location of the205* tree within the provided compilation unit206*207* @param kind the kind of message208* @param msg the message, or an empty string if none209* @param t the tree to use as a position hint210* @param root the compilation unit that contains tree211*/212public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,213com.sun.source.tree.Tree t,214com.sun.source.tree.CompilationUnitTree root);215216/**217* Gets the lub of an exception parameter declared in a catch clause.218* @param tree the tree for the catch clause219* @return The lub of the exception parameter220*/221public abstract TypeMirror getLub(CatchTree tree);222}223224225