langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java
author jjg
Thu, 16 Sep 2010 09:56:25 -0700
changeset 6600 b3bb16faccc2
parent 6590 f745e683da2c
child 6717 0103d76cfe48
permissions -rw-r--r--
6985181: Annotations lost from classfile Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. 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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    23
 * questions.
10
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.tools.javac.api;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.annotation.processing.ProcessingEnvironment;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.AnnotationMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.AnnotationValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.element.ExecutableElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.type.TypeMirror;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
    38
import javax.tools.Diagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.source.tree.CompilationUnitTree;
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
import com.sun.source.util.SourcePositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.source.util.TreePath;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.source.util.Trees;
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
    48
import com.sun.tools.javac.code.Flags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.code.Symbol.TypeSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.code.Symbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.comp.Attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import com.sun.tools.javac.comp.MemberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.javac.comp.Resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import com.sun.tools.javac.model.JavacElements;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
    59
import com.sun.tools.javac.processing.JavacMessager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
import com.sun.tools.javac.tree.TreeCopier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
import com.sun.tools.javac.tree.TreeMaker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import com.sun.tools.javac.util.Context;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
    67
import com.sun.tools.javac.util.JCDiagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
import com.sun.tools.javac.util.Log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * Provides an implementation of Trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    75
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
public class JavacTrees extends Trees {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    private final Resolve resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    private final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    private final MemberEnter memberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    private final TreeMaker treeMaker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    private final JavacElements elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    private final JavacTaskImpl javacTaskImpl;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public static JavacTrees instance(JavaCompiler.CompilationTask task) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        if (!(task instanceof JavacTaskImpl))
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        return instance(((JavacTaskImpl)task).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public static JavacTrees instance(ProcessingEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        if (!(env instanceof JavacProcessingEnvironment))
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        return instance(((JavacProcessingEnvironment)env).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public static JavacTrees instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        JavacTrees instance = context.get(JavacTrees.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            instance = new JavacTrees(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    private JavacTrees(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        context.put(JavacTrees.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        elements = JavacElements.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        resolve = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        treeMaker = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        memberEnter = MemberEnter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        javacTaskImpl = context.get(JavacTaskImpl.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    public SourcePositions getSourcePositions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        return new SourcePositions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                public long getStartPosition(CompilationUnitTree file, Tree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                    return TreeInfo.getStartPos((JCTree) tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                public long getEndPosition(CompilationUnitTree file, Tree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                    Map<JCTree,Integer> endPositions = ((JCCompilationUnit) file).endPositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                    return TreeInfo.getEndPos((JCTree) tree, endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    public JCClassDecl getTree(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return (JCClassDecl) getTree((Element) element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    public JCMethodDecl getTree(ExecutableElement method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        return (JCMethodDecl) getTree((Element) method);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    public JCTree getTree(Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        Symbol symbol = (Symbol) element;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        TypeSymbol enclosing = symbol.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        Env<AttrContext> env = enter.getEnv(enclosing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        if (env == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        JCClassDecl classNode = env.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        if (classNode != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            if (TreeInfo.symbolFor(classNode) == element)
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                return classNode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            for (JCTree node : classNode.getMembers())
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                if (TreeInfo.symbolFor(node) == element)
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                    return node;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    public JCTree getTree(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        return getTree(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        return treeTopLevel.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    public TreePath getPath(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        return TreePath.getPath(unit, node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    public TreePath getPath(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        return getPath(e, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public TreePath getPath(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        return getPath(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    public Element getElement(TreePath path) {
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   193
        JCTree tree = (JCTree) path.getLeaf();
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   194
        Symbol sym = TreeInfo.symbolFor(tree);
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   195
        if (sym == null && TreeInfo.isDeclaration(tree)) {
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   196
            for (TreePath p = path; p != null; p = p.getParentPath()) {
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   197
                JCTree t = (JCTree) p.getLeaf();
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   198
                if (t.getTag() == JCTree.CLASSDEF) {
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   199
                    JCClassDecl ct = (JCClassDecl) t;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   200
                    if (ct.sym != null) {
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   201
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   202
                            attr.attribClass(ct.pos(), ct.sym);
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   203
                            sym = TreeInfo.symbolFor(tree);
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   204
                        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   205
                        break;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   206
                    }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   207
                }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   208
            }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   209
        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   210
        return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    public TypeMirror getTypeMirror(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        Tree t = path.getLeaf();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        return ((JCTree)t).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    public JavacScope getScope(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        return new JavacScope(getAttrContext(path));
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    public boolean isAccessible(Scope scope, TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        if (scope instanceof JavacScope && type instanceof ClassSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            Env<AttrContext> env = ((JavacScope) scope).env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            return resolve.isAccessible(env, (ClassSymbol)type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        if (scope instanceof JavacScope
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                && member instanceof Symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                && type instanceof com.sun.tools.javac.code.Type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            Env<AttrContext> env = ((JavacScope) scope).env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member);
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    private Env<AttrContext> getAttrContext(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        if (!(path.getLeaf() instanceof JCTree))  // implicit null-check
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        // if we're being invoked via from a JSR199 client, we need to make sure
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        // all the classes have been entered; if we're being invoked from JSR269,
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        // then the classes will already have been entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        if (javacTaskImpl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                javacTaskImpl.enter(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                throw new Error("unexpected error while entering symbols: " + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        Copier copier = new Copier(treeMaker.forToplevel(unit));
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        Env<AttrContext> env = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        JCMethodDecl method = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        JCVariableDecl field = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        List<Tree> l = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        TreePath p = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        while (p != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            l = l.prepend(p.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            p = p.getParentPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        for ( ; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            Tree tree = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            switch (tree.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                case COMPILATION_UNIT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
//                    System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    env = enter.getTopLevelEnv((JCCompilationUnit)tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                case CLASS:
6600
b3bb16faccc2 6985181: Annotations lost from classfile
jjg
parents: 6590
diff changeset
   278
                case INTERFACE:
b3bb16faccc2 6985181: Annotations lost from classfile
jjg
parents: 6590
diff changeset
   279
                case ENUM:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
//                    System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                    env = enter.getClassEnv(((JCClassDecl)tree).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
//                    System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                    method = (JCMethodDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                case VARIABLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
//                    System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                    field = (JCVariableDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                case BLOCK: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
//                    System.err.println("BLOCK: ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                    if (method != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                        env = memberEnter.getMethodEnv(method, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                    JCTree body = copier.copy((JCTree)tree, (JCTree) path.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                    env = attribStatToTree(body, env, copier.leafCopy);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                    return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
//                    System.err.println("DEFAULT: " + tree.getKind());
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    if (field != null && field.getInitializer() == tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                        env = memberEnter.getInitEnv(field, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                        JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                        env = attribExprToTree(expr, env, copier.leafCopy);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        return field != null ? memberEnter.getInitEnv(field, env) : env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            return attr.attribStatToTree(stat, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            return attr.attribExprToTree(expr, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
     * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    static class Copier extends TreeCopier<JCTree> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        JCTree leafCopy = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        Copier(TreeMaker M) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            super(M);
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        public <T extends JCTree> T copy(T t, JCTree leaf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            T t2 = super.copy(t, leaf);
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            if (t == leaf)
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                leafCopy = t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            return t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    }
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
   347
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   348
    /**
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   349
     * 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
   350
     * @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
   351
     * @returns 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
   352
     *          noType (type.tag == NONE) is returned if there is no 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
   353
     */
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   354
    public TypeMirror getOriginalType(javax.lang.model.type.ErrorType 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
   355
        if (errorType instanceof com.sun.tools.javac.code.Type.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
   356
            return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   357
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   358
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   359
        return com.sun.tools.javac.code.Type.noType;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   360
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   361
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   362
    /**
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   363
     * Prints a message of the specified kind at the location of the
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   364
     * tree within the provided compilation unit
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   365
     *
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   366
     * @param kind the kind of message
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   367
     * @param msg  the message, or an empty string if none
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   368
     * @param t    the tree to use as a position hint
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   369
     * @param root the compilation unit that contains tree
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   370
     */
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   371
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   372
            com.sun.source.tree.Tree t,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   373
            com.sun.source.tree.CompilationUnitTree root) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   374
        JavaFileObject oldSource = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   375
        JavaFileObject newSource = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   376
        JCDiagnostic.DiagnosticPosition pos = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   377
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   378
        newSource = root.getSourceFile();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   379
        if (newSource != null) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   380
            oldSource = log.useSource(newSource);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   381
            pos = ((JCTree) t).pos();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   382
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   383
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   384
        try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   385
            switch (kind) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   386
            case ERROR:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   387
                boolean prev = log.multipleErrors;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   388
                try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   389
                    log.error(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   390
                } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   391
                    log.multipleErrors = prev;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   392
                }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   393
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   394
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   395
            case WARNING:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   396
                log.warning(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   397
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   398
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   399
            case MANDATORY_WARNING:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   400
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   401
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   402
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   403
            default:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   404
                log.note(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   405
            }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   406
        } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   407
            if (oldSource != null)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   408
                log.useSource(oldSource);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   409
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   410
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
}