langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java
author jjg
Tue, 28 Feb 2012 10:33:49 -0800
changeset 12016 1990493b64db
parent 11055 ec1418effa77
child 13077 16fb753bb5dc
permissions -rw-r--r--
7093891: support multiple task listeners Reviewed-by: darcy, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
     2
 * Copyright (c) 2005, 2012, 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;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    29
10
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;
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
    37
import javax.lang.model.type.TypeKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.type.TypeMirror;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
    39
import javax.tools.Diagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
    43
import com.sun.source.tree.CatchTree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.source.tree.CompilationUnitTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.source.tree.Scope;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.source.tree.Tree;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    47
import com.sun.source.util.JavacTask;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.source.util.SourcePositions;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.source.util.TreePath;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
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
    51
import com.sun.tools.javac.code.Flags;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    52
import com.sun.tools.javac.code.Symbol;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.code.Symbol.TypeSymbol;
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
    55
import com.sun.tools.javac.code.Type.UnionClassType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import com.sun.tools.javac.comp.Attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import com.sun.tools.javac.comp.MemberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import com.sun.tools.javac.comp.Resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
import com.sun.tools.javac.model.JavacElements;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
    63
import com.sun.tools.javac.parser.EndPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    65
import com.sun.tools.javac.tree.JCTree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
import com.sun.tools.javac.tree.TreeCopier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
import com.sun.tools.javac.tree.TreeMaker;
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
    70
import com.sun.tools.javac.util.Assert;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
import com.sun.tools.javac.util.Context;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
    72
import com.sun.tools.javac.util.JCDiagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
import com.sun.tools.javac.util.Log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 * Provides an implementation of Trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    80
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
public class JavacTrees extends Trees {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
6717
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    89
    // in a world of a single context per compilation, these would all be final
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    90
    private Resolve resolve;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    91
    private Enter enter;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    92
    private Log log;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    93
    private MemberEnter memberEnter;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    94
    private Attr attr;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    95
    private TreeMaker treeMaker;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    96
    private JavacElements elements;
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
    97
    private JavacTaskImpl javacTaskImpl;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
    99
    // called reflectively from Trees.instance(CompilationTask task)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    public static JavacTrees instance(JavaCompiler.CompilationTask task) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        if (!(task instanceof JavacTaskImpl))
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        return instance(((JavacTaskImpl)task).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   106
    // called reflectively from Trees.instance(ProcessingEnvironment env)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    public static JavacTrees instance(ProcessingEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        if (!(env instanceof JavacProcessingEnvironment))
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        return instance(((JavacProcessingEnvironment)env).getContext());
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public static JavacTrees instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        JavacTrees instance = context.get(JavacTrees.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            instance = new JavacTrees(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    private JavacTrees(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        context.put(JavacTrees.class, this);
6717
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   122
        init(context);
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   123
    }
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   124
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   125
    public void updateContext(Context context) {
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   126
        init(context);
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   127
    }
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   128
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   129
    private void init(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        elements = JavacElements.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        resolve = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        treeMaker = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        memberEnter = MemberEnter.instance(context);
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   137
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   138
        JavacTask t = context.get(JavacTask.class);
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   139
        if (t instanceof JavacTaskImpl)
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11055
diff changeset
   140
            javacTaskImpl = (JavacTaskImpl) t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public SourcePositions getSourcePositions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        return new SourcePositions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                public long getStartPosition(CompilationUnitTree file, Tree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                    return TreeInfo.getStartPos((JCTree) tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                public long getEndPosition(CompilationUnitTree file, Tree tree) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   150
                    EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   151
                    return TreeInfo.getEndPos((JCTree) tree, endPosTable);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    public JCClassDecl getTree(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        return (JCClassDecl) getTree((Element) element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    public JCMethodDecl getTree(ExecutableElement method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        return (JCMethodDecl) getTree((Element) method);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    public JCTree getTree(Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        Symbol symbol = (Symbol) element;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        TypeSymbol enclosing = symbol.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        Env<AttrContext> env = enter.getEnv(enclosing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        if (env == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        JCClassDecl classNode = env.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        if (classNode != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            if (TreeInfo.symbolFor(classNode) == element)
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                return classNode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            for (JCTree node : classNode.getMembers())
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                if (TreeInfo.symbolFor(node) == element)
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                    return node;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        return 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 JCTree getTree(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        return getTree(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 JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        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 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 TreePath getPath(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        return TreePath.getPath(unit, node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    public TreePath getPath(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        return getPath(e, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    public TreePath getPath(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return getPath(e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        if (treeTopLevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    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
   212
        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
   213
        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
   214
        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
   215
            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
   216
                JCTree t = (JCTree) p.getLeaf();
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10631
diff changeset
   217
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   218
                    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
   219
                    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
   220
                        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
   221
                            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
   222
                            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
   223
                        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   224
                        break;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   225
                    }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   226
                }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   227
            }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   228
        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5847
diff changeset
   229
        return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    public TypeMirror getTypeMirror(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        Tree t = path.getLeaf();
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        return ((JCTree)t).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    public JavacScope getScope(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        return new JavacScope(getAttrContext(path));
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   241
    public String getDocComment(TreePath path) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   242
        CompilationUnitTree t = path.getCompilationUnit();
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   243
        if (t instanceof JCTree.JCCompilationUnit) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   244
            JCCompilationUnit cu = (JCCompilationUnit) t;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   245
            if (cu.docComments != null) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   246
                return cu.docComments.get(path.getLeaf());
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   247
            }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   248
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   249
        return null;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   250
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents: 7209
diff changeset
   251
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    public boolean isAccessible(Scope scope, TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        if (scope instanceof JavacScope && type instanceof ClassSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            Env<AttrContext> env = ((JavacScope) scope).env;
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 7077
diff changeset
   255
            return resolve.isAccessible(env, (ClassSymbol)type, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        if (scope instanceof JavacScope
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                && member instanceof Symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                && type instanceof com.sun.tools.javac.code.Type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            Env<AttrContext> env = ((JavacScope) scope).env;
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 7077
diff changeset
   265
            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    private Env<AttrContext> getAttrContext(TreePath path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        if (!(path.getLeaf() instanceof JCTree))  // implicit null-check
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   274
        // if we're being invoked from a Tree API client via parse/enter/analyze,
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   275
        // we need to make sure all the classes have been entered;
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   276
        // if we're being invoked from JSR 199 or JSR 269, then the classes
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   277
        // will already have been entered.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        if (javacTaskImpl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                javacTaskImpl.enter(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                throw new Error("unexpected error while entering symbols: " + e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        Copier copier = new Copier(treeMaker.forToplevel(unit));
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        Env<AttrContext> env = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        JCMethodDecl method = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        JCVariableDecl field = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        List<Tree> l = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        TreePath p = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        while (p != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            l = l.prepend(p.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            p = p.getParentPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        for ( ; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            Tree tree = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            switch (tree.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                case COMPILATION_UNIT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
//                    System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                    env = enter.getTopLevelEnv((JCCompilationUnit)tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                    break;
7077
55689b828dc5 6993304: JavacTrees.getAttrContext not updated to Tree.Kind.{ANNOTATION_TYPE,ENUM,INTERFACE}
jjg
parents: 6717
diff changeset
   308
                case ANNOTATION_TYPE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                case CLASS:
7077
55689b828dc5 6993304: JavacTrees.getAttrContext not updated to Tree.Kind.{ANNOTATION_TYPE,ENUM,INTERFACE}
jjg
parents: 6717
diff changeset
   310
                case ENUM:
6600
b3bb16faccc2 6985181: Annotations lost from classfile
jjg
parents: 6590
diff changeset
   311
                case INTERFACE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
//                    System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    env = enter.getClassEnv(((JCClassDecl)tree).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
//                    System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                    method = (JCMethodDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                case VARIABLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
//                    System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                    field = (JCVariableDecl)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                case BLOCK: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
//                    System.err.println("BLOCK: ");
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   325
                    if (method != null) {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   326
                        try {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   327
                            Assert.check(method.body == tree);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   328
                            method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   329
                            env = memberEnter.getMethodEnv(method, env);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   330
                            env = attribStatToTree(method.body, env, copier.leafCopy);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   331
                        } finally {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   332
                            method.body = (JCBlock) tree;
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   333
                        }
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   334
                    } else {
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   335
                        JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   336
                        env = attribStatToTree(body, env, copier.leafCopy);
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   337
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                    return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
//                    System.err.println("DEFAULT: " + tree.getKind());
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                    if (field != null && field.getInitializer() == tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                        env = memberEnter.getInitEnv(field, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                        JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                        env = attribExprToTree(expr, env, copier.leafCopy);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        }
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 9599
diff changeset
   350
        return (field != null) ? memberEnter.getInitEnv(field, env) : env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            return attr.attribStatToTree(stat, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            return attr.attribExprToTree(expr, env, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
     * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    static class Copier extends TreeCopier<JCTree> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        JCTree leafCopy = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        Copier(TreeMaker M) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            super(M);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
6717
0103d76cfe48 6986246: Trees object is round-specific
jjg
parents: 6600
diff changeset
   381
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        public <T extends JCTree> T copy(T t, JCTree leaf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            T t2 = super.copy(t, leaf);
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            if (t == leaf)
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                leafCopy = t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            return t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    }
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
   389
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   390
    /**
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   391
     * 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
   392
     * @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
   393
     * @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
   394
     *          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
   395
     */
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   396
    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
   397
        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
   398
            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
   399
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   400
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 10
diff changeset
   401
        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
   402
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   403
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   404
    /**
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   405
     * Prints a message of the specified kind at the location of the
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   406
     * tree within the provided compilation unit
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   407
     *
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   408
     * @param kind the kind of message
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   409
     * @param msg  the message, or an empty string if none
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   410
     * @param t    the tree to use as a position hint
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   411
     * @param root the compilation unit that contains tree
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   412
     */
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   413
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   414
            com.sun.source.tree.Tree t,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   415
            com.sun.source.tree.CompilationUnitTree root) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   416
        JavaFileObject oldSource = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   417
        JavaFileObject newSource = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   418
        JCDiagnostic.DiagnosticPosition pos = null;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   419
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   420
        newSource = root.getSourceFile();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   421
        if (newSource != null) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   422
            oldSource = log.useSource(newSource);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   423
            pos = ((JCTree) t).pos();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   424
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   425
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   426
        try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   427
            switch (kind) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   428
            case ERROR:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   429
                boolean prev = log.multipleErrors;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   430
                try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   431
                    log.error(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   432
                } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   433
                    log.multipleErrors = prev;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   434
                }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   435
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   436
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   437
            case WARNING:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   438
                log.warning(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   439
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   440
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   441
            case MANDATORY_WARNING:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   442
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   443
                break;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   444
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   445
            default:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   446
                log.note(pos, "proc.messager", msg.toString());
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   447
            }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   448
        } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   449
            if (oldSource != null)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   450
                log.useSource(oldSource);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   451
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   452
    }
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   453
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   454
    @Override
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   455
    public TypeMirror getLub(CatchTree tree) {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   456
        JCCatch ct = (JCCatch) tree;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   457
        JCVariableDecl v = ct.param;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   458
        if (v.type != null && v.type.getKind() == TypeKind.UNION) {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   459
            UnionClassType ut = (UnionClassType) v.type;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   460
            return ut.getLub();
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   461
        } else {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   462
            return v.type;
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   463
        }
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 7631
diff changeset
   464
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
}