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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 110
91eea580fbe9
child 184
905e151a185a
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 2005-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any 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.JavaCompiler.CompilationTask;
    40 import com.sun.source.tree.ClassTree;
    41 import com.sun.source.tree.CompilationUnitTree;
    42 import com.sun.source.tree.MethodTree;
    43 import com.sun.source.tree.Scope;
    44 import com.sun.source.tree.Tree;
    46 /**
    47  * Bridges JSR 199, JSR 269, and the Tree API.
    48  *
    49  * @author Peter von der Ahé
    50  */
    51 public abstract class Trees {
    52     /**
    53      * Gets a Trees object for a given CompilationTask.
    54      * @throws IllegalArgumentException if the task does not support the Trees API.
    55      */
    56     public static Trees instance(CompilationTask task) {
    57         if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl"))
    58             throw new IllegalArgumentException();
    59         return getJavacTrees(CompilationTask.class, task);
    60     }
    62     /**
    63      * Gets a Trees object for a given CompilationTask.
    64      * @throws IllegalArgumentException if the env does not support the Trees API.
    65      */
    66     public static Trees instance(ProcessingEnvironment env) {
    67         if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
    68             throw new IllegalArgumentException();
    69         return getJavacTrees(ProcessingEnvironment.class, env);
    70     }
    72     private static Trees getJavacTrees(Class<?> argType, Object arg) {
    73         try {
    74             ClassLoader cl = arg.getClass().getClassLoader();
    75             Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
    76             argType = Class.forName(argType.getName(), false, cl);
    77             Method m = c.getMethod("instance", new Class[] { argType });
    78             return (Trees) m.invoke(null, new Object[] { arg });
    79         } catch (Throwable e) {
    80             throw new AssertionError(e);
    81         }
    82     }
    84     /**
    85      * Gets a utility object for obtaining source positions.
    86      */
    87     public abstract SourcePositions getSourcePositions();
    89     /**
    90      * Gets the Tree node for a given Element.
    91      * Returns null if the node can not be found.
    92      */
    93     public abstract Tree getTree(Element element);
    95     /**
    96      * Gets the ClassTree node for a given TypeElement.
    97      * Returns null if the node can not be found.
    98      */
    99     public abstract ClassTree getTree(TypeElement element);
   101     /**
   102      * Gets the MethodTree node for a given ExecutableElement.
   103      * Returns null if the node can not be found.
   104      */
   105     public abstract MethodTree getTree(ExecutableElement method);
   107     /**
   108      * Gets the Tree node for an AnnotationMirror on a given Element.
   109      * Returns null if the node can not be found.
   110      */
   111     public abstract Tree getTree(Element e, AnnotationMirror a);
   113     /**
   114      * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
   115      * Returns null if the node can not be found.
   116      */
   117     public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
   119     /**
   120      * Gets the path to tree node within the specified compilation unit.
   121      */
   122     public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
   124     /**
   125      * Gets the TreePath node for a given Element.
   126      * Returns null if the node can not be found.
   127      */
   128     public abstract TreePath getPath(Element e);
   130     /**
   131      * Gets the TreePath node for an AnnotationMirror on a given Element.
   132      * Returns null if the node can not be found.
   133      */
   134     public abstract TreePath getPath(Element e, AnnotationMirror a);
   136     /**
   137      * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
   138      * Returns null if the node can not be found.
   139      */
   140     public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
   142     /**
   143      * Gets the Element for the Tree node identified by a given TreePath.
   144      * Returns null if the element is not available.
   145      * @throws IllegalArgumentException is the TreePath does not identify
   146      * a Tree node that might have an associated Element.
   147      */
   148     public abstract Element getElement(TreePath path);
   150     /**
   151      * Gets the TypeMirror for the Tree node identified by a given TreePath.
   152      * Returns null if the TypeMirror is not available.
   153      * @throws IllegalArgumentException is the TreePath does not identify
   154      * a Tree node that might have an associated TypeMirror.
   155      */
   156     public abstract TypeMirror getTypeMirror(TreePath path);
   158     /**
   159      * Gets the Scope for the Tree node identified by a given TreePath.
   160      * Returns null if the Scope is not available.
   161      */
   162     public abstract Scope getScope(TreePath path);
   164     /**
   165      * Checks whether a given type is accessible in a given scope.
   166      * @param scope the scope to be checked
   167      * @param type the type to be checked
   168      * @return true if {@code type} is accessible
   169      */
   170     public abstract boolean isAccessible(Scope scope, TypeElement type);
   172     /**
   173      * Checks whether the given element is accessible as a member of the given
   174      * type in a given scope.
   175      * @param scope the scope to be checked
   176      * @param member the member to be checked
   177      * @param type the type for which to check if the member is accessible
   178      * @return true if {@code member} is accessible in {@code type}
   179      */
   180     public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
   182     /**
   183       * Gets the original type from the ErrorType object.
   184       * @param errorType The errorType for which we want to get the original type.
   185       * @returns javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
   186       */
   187     public abstract TypeMirror getOriginalType(ErrorType errorType);
   188 }

mercurial