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