langtools/src/share/classes/com/sun/tools/javac/comp/Annotate.java
author jjg
Tue, 08 Nov 2011 11:51:05 -0800
changeset 10950 e87b50888909
parent 9603 fa337b87574b
child 13689 4d519199a6aa
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: 9603
diff changeset
     2
 * Copyright (c) 2003, 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.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    34
import static com.sun.tools.javac.tree.JCTree.Tag.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    35
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/** Enter annotations on symbols.  Annotations accumulate in a queue,
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  which is processed at the top level of any set of recursive calls
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  requesting it be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    40
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    41
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class Annotate {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    protected static final Context.Key<Annotate> annotateKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        new Context.Key<Annotate>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public static Annotate instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        Annotate instance = context.get(annotateKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            instance = new Annotate(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    final Symtab syms;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    60
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    final Resolve rs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    final Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    final ConstFold cfolder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    protected Annotate(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        context.put(annotateKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        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
    72
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 * Queue maintenance
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private int enterCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    ListBuffer<Annotator> q = new ListBuffer<Annotator>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public void later(Annotator a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        q.append(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    public void earlier(Annotator a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        q.prepend(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    /** Called when the Enter phase starts. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public void enterStart() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /** Called after the Enter phase completes. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public void enterDone() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (enterCount != 0) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            while (q.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                q.next().enterAnnotation();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    /** A client that has annotations to add registers an annotator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *  the method it will use to add the annotation.  There are no
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     *  parameters; any needed data should be captured by the
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     *  Annotator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public interface Annotator {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        void enterAnnotation();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        String toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
 * Compute an attribute from its annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    /** Process a single compound annotation, returning its
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     *  Attribute. Used from MemberEnter for attaching the attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     *  to the annotated symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    Attribute.Compound enterAnnotation(JCAnnotation a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                                       Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                                       Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        // The annotation might have had its type attributed (but not checked)
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        // need to do it again.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        Type at = (a.annotationType.type != null ? a.annotationType.type
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                  : attr.attribType(a.annotationType, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        a.type = chk.checkType(a.annotationType.pos(), at, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        if (a.type.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            log.error(a.annotationType.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                      "not.annotation.type", a.type.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        List<JCExpression> args = a.args;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   153
        if (args.length() == 1 && !args.head.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            // special case: elided "value=" assumed
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            args.head = make.at(args.head.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                Assign(make.Ident(names.value), args.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        ListBuffer<Pair<MethodSymbol,Attribute>> buf =
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            new ListBuffer<Pair<MethodSymbol,Attribute>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            JCExpression t = tl.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   162
            if (!t.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                log.error(t.pos(), "annotation.value.must.be.name.value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            JCAssign assign = (JCAssign)t;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   167
            if (!assign.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                log.error(t.pos(), "annotation.value.must.be.name.value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            JCIdent left = (JCIdent)assign.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            Symbol method = rs.resolveQualifiedMethod(left.pos(),
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   173
                                                          env,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   174
                                                          a.type,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   175
                                                          left.name,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   176
                                                          List.<Type>nil(),
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   177
                                                          null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            left.sym = method;
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            left.type = method.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            if (method.owner != a.type.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                log.error(left.pos(), "no.annotation.member", left.name, a.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            Type result = method.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            Attribute value = enterAttributeValue(result, assign.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            if (!method.type.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                buf.append(new Pair<MethodSymbol,Attribute>
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                           ((MethodSymbol)method, value));
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   187
            t.type = result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        return new Attribute.Compound(a.type, buf.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    Attribute enterAttributeValue(Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                                  JCExpression tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                                  Env<AttrContext> env) {
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   195
        //first, try completing the attribution value sym - if a completion
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   196
        //error is thrown, we should recover gracefully, and display an
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   197
        //ordinary resolution diagnostic.
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   198
        try {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   199
            expected.tsym.complete();
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   200
        } catch(CompletionFailure e) {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   201
            log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   202
            return new Attribute.Error(expected);
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   203
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            Type result = attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            if (result.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            if (result.constValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                log.error(tree.pos(), "attribute.value.must.be.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            result = cfolder.coerce(result, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            return new Attribute.Constant(expected, result.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        if (expected.tsym == syms.classType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            Type result = attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            if (result.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            if (TreeInfo.name(tree) != names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                log.error(tree.pos(), "annotation.value.must.be.class.literal");
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            return new Attribute.Class(types,
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                                       (((JCFieldAccess) tree).selected).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   227
            if (!tree.hasTag(ANNOTATION)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                log.error(tree.pos(), "annotation.value.must.be.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                expected = syms.errorType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            return enterAnnotation((JCAnnotation)tree, expected, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   234
            if (!tree.hasTag(NEWARRAY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                tree = make.at(tree.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                    NewArray(null, List.<JCExpression>nil(), List.of(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            JCNewArray na = (JCNewArray)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            if (na.elemtype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                buf.append(enterAttributeValue(types.elemtype(expected),
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                                               l.head,
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                                               env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   249
            na.type = expected;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            return new Attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                Array(expected, buf.toArray(new Attribute[buf.length()]));
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        if (expected.tag == TypeTags.CLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            (expected.tsym.flags() & Flags.ENUM) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            Symbol sym = TreeInfo.symbol(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                TreeInfo.nonstaticSelect(tree) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                sym.kind != Kinds.VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                (sym.flags() & Flags.ENUM) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            VarSymbol enumerator = (VarSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            return new Attribute.Enum(expected, enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        if (!expected.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            log.error(tree.pos(), "annotation.value.not.allowable.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        return new Attribute.Error(attr.attribExpr(tree, env, expected));
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
}