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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 308
03944ee4fac4
child 783
90914ac50868
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.source.util;
    28 import java.lang.reflect.Method;
    29 import javax.annotation.processing.ProcessingEnvironment;
    30 import javax.lang.model.element.AnnotationMirror;
    31 import javax.lang.model.element.AnnotationValue;
    32 import javax.lang.model.element.Element;
    33 import javax.lang.model.element.ExecutableElement;
    34 import javax.lang.model.element.TypeElement;
    35 import javax.lang.model.type.DeclaredType;
    36 import javax.lang.model.type.ErrorType;
    37 import javax.lang.model.type.TypeMirror;
    38 import javax.tools.Diagnostic;
    39 import javax.tools.JavaCompiler.CompilationTask;
    41 import com.sun.source.tree.ClassTree;
    42 import com.sun.source.tree.CompilationUnitTree;
    43 import com.sun.source.tree.MethodTree;
    44 import com.sun.source.tree.Scope;
    45 import com.sun.source.tree.Tree;
    47 /**
    48  * Bridges JSR 199, JSR 269, and the Tree API.
    49  *
    50  * @author Peter von der Ahé
    51  */
    52 public abstract class Trees {
    53     /**
    54      * Gets a Trees object for a given CompilationTask.
    55      * @throws IllegalArgumentException if the task does not support the Trees API.
    56      */
    57     public static Trees instance(CompilationTask task) {
    58         if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl"))
    59             throw new IllegalArgumentException();
    60         return getJavacTrees(CompilationTask.class, task);
    61     }
    63     /**
    64      * Gets a Trees object for a given CompilationTask.
    65      * @throws IllegalArgumentException if the env does not support the Trees API.
    66      */
    67     public static Trees instance(ProcessingEnvironment env) {
    68         if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
    69             throw new IllegalArgumentException();
    70         return getJavacTrees(ProcessingEnvironment.class, env);
    71     }
    73     private static Trees getJavacTrees(Class<?> argType, Object arg) {
    74         try {
    75             ClassLoader cl = arg.getClass().getClassLoader();
    76             Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
    77             argType = Class.forName(argType.getName(), false, cl);
    78             Method m = c.getMethod("instance", new Class<?>[] { argType });
    79             return (Trees) m.invoke(null, new Object[] { arg });
    80         } catch (Throwable e) {
    81             throw new AssertionError(e);
    82         }
    83     }
    85     /**
    86      * Gets a utility object for obtaining source positions.
    87      */
    88     public abstract SourcePositions getSourcePositions();
    90     /**
    91      * Gets the Tree node for a given Element.
    92      * Returns null if the node can not be found.
    93      */
    94     public abstract Tree getTree(Element element);
    96     /**
    97      * Gets the ClassTree node for a given TypeElement.
    98      * Returns null if the node can not be found.
    99      */
   100     public abstract ClassTree getTree(TypeElement element);
   102     /**
   103      * Gets the MethodTree node for a given ExecutableElement.
   104      * Returns null if the node can not be found.
   105      */
   106     public abstract MethodTree getTree(ExecutableElement method);
   108     /**
   109      * Gets the Tree node for an AnnotationMirror on a given Element.
   110      * Returns null if the node can not be found.
   111      */
   112     public abstract Tree getTree(Element e, AnnotationMirror a);
   114     /**
   115      * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
   116      * Returns null if the node can not be found.
   117      */
   118     public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
   120     /**
   121      * Gets the path to tree node within the specified compilation unit.
   122      */
   123     public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
   125     /**
   126      * Gets the TreePath node for a given Element.
   127      * Returns null if the node can not be found.
   128      */
   129     public abstract TreePath getPath(Element e);
   131     /**
   132      * Gets the TreePath node for an AnnotationMirror on a given Element.
   133      * Returns null if the node can not be found.
   134      */
   135     public abstract TreePath getPath(Element e, AnnotationMirror a);
   137     /**
   138      * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
   139      * Returns null if the node can not be found.
   140      */
   141     public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
   143     /**
   144      * Gets the Element for the Tree node identified by a given TreePath.
   145      * Returns null if the element is not available.
   146      * @throws IllegalArgumentException is the TreePath does not identify
   147      * a Tree node that might have an associated Element.
   148      */
   149     public abstract Element getElement(TreePath path);
   151     /**
   152      * Gets the TypeMirror for the Tree node identified by a given TreePath.
   153      * Returns null if the TypeMirror is not available.
   154      * @throws IllegalArgumentException is the TreePath does not identify
   155      * a Tree node that might have an associated TypeMirror.
   156      */
   157     public abstract TypeMirror getTypeMirror(TreePath path);
   159     /**
   160      * Gets the Scope for the Tree node identified by a given TreePath.
   161      * Returns null if the Scope is not available.
   162      */
   163     public abstract Scope getScope(TreePath path);
   165     /**
   166      * Checks whether a given type is accessible in a given scope.
   167      * @param scope the scope to be checked
   168      * @param type the type to be checked
   169      * @return true if {@code type} is accessible
   170      */
   171     public abstract boolean isAccessible(Scope scope, TypeElement type);
   173     /**
   174      * Checks whether the given element is accessible as a member of the given
   175      * type in a given scope.
   176      * @param scope the scope to be checked
   177      * @param member the member to be checked
   178      * @param type the type for which to check if the member is accessible
   179      * @return true if {@code member} is accessible in {@code type}
   180      */
   181     public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
   183     /**
   184       * Gets the original type from the ErrorType object.
   185       * @param errorType The errorType for which we want to get the original type.
   186       * @return javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
   187       */
   188     public abstract TypeMirror getOriginalType(ErrorType errorType);
   190     /**
   191      * Prints a message of the specified kind at the location of the
   192      * tree within the provided compilation unit
   193      *
   194      * @param kind the kind of message
   195      * @param msg  the message, or an empty string if none
   196      * @param t    the tree to use as a position hint
   197      * @param root the compilation unit that contains tree
   198      */
   199     public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
   200             com.sun.source.tree.Tree t,
   201             com.sun.source.tree.CompilationUnitTree root);
   202 }

mercurial