langtools/src/share/classes/com/sun/tools/javac/model/JavacElements.java
author jjg
Tue, 08 Nov 2011 11:51:05 -0800
changeset 10950 e87b50888909
parent 7681 1f0819a3341f
child 13077 16fb753bb5dc
permissions -rw-r--r--
6921494: provide way to print javac tree tag values Reviewed-by: jjg, mcimadamore Contributed-by: vicenterz@yahoo.es
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
     2
 * Copyright (c) 2005, 2011, 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: 1264
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: 1264
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: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
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.model;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.lang.annotation.Annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.lang.annotation.Inherited;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.util.Elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.code.TypeTags;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.main.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.processing.PrintingProcessor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.tree.TreeScanner;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    48
import com.sun.tools.javac.util.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.util.Name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
    51
import static com.sun.tools.javac.tree.JCTree.Tag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import static javax.lang.model.util.ElementFilter.methodsIn;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * Utility methods for operating on program elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    57
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
public class JavacElements implements Elements {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    private JavaCompiler javaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private Symtab syms;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    66
    private Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    private Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public static JavacElements instance(Context context) {
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    71
        JavacElements instance = context.get(JavacElements.class);
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    72
        if (instance == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            instance = new JavacElements(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * Public for use only by JavacProcessingEnvironment
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    80
    protected JavacElements(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * Use a new context.  May be called from outside to update
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * internal state for a new annotation-processing round.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    public void setContext(Context context) {
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    89
        context.put(JavacElements.class, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        javaCompiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        syms = Symtab.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    92
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * An internal-use utility that creates a reified annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public static <A extends Annotation> A getAnnotation(Symbol annotated,
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                                                         Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        if (!annoType.isAnnotation())
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            throw new IllegalArgumentException("Not an annotation type: "
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                                               + annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        String name = annoType.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        for (Attribute.Compound anno : annotated.getAnnotationMirrors())
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            if (name.equals(anno.type.tsym.flatName().toString()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                return AnnotationProxyMaker.generateAnnotation(anno, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * An internal-use utility that creates a reified annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * This overloaded version take annotation inheritance into account.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                                                         Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        boolean inherited = annoType.isAnnotationPresent(Inherited.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        A result = null;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   121
        while (annotated.name != annotated.name.table.names.java_lang_Object) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            result = getAnnotation((Symbol)annotated, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            if (result != null || !inherited)
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            Type sup = annotated.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            if (sup.tag != TypeTags.CLASS || sup.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            annotated = (ClassSymbol) sup.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public PackageSymbol getPackageElement(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        String strName = name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        if (strName.equals(""))
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            return syms.unnamedPackage;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return SourceVersion.isName(strName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            ? nameToSymbol(strName, PackageSymbol.class)
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            : null;
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 ClassSymbol getTypeElement(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        String strName = name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return SourceVersion.isName(strName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            ? nameToSymbol(strName, ClassSymbol.class)
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     * Returns a symbol given the type's or packages's canonical name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     * or null if the name isn't found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    private <S extends Symbol> S nameToSymbol(String nameStr, Class<S> clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        Name name = names.fromString(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        // First check cache.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        Symbol sym = (clazz == ClassSymbol.class)
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                    ? syms.classes.get(name)
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                    : syms.packages.get(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            if (sym == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                sym = javaCompiler.resolveIdent(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            sym.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            return (sym.kind != Kinds.ERR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                    sym.exists() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                    clazz.isInstance(sym) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                    name.equals(sym.getQualifiedName()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                ? clazz.cast(sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        } catch (CompletionFailure e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    public JavacSourcePosition getSourcePosition(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        JavaFileObject sourcefile = toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        if (sourcefile == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        JavaFileObject sourcefile = toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        if (sourcefile == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        JCTree annoTree = matchAnnoToTree(a, e, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        if (annoTree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return new JavacSourcePosition(sourcefile, annoTree.pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                                       toplevel.lineMap);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                                            AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        // TODO: better accuracy in getSourcePosition(... AnnotationValue)
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        return getSourcePosition(e, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     * Returns the tree for an annotation given the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     * and the element's own tree.  Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    private JCTree matchAnnoToTree(AnnotationMirror findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                                   Element e, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        class Vis extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            List<JCAnnotation> result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            public void visitTopLevel(JCCompilationUnit tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                result = tree.packageAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        Vis vis = new Vis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        tree.accept(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        if (vis.result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        return matchAnnoToTree(cast(Attribute.Compound.class, findme),
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                               sym.getAnnotationMirrors(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                               vis.result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
     * Returns the tree for an annotation given a list of annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     * in which to search (recursively) and their corresponding trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     * Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    private JCTree matchAnnoToTree(Attribute.Compound findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                                   List<Attribute.Compound> annos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                                   List<JCAnnotation> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        for (Attribute.Compound anno : annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            for (JCAnnotation tree : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                JCTree match = matchAnnoToTree(findme, anno, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                if (match != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                    return match;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * Returns the tree for an annotation given an Attribute to
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * search (recursively) and its corresponding tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    private JCTree matchAnnoToTree(final Attribute.Compound findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                                   final Attribute attr,
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                                   final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        if (attr == findme)
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            return (tree.type.tsym == findme.type.tsym) ? tree : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        class Vis implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            JCTree result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            public void visitConstant(Attribute.Constant value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            public void visitCompound(Attribute.Compound anno) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                for (Pair<MethodSymbol, Attribute> pair : anno.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                    JCExpression expr = scanForAssign(pair.fst, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                    if (expr != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                        JCTree match = matchAnnoToTree(findme, pair.snd, expr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                        if (match != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                            result = match;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            public void visitArray(Attribute.Array array) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
   292
                if (tree.hasTag(NEWARRAY) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                        types.elemtype(array.type).tsym == findme.type.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                    List<JCExpression> elems = ((JCNewArray) tree).elems;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                    for (Attribute value : array.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                        if (value == findme) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                            result = elems.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                        elems = elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        Vis vis = new Vis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        attr.accept(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        return vis.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     * Scans for a JCAssign node with a LHS matching a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
     * symbol, and returns its RHS.  Does not scan nested JCAnnotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    private JCExpression scanForAssign(final MethodSymbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                                       final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        class TS extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            JCExpression result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            public void scan(JCTree t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                if (t != null && result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                    t.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            public void visitAnnotation(JCAnnotation t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                if (t == tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                    scan(t.args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            public void visitAssign(JCAssign t) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
   331
                if (t.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                    JCIdent ident = (JCIdent) t.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                    if (ident.sym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                        result = t.rhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        TS scanner = new TS();
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        tree.accept(scanner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        return scanner.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     * Returns the tree node corresponding to this element, or null
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     * if none can be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
    public JCTree getTree(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        return (treeTop != null) ? treeTop.fst : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    public String getDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        // Our doc comment is contained in a map in our toplevel,
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        // indexed by our tree.  Find our enter environment, which gives
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        // us our toplevel.  It also gives us a tree that contains our
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        // tree:  walk it to find our tree.  This is painful.
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        if (toplevel.docComments == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        return toplevel.docComments.get(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
    public PackageElement getPackageOf(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        return cast(Symbol.class, e).packge();
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    public boolean isDeprecated(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        return (sym.flags() & Flags.DEPRECATED) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    public Name getBinaryName(TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        return cast(TypeSymbol.class, type).flatName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                                                        AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        Attribute.Compound anno = cast(Attribute.Compound.class, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
        DeclaredType annotype = a.getAnnotationType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        for (ExecutableElement ex :
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                 methodsIn(annotype.asElement().getEnclosedElements())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            MethodSymbol meth = (MethodSymbol) ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            Attribute defaultValue = meth.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            if (defaultValue != null && !valmap.containsKey(meth)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                valmap.put(meth, defaultValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        return valmap;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    public FilteredMemberList getAllMembers(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        Symbol sym = cast(Symbol.class, element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        Scope scope = sym.members().dupUnshared();
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        List<Type> closure = types.closure(sym.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        for (Type t : closure)
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            addMembers(scope, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        return new FilteredMemberList(scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        private void addMembers(Scope scope, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            members:
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                Scope.Entry overrider = scope.lookup(e.sym.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                while (overrider.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                    if (overrider.sym.kind == e.sym.kind
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                        && (overrider.sym.flags() & Flags.SYNTHETIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                        if (overrider.sym.getKind() == ElementKind.METHOD
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                        && overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                            continue members;
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                    overrider = overrider.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                boolean derived = e.sym.getEnclosingElement() != scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                ElementKind kind = e.sym.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                boolean initializer = kind == ElementKind.CONSTRUCTOR
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                    || kind == ElementKind.INSTANCE_INIT
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                    || kind == ElementKind.STATIC_INIT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                    scope.enter(e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
     * Returns all annotations of an element, whether
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
     * inherited or directly present.
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
     * @param e  the element being examined
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
     * @return all annotations of the element
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        List<Attribute.Compound> annos = sym.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        while (sym.getKind() == ElementKind.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            Type sup = ((ClassSymbol) sym).getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            if (sup.tag != TypeTags.CLASS || sup.isErroneous() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                    sup.tsym == syms.objectType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            sym = sup.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            List<Attribute.Compound> oldAnnos = annos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            for (Attribute.Compound anno : sym.getAnnotationMirrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                if (isInherited(anno.type) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                        !containsAnnoOfType(oldAnnos, anno.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                    annos = annos.prepend(anno);
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        return annos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
     * Tests whether an annotation type is @Inherited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    private boolean isInherited(Type annotype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        for (Attribute.Compound anno : annotype.tsym.getAnnotationMirrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            if (anno.type.tsym == syms.inheritedType.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
     * Tests whether a list of annotations contains an annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     * of a given type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                                              Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        for (Attribute.Compound anno : annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
            if (anno.type.tsym == type.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    public boolean hides(Element hiderEl, Element hideeEl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        Symbol hider = cast(Symbol.class, hiderEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        Symbol hidee = cast(Symbol.class, hideeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        // Fields only hide fields; methods only methods; types only types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        // Names must match.  Nothing hides itself (just try it).
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
        if (hider == hidee ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                hider.kind != hidee.kind ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                hider.name != hidee.name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        // Only static methods can hide other methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        // Methods only hide methods with matching signatures.
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        if (hider.kind == Kinds.MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            if (!hider.isStatic() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                        !types.isSubSignature(hider.type, hidee.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        // Hider must be in a subclass of hidee's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        // in M1's class, then M1 and M2 both hide M3.
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        ClassSymbol hiderClass = hider.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        ClassSymbol hideeClass = hidee.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        if (hiderClass == null || hideeClass == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                !hiderClass.isSubClass(hideeClass, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        // Hidee must be accessible in hider's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        // The method isInheritedIn is poorly named:  it checks only access.
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        return hidee.isInheritedIn(hiderClass, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
    public boolean overrides(ExecutableElement riderEl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                             ExecutableElement rideeEl, TypeElement typeEl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        MethodSymbol rider = cast(MethodSymbol.class, riderEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        ClassSymbol origin = cast(ClassSymbol.class, typeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        return rider.name == ridee.name &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
               // not reflexive as per JLS
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
               rider != ridee &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
               // we don't care if ridee is static, though that wouldn't
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
               // compile
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
               !rider.isStatic() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
               // Symbol.overrides assumes the following
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
               ridee.isMemberOf(origin, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
               // check access and signatures; don't check return types
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
               rider.overrides(ridee, origin, types, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
    public String getConstantExpression(Object value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        return Constants.format(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
     * Print a representation of the elements to the given writer in
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
     * the specified order.  The main purpose of this method is for
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
     * diagnostics.  The exact format of the output is <em>not</em>
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
     * specified and is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
     * @param w the writer to print the output to
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
     * @param elements the elements to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    public void printElements(java.io.Writer w, Element... elements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        for (Element element : elements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    public Name getName(CharSequence cs) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   563
        return names.fromString(cs.toString());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
     * Returns the tree node and compilation unit corresponding to this
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
     * element, or null if they can't be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
    private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        Env<AttrContext> enterEnv = getEnterEnv(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        if (enterEnv == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        if (tree == null || enterEnv.toplevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
        return new Pair<JCTree,JCCompilationUnit>(tree, enterEnv.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
     * Returns the best approximation for the tree node and compilation unit
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
     * corresponding to the given element, annotation and value.
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
     * If the element is null, null is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
     * If the annotation is null or cannot be found, the tree node and
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
     * compilation unit for the element is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
     * If the annotation value is null or cannot be found, the tree node and
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
     * compilation unit for the annotation is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
    public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
                      Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        if (e == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
        Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        if (elemTreeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        if (a == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            return elemTreeTop;
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        if (annoTree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            return elemTreeTop;
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
        // 6388543: if v != null, we should search within annoTree to find
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        // the tree matching v. For now, we ignore v and return the tree of
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
        // the annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
     * Returns a symbol's enter environment, or null if it has none.
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
    private Env<AttrContext> getEnterEnv(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        // Get enclosing class of sym, or sym itself if it is a class
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        // or package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
        TypeSymbol ts = (sym.kind != Kinds.PCK)
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                        ? sym.enclClass()
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                        : (PackageSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        return (ts != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                ? enter.getEnv(ts)
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
     * Returns an object cast to the specified type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
     * @throws NullPointerException if the object is {@code null}
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
     * @throws IllegalArgumentException if the object is of the wrong type
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    private static <T> T cast(Class<T> clazz, Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        if (! clazz.isInstance(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
            throw new IllegalArgumentException(o.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
        return clazz.cast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
}