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