langtools/src/share/classes/com/sun/tools/javac/comp/Annotate.java
author jfranck
Tue, 20 Aug 2013 17:21:47 +0200
changeset 19507 323f001d6be1
parent 19122 1841f2fa76de
child 20599 5201f7144a3c
permissions -rw-r--r--
8019243: AnnotationTypeMismatchException instead of MirroredTypeException Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    28
import java.util.Map;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.util.*;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    31
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
    37
import static com.sun.tools.javac.code.TypeTag.ARRAY;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
    38
import static com.sun.tools.javac.code.TypeTag.CLASS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    39
import static com.sun.tools.javac.tree.JCTree.Tag.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    40
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/** Enter annotations on symbols.  Annotations accumulate in a queue,
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  which is processed at the top level of any set of recursive calls
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  requesting it be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    45
 *  <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
    46
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public class Annotate {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    protected static final Context.Key<Annotate> annotateKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        new Context.Key<Annotate>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    public static Annotate instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        Annotate instance = context.get(annotateKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            instance = new Annotate(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    final Symtab syms;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    65
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    final Resolve rs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    final Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    final ConstFold cfolder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    protected Annotate(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        context.put(annotateKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        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
    77
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        chk = Check.instance(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
 * Queue maintenance
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private int enterCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    ListBuffer<Annotator> q = new ListBuffer<Annotator>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    91
    ListBuffer<Annotator> typesQ = new ListBuffer<Annotator>();
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    92
    ListBuffer<Annotator> repeatedQ = new ListBuffer<Annotator>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    93
    ListBuffer<Annotator> afterRepeatedQ = new ListBuffer<Annotator>();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    94
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    95
    public void earlier(Annotator a) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    96
        q.prepend(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    97
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    99
    public void normal(Annotator a) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        q.append(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   103
    public void typeAnnotation(Annotator a) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   104
        typesQ.append(a);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   107
    public void repeated(Annotator a) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   108
        repeatedQ.append(a);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   109
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   110
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   111
    public void afterRepeated(Annotator a) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   112
        afterRepeatedQ.append(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   113
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   114
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /** Called when the Enter phase starts. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    public void enterStart() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    /** Called after the Enter phase completes. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    public void enterDone() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        if (enterCount != 0) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        try {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   130
            while (q.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                q.next().enterAnnotation();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   132
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   133
            while (typesQ.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   134
                typesQ.next().enterAnnotation();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   135
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   136
            while (repeatedQ.nonEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   137
                repeatedQ.next().enterAnnotation();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   138
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   139
            while (afterRepeatedQ.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   140
                afterRepeatedQ.next().enterAnnotation();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   141
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    /** A client that has annotations to add registers an annotator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     *  the method it will use to add the annotation.  There are no
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  parameters; any needed data should be captured by the
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     *  Annotator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    public interface Annotator {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        void enterAnnotation();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        String toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   157
    /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   158
     * This context contains all the information needed to synthesize new
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   159
     * annotations trees by the completer for repeating annotations.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   160
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   161
    public class AnnotateRepeatedContext<T extends Attribute.Compound> {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   162
        public final Env<AttrContext> env;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   163
        public final Map<Symbol.TypeSymbol, ListBuffer<T>> annotated;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   164
        public final Map<T, JCDiagnostic.DiagnosticPosition> pos;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   165
        public final Log log;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   166
        public final boolean isTypeCompound;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   167
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   168
        public AnnotateRepeatedContext(Env<AttrContext> env,
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   169
                                       Map<Symbol.TypeSymbol, ListBuffer<T>> annotated,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   170
                                       Map<T, JCDiagnostic.DiagnosticPosition> pos,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   171
                                       Log log,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   172
                                       boolean isTypeCompound) {
13840
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   173
            Assert.checkNonNull(env);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   174
            Assert.checkNonNull(annotated);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   175
            Assert.checkNonNull(pos);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   176
            Assert.checkNonNull(log);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   177
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   178
            this.env = env;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   179
            this.annotated = annotated;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   180
            this.pos = pos;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   181
            this.log = log;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   182
            this.isTypeCompound = isTypeCompound;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   183
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   184
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   185
        /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   186
         * Process a list of repeating annotations returning a new
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   187
         * Attribute.Compound that is the attribute for the synthesized tree
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   188
         * for the container.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   189
         *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   190
         * @param repeatingAnnotations a List of repeating annotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   191
         * @return a new Attribute.Compound that is the container for the repeatingAnnotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   192
         */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   193
        public T processRepeatedAnnotations(List<T> repeatingAnnotations, Symbol sym) {
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   194
            return Annotate.this.processRepeatedAnnotations(repeatingAnnotations, this, sym);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   195
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   196
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   197
        /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   198
         * Queue the Annotator a on the repeating annotations queue of the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   199
         * Annotate instance this context belongs to.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   200
         *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   201
         * @param a the Annotator to enqueue for repeating annotation annotating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   202
         */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   203
        public void annotateRepeated(Annotator a) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   204
            Annotate.this.repeated(a);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   205
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   206
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
 * Compute an attribute from its annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    /** Process a single compound annotation, returning its
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     *  Attribute. Used from MemberEnter for attaching the attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     *  to the annotated symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    Attribute.Compound enterAnnotation(JCAnnotation a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                                       Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                                       Env<AttrContext> env) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   219
        return enterAnnotation(a, expected, env, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   220
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   221
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   222
    Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   223
            Type expected,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   224
            Env<AttrContext> env) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   225
        return (Attribute.TypeCompound) enterAnnotation(a, expected, env, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   226
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   227
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   228
    // boolean typeAnnotation determines whether the method returns
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   229
    // a Compound (false) or TypeCompound (true).
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   230
    Attribute.Compound enterAnnotation(JCAnnotation a,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   231
            Type expected,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   232
            Env<AttrContext> env,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   233
            boolean typeAnnotation) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        // The annotation might have had its type attributed (but not checked)
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        // need to do it again.
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        Type at = (a.annotationType.type != null ? a.annotationType.type
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                  : attr.attribType(a.annotationType, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        a.type = chk.checkType(a.annotationType.pos(), at, expected);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   240
        if (a.type.isErroneous()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   241
            if (typeAnnotation) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   242
                return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   243
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   244
                return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   245
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   246
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            log.error(a.annotationType.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                      "not.annotation.type", a.type.toString());
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   250
            if (typeAnnotation) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   251
                return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   252
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   253
                return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   254
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        List<JCExpression> args = a.args;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   257
        if (args.length() == 1 && !args.head.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            // special case: elided "value=" assumed
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            args.head = make.at(args.head.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                Assign(make.Ident(names.value), args.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        ListBuffer<Pair<MethodSymbol,Attribute>> buf =
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            new ListBuffer<Pair<MethodSymbol,Attribute>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            JCExpression t = tl.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   266
            if (!t.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                log.error(t.pos(), "annotation.value.must.be.name.value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            JCAssign assign = (JCAssign)t;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   271
            if (!assign.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                log.error(t.pos(), "annotation.value.must.be.name.value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            JCIdent left = (JCIdent)assign.lhs;
19122
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 17578
diff changeset
   276
            Symbol method = rs.resolveQualifiedMethod(assign.rhs.pos(),
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   277
                                                          env,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   278
                                                          a.type,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   279
                                                          left.name,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   280
                                                          List.<Type>nil(),
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   281
                                                          null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            left.sym = method;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            left.type = method.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            if (method.owner != a.type.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                log.error(left.pos(), "no.annotation.member", left.name, a.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            Type result = method.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            Attribute value = enterAttributeValue(result, assign.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            if (!method.type.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                buf.append(new Pair<MethodSymbol,Attribute>
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                           ((MethodSymbol)method, value));
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   291
            t.type = result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   293
        if (typeAnnotation) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   294
            if (a.attribute == null || !(a.attribute instanceof Attribute.TypeCompound)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   295
                // Create a new TypeCompound
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   296
                Attribute.TypeCompound tc = new Attribute.TypeCompound(a.type, buf.toList(), new TypeAnnotationPosition());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   297
                a.attribute = tc;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   298
                return tc;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   299
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   300
                // Use an existing TypeCompound
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   301
                return a.attribute;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   302
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   303
        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   304
            Attribute.Compound ac = new Attribute.Compound(a.type, buf.toList());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   305
            a.attribute = ac;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   306
            return ac;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   307
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    Attribute enterAttributeValue(Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                                  JCExpression tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                                  Env<AttrContext> env) {
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   313
        //first, try completing the attribution value sym - if a completion
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   314
        //error is thrown, we should recover gracefully, and display an
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   315
        //ordinary resolution diagnostic.
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   316
        try {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   317
            expected.tsym.complete();
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   318
        } catch(CompletionFailure e) {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   319
            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
   320
            return new Attribute.Error(expected);
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   321
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            Type result = attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            if (result.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            if (result.constValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                log.error(tree.pos(), "attribute.value.must.be.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            result = cfolder.coerce(result, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            return new Attribute.Constant(expected, result.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        if (expected.tsym == syms.classType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            Type result = attr.attribExpr(tree, env, expected);
19507
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   335
            if (result.isErroneous()) {
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   336
                // Does it look like a class literal?
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   337
                if (TreeInfo.name(tree) == names._class) {
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   338
                    Name n = (((JCFieldAccess) tree).selected).type.tsym.flatName();
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   339
                    return new Attribute.UnresolvedClass(expected,
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   340
                            types.createErrorType(n,
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   341
                                    syms.unknownSymbol, syms.classType));
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   342
                } else {
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   343
                    return new Attribute.Error(expected);
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   344
                }
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   345
            }
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   346
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   347
            // Class literals look like field accesses of a field named class
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   348
            // at the tree level
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            if (TreeInfo.name(tree) != names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                log.error(tree.pos(), "annotation.value.must.be.class.literal");
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            return new Attribute.Class(types,
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                                       (((JCFieldAccess) tree).selected).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   357
            if (!tree.hasTag(ANNOTATION)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                log.error(tree.pos(), "annotation.value.must.be.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                expected = syms.errorType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            return enterAnnotation((JCAnnotation)tree, expected, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        }
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
   363
        if (expected.hasTag(ARRAY)) { // should really be isArray()
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   364
            if (!tree.hasTag(NEWARRAY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                tree = make.at(tree.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                    NewArray(null, List.<JCExpression>nil(), List.of(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            JCNewArray na = (JCNewArray)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            if (na.elemtype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                buf.append(enterAttributeValue(types.elemtype(expected),
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                                               l.head,
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                                               env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   379
            na.type = expected;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            return new Attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                Array(expected, buf.toArray(new Attribute[buf.length()]));
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        }
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
   383
        if (expected.hasTag(CLASS) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            (expected.tsym.flags() & Flags.ENUM) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            Symbol sym = TreeInfo.symbol(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                TreeInfo.nonstaticSelect(tree) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                sym.kind != Kinds.VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                (sym.flags() & Flags.ENUM) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            VarSymbol enumerator = (VarSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            return new Attribute.Enum(expected, enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        if (!expected.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            log.error(tree.pos(), "annotation.value.not.allowable.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        return new Attribute.Error(attr.attribExpr(tree, env, expected));
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   401
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   402
    /* *********************************
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   403
     * Support for repeating annotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   404
     ***********************************/
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   405
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   406
    /* Process repeated annotations. This method returns the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   407
     * synthesized container annotation or null IFF all repeating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   408
     * annotation are invalid.  This method reports errors/warnings.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   409
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   410
    private <T extends Attribute.Compound> T processRepeatedAnnotations(List<T> annotations,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   411
            AnnotateRepeatedContext<T> ctx,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   412
            Symbol on) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   413
        T firstOccurrence = annotations.head;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   414
        List<Attribute> repeated = List.nil();
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   415
        Type origAnnoType = null;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   416
        Type arrayOfOrigAnnoType = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   417
        Type targetContainerType = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   418
        MethodSymbol containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   419
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   420
        Assert.check(!annotations.isEmpty() &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   421
                     !annotations.tail.isEmpty()); // i.e. size() > 1
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   422
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   423
        int count = 0;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   424
        for (List<T> al = annotations;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   425
             !al.isEmpty();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   426
             al = al.tail)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   427
        {
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   428
            count++;
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   429
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   430
            // There must be more than a single anno in the annotation list
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   431
            Assert.check(count > 1 || !al.tail.isEmpty());
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   432
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   433
            T currentAnno = al.head;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   434
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   435
            origAnnoType = currentAnno.type;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   436
            if (arrayOfOrigAnnoType == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   437
                arrayOfOrigAnnoType = types.makeArrayType(origAnnoType);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   438
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   439
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   440
            // Only report errors if this isn't the first occurrence I.E. count > 1
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   441
            boolean reportError = count > 1;
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   442
            Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno), reportError);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   443
            if (currentContainerType == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   444
                continue;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   445
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   446
            // Assert that the target Container is == for all repeated
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   447
            // annos of the same annotation type, the types should
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   448
            // come from the same Symbol, i.e. be '=='
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   449
            Assert.check(targetContainerType == null || currentContainerType == targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   450
            targetContainerType = currentContainerType;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   451
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   452
            containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   453
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   454
            if (containerValueSymbol == null) { // Check of CA type failed
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   455
                // errors are already reported
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   456
                continue;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   457
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   458
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   459
            repeated = repeated.prepend(currentAnno);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   460
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   461
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   462
        if (!repeated.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   463
            repeated = repeated.reverse();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   464
            TreeMaker m = make.at(ctx.pos.get(firstOccurrence));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   465
            Pair<MethodSymbol, Attribute> p =
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   466
                    new Pair<MethodSymbol, Attribute>(containerValueSymbol,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   467
                                                      new Attribute.Array(arrayOfOrigAnnoType, repeated));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   468
            if (ctx.isTypeCompound) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   469
                /* TODO: the following code would be cleaner:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   470
                Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   471
                        ((Attribute.TypeCompound)annotations.head).position);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   472
                JCTypeAnnotation annoTree = m.TypeAnnotation(at);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   473
                at = enterTypeAnnotation(annoTree, targetContainerType, ctx.env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   474
                */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   475
                // However, we directly construct the TypeCompound to keep the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   476
                // direct relation to the contained TypeCompounds.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   477
                Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   478
                        ((Attribute.TypeCompound)annotations.head).position);
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   479
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   480
                // TODO: annotation applicability checks from below?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   481
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   482
                at.setSynthesized(true);
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   483
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   484
                @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   485
                T x = (T) at;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   486
                return x;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   487
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   488
                Attribute.Compound c = new Attribute.Compound(targetContainerType, List.of(p));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   489
                JCAnnotation annoTree = m.Annotation(c);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   490
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   491
                if (!chk.annotationApplicable(annoTree, on))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   492
                    log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType);
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   493
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   494
                if (!chk.validateAnnotationDeferErrors(annoTree))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   495
                    log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   496
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   497
                c = enterAnnotation(annoTree, targetContainerType, ctx.env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   498
                c.setSynthesized(true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   499
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   500
                @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   501
                T x = (T) c;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   502
                return x;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   503
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   504
        } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   505
            return null; // errors should have been reported elsewhere
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   506
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   507
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   508
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   509
    /** Fetches the actual Type that should be the containing annotation. */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   510
    private Type getContainingType(Attribute.Compound currentAnno,
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   511
            DiagnosticPosition pos,
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   512
            boolean reportError)
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   513
    {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   514
        Type origAnnoType = currentAnno.type;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   515
        TypeSymbol origAnnoDecl = origAnnoType.tsym;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   516
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   517
        // Fetch the Repeatable annotation from the current
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   518
        // annotation's declaration, or null if it has none
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   519
        Attribute.Compound ca = origAnnoDecl.attribute(syms.repeatableType.tsym);
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   520
        if (ca == null) { // has no Repeatable annotation
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   521
            if (reportError)
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   522
                log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.repeatableType);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   523
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   524
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   525
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   526
        return filterSame(extractContainingType(ca, pos, origAnnoDecl),
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   527
                          origAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   528
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   529
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   530
    // returns null if t is same as 's', returns 't' otherwise
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   531
    private Type filterSame(Type t, Type s) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   532
        if (t == null || s == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   533
            return t;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   534
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   535
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   536
        return types.isSameType(t, s) ? null : t;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   537
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   538
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   539
    /** Extract the actual Type to be used for a containing annotation. */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   540
    private Type extractContainingType(Attribute.Compound ca,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   541
            DiagnosticPosition pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   542
            TypeSymbol annoDecl)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   543
    {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   544
        // The next three checks check that the Repeatable annotation
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   545
        // on the declaration of the annotation type that is repeating is
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   546
        // valid.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   547
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   548
        // Repeatable must have at least one element
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   549
        if (ca.values.isEmpty()) {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   550
            log.error(pos, "invalid.repeatable.annotation", annoDecl);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   551
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   552
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   553
        Pair<MethodSymbol,Attribute> p = ca.values.head;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   554
        Name name = p.fst.name;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   555
        if (name != names.value) { // should contain only one element, named "value"
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   556
            log.error(pos, "invalid.repeatable.annotation", annoDecl);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   557
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   558
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   559
        if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   560
            log.error(pos, "invalid.repeatable.annotation", annoDecl);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   561
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   562
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   563
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   564
        return ((Attribute.Class)p.snd).getValue();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   565
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   566
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   567
    /* Validate that the suggested targetContainerType Type is a valid
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   568
     * container type for repeated instances of originalAnnoType
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   569
     * annotations. Return null and report errors if this is not the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   570
     * case, return the MethodSymbol of the value element in
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   571
     * targetContainerType if it is suitable (this is needed to
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   572
     * synthesize the container). */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   573
    private MethodSymbol validateContainer(Type targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   574
                                           Type originalAnnoType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   575
                                           DiagnosticPosition pos) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   576
        MethodSymbol containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   577
        boolean fatalError = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   578
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   579
        // Validate that there is a (and only 1) value method
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   580
        Scope scope = targetContainerType.tsym.members();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   581
        int nr_value_elems = 0;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   582
        boolean error = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   583
        for(Symbol elm : scope.getElementsByName(names.value)) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   584
            nr_value_elems++;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   585
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   586
            if (nr_value_elems == 1 &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   587
                elm.kind == Kinds.MTH) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   588
                containerValueSymbol = (MethodSymbol)elm;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   589
            } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   590
                error = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   591
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   592
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   593
        if (error) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   594
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   595
                      "invalid.repeatable.annotation.multiple.values",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   596
                      targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   597
                      nr_value_elems);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   598
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   599
        } else if (nr_value_elems == 0) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   600
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   601
                      "invalid.repeatable.annotation.no.value",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   602
                      targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   603
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   604
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   605
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   606
        // validate that the 'value' element is a method
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   607
        // probably "impossible" to fail this
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   608
        if (containerValueSymbol.kind != Kinds.MTH) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   609
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   610
                      "invalid.repeatable.annotation.invalid.value",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   611
                      targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   612
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   613
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   614
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   615
        // validate that the 'value' element has the correct return type
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   616
        // i.e. array of original anno
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   617
        Type valueRetType = containerValueSymbol.type.getReturnType();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   618
        Type expectedType = types.makeArrayType(originalAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   619
        if (!(types.isArray(valueRetType) &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   620
              types.isSameType(expectedType, valueRetType))) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   621
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   622
                      "invalid.repeatable.annotation.value.return",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   623
                      targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   624
                      valueRetType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   625
                      expectedType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   626
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   627
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   628
        if (error) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   629
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   630
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   631
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   632
        // The conditions for a valid containing annotation are made
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   633
        // in Check.validateRepeatedAnnotaton();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   634
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   635
        return fatalError ? null : containerValueSymbol;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   636
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   637
}