Path: blob/master/src/jdk.compiler/share/classes/com/sun/source/util/Trees.java
41175 views
/*1* Copyright (c) 2005, 2020, 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*/53public abstract class Trees {54/**55* Constructor for subclasses to call.56*/57public Trees() {}5859/**60* Returns a Trees object for a given CompilationTask.61* @param task the compilation task for which to get the Trees object62* @throws IllegalArgumentException if the task does not support the Trees API.63* @return the Trees object64*/65public static Trees instance(CompilationTask task) {66String taskClassName = task.getClass().getName();67if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl")68&& !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask"))69throw new IllegalArgumentException();70return getJavacTrees(CompilationTask.class, task);71}7273/**74* Returns a Trees object for a given ProcessingEnvironment.75* @param env the processing environment for which to get the Trees object76* @throws IllegalArgumentException if the env does not support the Trees API.77* @return the Trees object78*/79public static Trees instance(ProcessingEnvironment env) {80if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))81throw new IllegalArgumentException();82return getJavacTrees(ProcessingEnvironment.class, env);83}8485static Trees getJavacTrees(Class<?> argType, Object arg) {86try {87ClassLoader cl = arg.getClass().getClassLoader();88Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);89argType = Class.forName(argType.getName(), false, cl);90Method m = c.getMethod("instance", argType);91return (Trees) m.invoke(null, arg);92} catch (ReflectiveOperationException e) {93throw new AssertionError(e);94}95}9697/**98* Returns a utility object for obtaining source positions.99* @return the utility object for obtaining source positions100*/101public abstract SourcePositions getSourcePositions();102103/**104* Returns the Tree node for a given Element.105* Returns {@code null} if the node can not be found.106* @param element the element107* @return the tree node108*/109public abstract Tree getTree(Element element);110111/**112* Returns the ClassTree node for a given TypeElement.113* Returns {@code null} if the node can not be found.114* @param element the element115* @return the class tree node116*/117public abstract ClassTree getTree(TypeElement element);118119/**120* Returns the MethodTree node for a given ExecutableElement.121* Returns {@code null} if the node can not be found.122* @param method the executable element123* @return the method tree node124*/125public abstract MethodTree getTree(ExecutableElement method);126127/**128* Returns the Tree node for an AnnotationMirror on a given Element.129* Returns {@code null} if the node can not be found.130* @param e the element131* @param a the annotation mirror132* @return the tree node133*/134public abstract Tree getTree(Element e, AnnotationMirror a);135136/**137* Returns the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.138* Returns {@code null} if the node can not be found.139* @param e the element140* @param a the annotation mirror141* @param v the annotation value142* @return the tree node143*/144public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);145146/**147* Returns the path to tree node within the specified compilation unit.148* @param unit the compilation unit149* @param node the tree node150* @return the tree path151*/152public abstract TreePath getPath(CompilationUnitTree unit, Tree node);153154/**155* Returns the TreePath node for a given Element.156* Returns {@code null} if the node can not be found.157* @param e the element158* @return the tree path159*/160public abstract TreePath getPath(Element e);161162/**163* Returns the TreePath node for an AnnotationMirror on a given Element.164* Returns {@code null} if the node can not be found.165* @param e the element166* @param a the annotation mirror167* @return the tree path168*/169public abstract TreePath getPath(Element e, AnnotationMirror a);170171/**172* Returns the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.173* Returns {@code null} if the node can not be found.174* @param e the element175* @param a the annotation mirror176* @param v the annotation value177* @return the tree path178*/179public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);180181/**182* Returns the Element for the Tree node identified by a given TreePath.183* Returns {@code null} if the element is not available.184* @param path the tree path185* @return the element186* @throws IllegalArgumentException is the TreePath does not identify187* a Tree node that might have an associated Element.188*/189public abstract Element getElement(TreePath path);190191/**192* Returns the TypeMirror for the Tree node identified by a given TreePath.193* Returns {@code null} if the TypeMirror is not available.194* @param path the tree path195* @return the type mirror196* @throws IllegalArgumentException is the TreePath does not identify197* a Tree node that might have an associated TypeMirror.198*/199public abstract TypeMirror getTypeMirror(TreePath path);200201/**202* Returns the Scope for the Tree node identified by a given TreePath.203* Returns {@code null} if the Scope is not available.204* @param path the tree path205* @return the scope206*/207public abstract Scope getScope(TreePath path);208209/**210* Returns the doc comment, if any, for the Tree node identified by a given TreePath.211* Returns {@code null} if no doc comment was found.212* @see DocTrees#getDocCommentTree(TreePath)213* @param path the tree path214* @return the doc comment215*/216public abstract String getDocComment(TreePath path);217218/**219* Checks whether a given type is accessible in a given scope.220* @param scope the scope to be checked221* @param type the type to be checked222* @return true if {@code type} is accessible223*/224public abstract boolean isAccessible(Scope scope, TypeElement type);225226/**227* Checks whether the given element is accessible as a member of the given228* type in a given scope.229* @param scope the scope to be checked230* @param member the member to be checked231* @param type the type for which to check if the member is accessible232* @return true if {@code member} is accessible in {@code type}233*/234public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);235236/**237* Returns the original type from the ErrorType object.238* @param errorType the errorType for which we want to get the original type239* @return the type mirror corresponding to the original type, replaced by the ErrorType240*/241public abstract TypeMirror getOriginalType(ErrorType errorType);242243/**244* Prints a message of the specified kind at the location of the245* tree within the provided compilation unit246*247* @param kind the kind of message248* @param msg the message, or an empty string if none249* @param t the tree to use as a position hint250* @param root the compilation unit that contains tree251*/252public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,253com.sun.source.tree.Tree t,254com.sun.source.tree.CompilationUnitTree root);255256/**257* Returns the lub of an exception parameter declared in a catch clause.258* @param tree the tree for the catch clause259* @return the lub of the exception parameter260*/261public abstract TypeMirror getLub(CatchTree tree);262}263264265