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