langtools/src/share/classes/com/sun/source/util/Trees.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 1264 076a3cde30d5
child 3149 0cd06d598d6f
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 1257
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.source.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.annotation.processing.ProcessingEnvironment;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.element.AnnotationMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.AnnotationValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.ExecutableElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.type.DeclaredType;
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
    36
import javax.lang.model.type.ErrorType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.JavaCompiler.CompilationTask;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.source.tree.ClassTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.source.tree.CompilationUnitTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.source.tree.MethodTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.source.tree.Scope;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.source.tree.Tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * Bridges JSR 199, JSR 269, and the Tree API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @author Peter von der Ahé
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public abstract class Trees {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * Gets a Trees object for a given CompilationTask.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * @throws IllegalArgumentException if the task does not support the Trees API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public static Trees instance(CompilationTask task) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl"))
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        return getJavacTrees(CompilationTask.class, task);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Gets a Trees object for a given CompilationTask.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * @throws IllegalArgumentException if the env does not support the Trees API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public static Trees instance(ProcessingEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        return getJavacTrees(ProcessingEnvironment.class, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private static Trees getJavacTrees(Class<?> argType, Object arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            ClassLoader cl = arg.getClass().getClassLoader();
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            Class<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            argType = Class.forName(argType.getName(), false, cl);
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    77
            Method m = c.getMethod("instance", new Class<?>[] { argType });
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            return (Trees) m.invoke(null, new Object[] { arg });
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * Gets a utility object for obtaining source positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public abstract SourcePositions getSourcePositions();
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * Gets the Tree node for a given Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public abstract Tree getTree(Element element);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * Gets the ClassTree node for a given TypeElement.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public abstract ClassTree getTree(TypeElement element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * Gets the MethodTree node for a given ExecutableElement.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public abstract MethodTree getTree(ExecutableElement method);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Gets the Tree node for an AnnotationMirror on a given Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    public abstract Tree getTree(Element e, AnnotationMirror a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     * Gets the path to tree node within the specified compilation unit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * Gets the TreePath node for a given Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    public abstract TreePath getPath(Element e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     * Gets the TreePath node for an AnnotationMirror on a given Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public abstract TreePath getPath(Element e, AnnotationMirror a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * Returns null if the node can not be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     * Gets the Element for the Tree node identified by a given TreePath.
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * Returns null if the element is not available.
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * @throws IllegalArgumentException is the TreePath does not identify
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     * a Tree node that might have an associated Element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    public abstract Element getElement(TreePath path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     * Gets the TypeMirror for the Tree node identified by a given TreePath.
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     * Returns null if the TypeMirror is not available.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     * @throws IllegalArgumentException is the TreePath does not identify
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     * a Tree node that might have an associated TypeMirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    public abstract TypeMirror getTypeMirror(TreePath path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     * Gets the Scope for the Tree node identified by a given TreePath.
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * Returns null if the Scope is not available.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    public abstract Scope getScope(TreePath path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     * Checks whether a given type is accessible in a given scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     * @param scope the scope to be checked
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * @param type the type to be checked
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     * @return true if {@code type} is accessible
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    public abstract boolean isAccessible(Scope scope, TypeElement type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
     * Checks whether the given element is accessible as a member of the given
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * type in a given scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * @param scope the scope to be checked
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * @param member the member to be checked
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * @param type the type for which to check if the member is accessible
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     * @return true if {@code member} is accessible in {@code type}
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   181
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   182
    /**
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   183
      * Gets the original type from the ErrorType object.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   184
      * @param errorType The errorType for which we want to get the original type.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   185
      * @returns javax.lang.model.type.TypeMirror corresponding to the original type, replaced by the ErrorType.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   186
      */
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   187
    public abstract TypeMirror getOriginalType(ErrorType errorType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
}