duke@1: /* darcy@1590: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.source.util; duke@1: duke@1: import java.lang.reflect.Method; jjg@1409: duke@1: import javax.annotation.processing.ProcessingEnvironment; duke@1: import javax.lang.model.element.AnnotationMirror; duke@1: import javax.lang.model.element.AnnotationValue; duke@1: import javax.lang.model.element.Element; duke@1: import javax.lang.model.element.ExecutableElement; duke@1: import javax.lang.model.element.TypeElement; duke@1: import javax.lang.model.type.DeclaredType; jjg@110: import javax.lang.model.type.ErrorType; duke@1: import javax.lang.model.type.TypeMirror; jjg@308: import javax.tools.Diagnostic; duke@1: import javax.tools.JavaCompiler.CompilationTask; duke@1: jjg@988: import com.sun.source.tree.CatchTree; duke@1: import com.sun.source.tree.ClassTree; duke@1: import com.sun.source.tree.CompilationUnitTree; duke@1: import com.sun.source.tree.MethodTree; duke@1: import com.sun.source.tree.Scope; duke@1: import com.sun.source.tree.Tree; duke@1: duke@1: /** duke@1: * Bridges JSR 199, JSR 269, and the Tree API. duke@1: * duke@1: * @author Peter von der Ahé duke@1: */ darcy@2083: @jdk.Exported duke@1: public abstract class Trees { duke@1: /** duke@1: * Gets a Trees object for a given CompilationTask. jjg@785: * @param task the compilation task for which to get the Trees object duke@1: * @throws IllegalArgumentException if the task does not support the Trees API. duke@1: */ duke@1: public static Trees instance(CompilationTask task) { jjg@1416: String taskClassName = task.getClass().getName(); jjg@1416: if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl") jjg@1416: && !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask")) duke@1: throw new IllegalArgumentException(); duke@1: return getJavacTrees(CompilationTask.class, task); duke@1: } duke@1: duke@1: /** jjg@785: * Gets a Trees object for a given ProcessingEnvironment. jjg@785: * @param env the processing environment for which to get the Trees object duke@1: * @throws IllegalArgumentException if the env does not support the Trees API. duke@1: */ duke@1: public static Trees instance(ProcessingEnvironment env) { duke@1: if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) duke@1: throw new IllegalArgumentException(); duke@1: return getJavacTrees(ProcessingEnvironment.class, env); duke@1: } duke@1: jjg@1409: static Trees getJavacTrees(Class argType, Object arg) { duke@1: try { duke@1: ClassLoader cl = arg.getClass().getClassLoader(); duke@1: Class c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl); duke@1: argType = Class.forName(argType.getName(), false, cl); mcimadamore@184: Method m = c.getMethod("instance", new Class[] { argType }); duke@1: return (Trees) m.invoke(null, new Object[] { arg }); duke@1: } catch (Throwable e) { duke@1: throw new AssertionError(e); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Gets a utility object for obtaining source positions. duke@1: */ duke@1: public abstract SourcePositions getSourcePositions(); duke@1: duke@1: /** duke@1: * Gets the Tree node for a given Element. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract Tree getTree(Element element); duke@1: duke@1: /** duke@1: * Gets the ClassTree node for a given TypeElement. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract ClassTree getTree(TypeElement element); duke@1: duke@1: /** duke@1: * Gets the MethodTree node for a given ExecutableElement. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract MethodTree getTree(ExecutableElement method); duke@1: duke@1: /** duke@1: * Gets the Tree node for an AnnotationMirror on a given Element. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract Tree getTree(Element e, AnnotationMirror a); duke@1: duke@1: /** duke@1: * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v); duke@1: duke@1: /** duke@1: * Gets the path to tree node within the specified compilation unit. duke@1: */ duke@1: public abstract TreePath getPath(CompilationUnitTree unit, Tree node); duke@1: duke@1: /** duke@1: * Gets the TreePath node for a given Element. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract TreePath getPath(Element e); duke@1: duke@1: /** duke@1: * Gets the TreePath node for an AnnotationMirror on a given Element. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract TreePath getPath(Element e, AnnotationMirror a); duke@1: duke@1: /** duke@1: * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element. duke@1: * Returns null if the node can not be found. duke@1: */ duke@1: public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v); duke@1: duke@1: /** duke@1: * Gets the Element for the Tree node identified by a given TreePath. duke@1: * Returns null if the element is not available. duke@1: * @throws IllegalArgumentException is the TreePath does not identify duke@1: * a Tree node that might have an associated Element. duke@1: */ duke@1: public abstract Element getElement(TreePath path); duke@1: duke@1: /** duke@1: * Gets the TypeMirror for the Tree node identified by a given TreePath. duke@1: * Returns null if the TypeMirror is not available. duke@1: * @throws IllegalArgumentException is the TreePath does not identify duke@1: * a Tree node that might have an associated TypeMirror. duke@1: */ duke@1: public abstract TypeMirror getTypeMirror(TreePath path); duke@1: duke@1: /** duke@1: * Gets the Scope for the Tree node identified by a given TreePath. duke@1: * Returns null if the Scope is not available. duke@1: */ duke@1: public abstract Scope getScope(TreePath path); duke@1: duke@1: /** jjg@783: * Gets the doc comment, if any, for the Tree node identified by a given TreePath. jjg@783: * Returns null if no doc comment was found. jjg@1409: * @see DocTrees#getDocCommentTree(TreePath) jjg@783: */ jjg@783: public abstract String getDocComment(TreePath path); jjg@783: jjg@783: /** duke@1: * Checks whether a given type is accessible in a given scope. duke@1: * @param scope the scope to be checked duke@1: * @param type the type to be checked duke@1: * @return true if {@code type} is accessible duke@1: */ duke@1: public abstract boolean isAccessible(Scope scope, TypeElement type); duke@1: duke@1: /** duke@1: * Checks whether the given element is accessible as a member of the given duke@1: * type in a given scope. duke@1: * @param scope the scope to be checked duke@1: * @param member the member to be checked duke@1: * @param type the type for which to check if the member is accessible duke@1: * @return true if {@code member} is accessible in {@code type} duke@1: */ duke@1: public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type); jjg@110: jjg@110: /** jjg@110: * Gets the original type from the ErrorType object. jjg@110: * @param errorType The errorType for which we want to get the original type. jjg@308: * @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType. jjg@110: */ jjg@110: public abstract TypeMirror getOriginalType(ErrorType errorType); jjg@308: jjg@308: /** jjg@308: * Prints a message of the specified kind at the location of the jjg@308: * tree within the provided compilation unit jjg@308: * jjg@308: * @param kind the kind of message jjg@308: * @param msg the message, or an empty string if none jjg@308: * @param t the tree to use as a position hint jjg@308: * @param root the compilation unit that contains tree jjg@308: */ jjg@308: public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg, jjg@308: com.sun.source.tree.Tree t, jjg@308: com.sun.source.tree.CompilationUnitTree root); jjg@988: jjg@988: /** jjg@988: * Gets the lub of an exception parameter declared in a catch clause. jjg@988: * @param tree the tree for the catch clause jjg@988: * @return The lub of the exception parameter jjg@988: */ jjg@988: public abstract TypeMirror getLub(CatchTree tree); duke@1: }